首页 > 代码库 > 【二分法】【尺取法】bzoj2348 [Baltic 2011]Plagiarism

【二分法】【尺取法】bzoj2348 [Baltic 2011]Plagiarism

一开始以为死于精度……调了半天发现死于long long……

一、二分法:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;bool cmp(const int &a,const int &b){return a>b;}int n,a[100001],b[100001];long long ans;int main(){    scanf("%d",&n);    for(int i=1;i<=n;++i) scanf("%d",&a[i]);    sort(a+1,a+n+1,cmp);    memcpy(b,a,(n+1)*sizeof(int));    for(int i=1;i<=n;++i) b[i]*=10;    for(int i=1;i<=n;++i) ans+=(long long)(upper_bound(b+i+1,b+n+1,a[i]*9,cmp)-b-i-1);    printf("%lld\n",ans);    return 0;}

二、尺取法:

#include<cstdio>#include<algorithm>using namespace std;bool cmp(const int &a,const int &b){return a>b;}int n,a[100001];long long ans;int main(){    scanf("%d",&n);    for(int i=1;i<=n;++i) scanf("%d",&a[i]);    sort(a+1,a+n+1,cmp);    int head=1,tail=1;    while(1)      {        while(a[tail]*10>=a[head]*9 && tail<=n) ++tail;        if(head==n) break;        ans+=(long long)(tail-head-1);        ++head;      }    printf("%lld\n",ans);    return 0;}

 

【二分法】【尺取法】bzoj2348 [Baltic 2011]Plagiarism