• 二分


    stl lower_bound()返回的是第一个大于等于查询值的iterator,upper_bound()返回的是第一个大于查询值的iterator。目测相当于二分。对int a【】,lower_bound(a,a+len,val)-a,可获得第一个>=val的id。

     Prime Gap http://poj.org/problem?id=3518

    素数筛法+二分

     1 #include<cstdio>
     2 #include<cstring>
     3 #define mt(a,b) memset(a,b,sizeof(a))
     4 const int M=1300000;
     5 bool vis[M];
     6 int prim[M];
     7 int main(){
     8     int lp=0;
     9     mt(vis,0);
    10     for(int i=2;i*i<M;i++){
    11         if(!vis[i]){
    12             for(int j=i*i;j<M;j+=i){
    13                 vis[j]=true;
    14             }
    15         }
    16     }
    17     for(int i=2;i<M;i++){
    18         if(!vis[i]){
    19             prim[lp++]=i;
    20         }
    21     }
    22     int n;
    23     while(~scanf("%d",&n),n){
    24         int L=0,R=lp-1;
    25         while(L<=R){
    26             int mid=(L+R)>>1;
    27             if(prim[mid]>=n){
    28                 R=mid-1;
    29             }
    30             else{
    31                 L=mid+1;
    32             }
    33         }
    34         if(prim[L]==n){
    35             puts("0");
    36         }
    37         else{
    38             printf("%d
    ",prim[L]-prim[L-1]);
    39         }
    40     }
    41     return 0;
    42 }
    View Code

     upper找的是第一个大于的

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #define mt(a,b) memset(a,b,sizeof(a))
     5 using namespace std;
     6 const int M=1300000;
     7 bool vis[M];
     8 int prim[M];
     9 int main(){
    10     int lp=0;
    11     mt(vis,0);
    12     for(int i=2;i*i<M;i++){
    13         if(!vis[i]){
    14             for(int j=i*i;j<M;j+=i){
    15                 vis[j]=true;
    16             }
    17         }
    18     }
    19     for(int i=2;i<M;i++){
    20         if(!vis[i]){
    21             prim[lp++]=i;
    22         }
    23     }
    24     int n;
    25     while(~scanf("%d",&n),n){
    26         int L=upper_bound(prim,prim+lp,n)-prim;
    27         if(prim[L-1]==n){
    28             puts("0");
    29         }
    30         else{
    31             printf("%d
    ",prim[L]-prim[L-1]);
    32         }
    33     }
    34     return 0;
    35 }
    View Code

    lower找到是第一个大于等于的

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #define mt(a,b) memset(a,b,sizeof(a))
     5 using namespace std;
     6 const int M=1300000;
     7 bool vis[M];
     8 int prim[M];
     9 int main(){
    10     int lp=0;
    11     mt(vis,0);
    12     for(int i=2;i*i<M;i++){
    13         if(!vis[i]){
    14             for(int j=i*i;j<M;j+=i){
    15                 vis[j]=true;
    16             }
    17         }
    18     }
    19     for(int i=2;i<M;i++){
    20         if(!vis[i]){
    21             prim[lp++]=i;
    22         }
    23     }
    24     int n;
    25     while(~scanf("%d",&n),n){
    26         int L=lower_bound(prim,prim+lp,n)-prim;
    27         if(prim[L]==n){
    28             puts("0");
    29         }
    30         else{
    31             printf("%d
    ",prim[L]-prim[L-1]);
    32         }
    33     }
    34     return 0;
    35 }
    View Code

     B. Books http://codeforces.com/contest/279/problem/B

    二分找终点。

     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 typedef __int64 LL;
     5 const int M=100010;
     6 int a[M];
     7 LL sum[M];
     8 int main(){
     9     int n,t;
    10     while(~scanf("%d%d",&n,&t)){
    11         for(int i=1;i<=n;i++){
    12             scanf("%d",&a[i]);
    13         }
    14         sum[0]=0;
    15         sum[1]=a[1];
    16         for(int i=2;i<=n;i++){
    17             sum[i]=sum[i-1]+a[i];
    18         }
    19         int ans=0;
    20         for(int head=1;head<=n;head++){
    21             int L=head,R=n;
    22             while(L<=R){
    23                 int mid=(L+R)>>1;
    24                 if(sum[mid]-sum[head-1]<=t){
    25                     L=mid+1;
    26                 }
    27                 else{
    28                     R=mid-1;
    29                 }
    30             }
    31             ans=max(ans,L-head);
    32         }
    33         printf("%d
    ",ans);
    34     }
    35     return 0;
    36 }
    View Code

    stl lower

     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 typedef __int64 LL;
     5 const int M=100010;
     6 int a[M];
     7 LL sum[M];
     8 int main(){
     9     int n,t;
    10     while(~scanf("%d%d",&n,&t)){
    11         for(int i=1;i<=n;i++){
    12             scanf("%d",&a[i]);
    13         }
    14         sum[0]=0;
    15         for(int i=1;i<=n;i++){
    16             sum[i]=sum[i-1]+a[i];
    17         }
    18         int ans=0;
    19         for(int head=1;head<=n;head++){
    20             int R=lower_bound(sum,sum+n+1,sum[head-1]+t)-sum;
    21             if(sum[R]-sum[head-1]>t||R>n) R--;
    22             ans=max(ans,R-head+1);
    23         }
    24         printf("%d
    ",ans);
    25     }
    26     return 0;
    27 }
    View Code

    end

  • 相关阅读:
    367 Valid Perfect Square 有效的完全平方数
    365 Water and Jug Problem 水壶问题
    363 Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
    357 Count Numbers with Unique Digits 计算各个位数不同的数字个数
    SpringBoot (四) :thymeleaf 使用详解
    SpringBoot(三) :Spring boot 中 Redis 的使用
    SpringBoot(二) :web综合开发
    SpringBoot (一) :入门篇
    程序员最核心的竞争力是什么?
    Java面试题:多继承
  • 原文地址:https://www.cnblogs.com/gaolzzxin/p/3877165.html
Copyright © 2020-2023  润新知