题目链接:http://codeforces.com/contest/474/problem/B
解题报告:给你n个堆,第i个堆有ai个物品,物品的编号从1开始,第一堆的编号从1到a1,第二堆编号从a1+1到a1+a2.......,现在有m次查询,给你一个编号,让你求出这个编号的物品在第几个堆。
离线算法,先把m次查询按照编号从小到大排序,然后全部求出来之后按照输入的顺序排序就行了。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int fan[][]; struct node
{
int d,cixu,ans;
}lab[];
bool cmpd(node a,node b)
{
return a.d < b.d;
}
bool cmpcixu(node a,node b)
{
return a.cixu < b.cixu;
}
int main()
{
int n,m;
while(scanf("%d",&n)!=EOF)
{
int d;
fan[][] = ;
for(int i = ;i <= n;++i)
{
scanf("%d",&d);
fan[i][] = fan[i-][] + ;
fan[i][] = fan[i][] + d - ;
}
scanf("%d",&m);
for(int i = ;i < m;++i)
{
scanf("%d",&lab[i].d);
lab[i].cixu = i;
}
sort(lab,lab+m,cmpd);
int f = ;
for(int i = ;i < m;++i)
{
while()
{
if(lab[i].d >= fan[f][] && lab[i].d <= fan[f][])
{
lab[i].ans = f;
break;
}
f++;
}
}
sort(lab,lab+m,cmpcixu);
for(int i = ;i < m;++i)
printf("%d\n",lab[i].ans);
}
return ;
}