标签:log 相加 cstring check blog and ret 暴力 file
Input
Output
Sample Input
6 -45 22 42 -16 -41 -27 56 30 -36 53 -37 77 -36 30 -75 -46 26 -38 -10 62 -32 -54 -6 45
Sample Output
5
Hint
#include<iostream> #include<algorithm> #include<cmath> using namespace std; int a[4005],b[4005],c[4005],d[4005]; int ab[16000005],cd[16000005]; int main() { int n,k=0,l=0,count=0; cin>>n; for(int i=0;i<n;i++) cin>>a[i]>>b[i]>>c[i]>>d[i]; for(int i=0;i<n;i++) for(int j=0;j<n;j++) ab[k++]=a[i]+b[j]; for(int i=0;i<n;i++) for(int j=0;j<n;j++) cd[l++]=c[i]+d[j]; sort(ab,ab+k); sort(cd,cd+l); int x=n*n-1,num=0; for(int i=0;i<n*n;i++){ while(x>=0&&ab[i]+cd[x]>0) x--; if(x<0) break; num=x; while(ab[i]+cd[num]==0&&num>=0){ count++; num--; } } cout<<count<<endl; return 0; }
二分
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int a[4001],b[4001],c[4001],d[4001]; int ab[4000*4000+1],cd[4000*4000+1]; int k2; int check(int x) { int left=1,right=k2-1,mid; while (left<=right) { mid=(left+right)/2; if (x==cd[mid]) { int w=0,e=mid; while (x==cd[e]&&e<k2) e++,w++; e=mid-1; while (x==cd[e]&&e>0) e--,w++; return w; } else if (x<cd[mid]) right=mid-1; else left=mid+1; } return 0; } int main() { int t,i,j,q; while (~scanf("%d",&t)) { for (i=1;i<=t;i++) scanf("%d %d %d %d",&a[i],&b[i],&c[i],&d[i]); memset(ab,0,sizeof(ab)); memset(cd,0,sizeof(cd)); int k1=1,sum=0; k2=1; for (i=1;i<=t;i++) { for (j=1;j<=t;j++) { ab[k1++]=a[i]+b[j]; cd[k2++]=-(c[i]+d[j]); } } sort(cd+1,cd+k2); for (i=1;i<k1;i++) sum+=check(ab[i]); printf("%d\n",sum); } return 0; }
标签:log 相加 cstring check blog and ret 暴力 file
原文地址:http://www.cnblogs.com/zzzying/p/7290508.html