链接:http://codeforces.com/contest/975/problem/C
想到了用前缀和+二分来写这道题,心想 没想到劳资刚学的lower_bound()这么快就派上用场了 我真是个天才
然后我竟然不知道怎么想的构造了两个前缀和,然后xjb二分,改了一个小时也没改对···
代码:
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define ll long long 6 #define max3(a,b,c) fmax(a,fmax(b,c)) 7 #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); 8 9 const int MAXN = 2e5+10; 10 ll pre[MAXN],a[MAXN],b[MAXN]; 11 12 int main() 13 { 14 ios 15 int n,q; 16 scanf("%d %d",&n,&q); 17 for(int i = 0;i < n;i++) scanf("%lld",&a[i]); 18 pre[0] = a[0]; 19 for(int i = 1;i < n;i++) pre[i] = pre[i-1]+a[i]; 20 for(int i = 0;i < q;i++) scanf("%lld",&b[i]); 21 ll ans = 0; 22 for(int i = 0;i < q;i++) 23 { 24 ans += b[i]; 25 if(ans >= pre[n-1]) 26 { 27 printf("%d ",n); 28 ans = 0; 29 } 30 else 31 { 32 int pos = lower_bound(pre,pre+n,ans)-pre; 33 34 int tt = 0; 35 if(pre[pos] == ans) 36 { 37 printf("%d ",n-pos-1); 38 } 39 else 40 { 41 printf("%d ",n-pos); 42 } 43 } 44 } 45 return 0; 46 }