pat:1048. Find Coins (25) (two-point lookup) AC

Source: Internet
Author: User

#include<stdio.h>#include<algorithm>Usingnamespace Std;int arr[100066];int FIND (int L,int R,int aim)Binary lookup, from L to R, find aim{int mid;while (l<=r) {mid= (l+r)/2;if (Arr[mid]==aim)return mid;Find: Return coordinatesElseif (Arr[mid]<aim) l=mid+1;else r=mid-1; }Return-1;Not found}int main () {int n,m; scanf"%d%d", &n,&m);Forint i=0; I<n; ++i) scanf ("%d", &arr[i]); Sort (arr,arr+n);Sorting before you can find it with two pointsfor (int i=0; i<n; ++i) {int x=find (i+1,n-1,m-arr[i]); //find the opposite number labeled I number, L from the beginning of the i+1 can be guaranteed to find the number subscript is not the original number. And the array ascending, the front is not found, the back is not used in front of the if (x!=-1) //- 1 represents the existence of opposing numbers//another notation: FIND (0,n-1,m-arr[i]) if (x!=-1 && x!=i) also requires that the existing number is not the original arr[i] {printf (< Span class= "delimiter" > "%d%d" arr[i],m-arr[i]); return 0;} printf ( "no solution" ); return 0;}          

pat:1048. Find Coins (25) (two-point lookup) AC

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.