Codeforces 474B Worms 二分法(水

主题链接:http://codeforces.com/contest/474/problem/B

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>

using namespace std;

template <class T>
inline bool rd(T &ret) {
    char c; int sgn;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    ret*=sgn;
    return 1;
}
template <class T>
inline void pt(T x) {
    if (x <0) {
        putchar('-');
        x = -x;
    }
    if(x>9) pt(x/10);
    putchar(x%10+'0');
}

const int N = 100005;
int a[N], n;

int main(){
    while(rd(n)){
        for(int i = 1; i <= n; i++)rd(a[i]);
        for(int i = 2; i <= n; i++)a[i]+=a[i-1];
        a[0] = -100;
        a[n+1] = 10000000;
        int que, u; rd(que);
        while(que--){
            rd(u);
            int v = lower_bound(a+1, a+2+n, u) - a;
            if(a[v] == u)
                pt(v);
            else
                pt(v); putchar('\n');
        }
    }
    return 0;
}


版权声明:本文博客原创文章,博客,未经同意,不得转载。

posted @ 2015-07-27 14:42  hrhguanli  阅读(208)  评论(0编辑  收藏  举报