Turtle vs. Rabbit Race: Optimal Trainings
https://codeforces.com/problemset/problem/1933/E
前缀和 + 二分查找,之前一直用三分,好像不太行?
总之找到u和u+1的就行
代码:
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
typedef unsigned long long ll;
using namespace std;
int T,n,a[100100],sum[100100];
signed main(){
cin>>T;
while(T--){
cin>>n;
for(int i=1;i<=n;i++){cin>>a[i];sum[i]=sum[i-1]+a[i];}
int q;cin>>q;
while(q--){
int l,u;
cin>>l>>u;
int L=l,R=n;
while(L<R){
int mid=(L+R+1)>>1;
if(sum[mid]-sum[l-1]<=u) L=mid;
else R=mid-1;
}
L=R;
int s1=abs(sum[L]-sum[l-1]-u),s2=abs(sum[L+1]-sum[l-1]-u);
if(L==n) cout<<L<<" ";
else{
if(s1<=s2-1) cout<<L<<" ";
else cout<<L+1<<" ";
}
}
cout<<endl;
}
return 0;
}