Letters CodeForces - 978C
题意:n个寝室,每个寝室多个房间(1-a[i]),一共m封信只知道房间号,现在要求把信送到第几个寝室的第几个房间里去,房间号从第一个寝室到第n个寝室的第一个房间到最后一个房间排下去
题意:用个二分查找吧,省点时间,二分才658ms,爆搜1600多ms
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<sstream> #include<cmath> #include<cstdlib> #include<queue> #include<map> using namespace std; #define INF 0x3f3f3f3f const int maxn=200004; int main() { long long int n,m; cin>>n>>m; long long int sum[maxn]={0}; long long int a[maxn]; for(long long int i=1;i<=n;i++) { cin>>a[i]; sum[i]=sum[i-1]+a[i]; } while(m--) { int flag; long long int t; scanf("%lld",&t); long long int l=0,r=n; while(r-l>1) { long long int mid=(l+r)/2; if(sum[mid]>t) r=mid; else l=mid; } if(t-sum[l]==0) printf("%lld %lld ",l,a[l]); else printf("%lld %lld ",l+1,t-sum[l]); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
int l = 1,r = n; while(r > l) { int mid = (l + r + 1) >> 1; if(... > ...) r = mid - 1; else l = mid; }