PAT (Advanced Level) 1048. Find Coins (25)
先对序列排序,然后枚举较小值,二分较大值。
#include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<vector> using namespace std; const int maxn=100000+10; int a[maxn]; int n,k; int main() { scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) scanf("%d",&a[i]); sort(a+1,a+1+n); int Find=0; for(int i=1;i<=n;i++) { int l=i+1,r=n; while(l<=r) { int mid=(l+r)/2; if(a[mid]>k-a[i]) r=mid-1; else if(a[mid]<k-a[i]) l=mid+1; else {Find=1;break;} } if(Find==1) { printf("%d %d\n",a[i],k-a[i]); break; } } if(Find==0) printf("No Solution\n"); return 0; }