测试 4

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
char s[100005];
int i,p,o,ans;
int main(){
    freopen("shower.in","r",stdin);
    freopen("shower.out","w",stdout);
    scanf("%s",s); p=strlen(s);
    for(i=0;i<p;i++){
        if(s[i]==')'){
            if(o==0){
                o++;
                ans++;
            }
            else    o--;
        }
        else    o++;
    }
    cout<<ans+o/2;
    return 0;
}

思路:首先线性筛预处理出前缀和。

然后二分答案。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 1000010
#define LL long long
using namespace std;
int tot,N,T,k;
int prime[80010];
LL sum[80010];
bool vis[MAXN];
void pre(){
    for(int i=2;i<MAXN;i++) {
        if(!vis[i]) prime[++tot]=i;
        for(int j=1;j<=tot;j++) {
            if(i*prime[j]>MAXN) break;
            vis[prime[j]*i]=true;
            if(i%prime[j]==0) break;
        }
    }
}
int main() {
    freopen("diary.in","r",stdin);
    freopen("diary.out","w",stdout);
    pre();
    for(int i=1;i<=tot;++i)    sum[i]=(LL)prime[i]+sum[i-1];
    scanf("%d",&T);
    while(T--) {
        scanf("%d%d",&N,&k);
        int l=0,r=tot+1;
        while(l+1<r) {
            int mid=(l+r)>>1;
            LL p=sum[mid+k-1]-sum[mid-1];
            if(p<=N) l=mid;
            else r=mid;
        }
        if(!l)    cout<<"-1"<<endl;
        else    cout<<sum[l+k-1]-sum[l-1]<<endl;
    }
    return 0;
}

 

posted @ 2017-11-05 17:16  一蓑烟雨任生平  阅读(162)  评论(0编辑  收藏  举报