您的位置:首页 > 其它

[Codeforces] 474B - Worms

2015-01-25 17:50 447 查看
题意:

输入一个数字n,接着输入n个数字ai,分别代表区间[1, a1],[a1+1, a2],[a2+1, a3]……[an-1+1, an]。

接着输入一个数字m,随后m条询问,询问某个数字在第几段区间内。

由于Sigma(ai) <= 106,此题两种做法,一种做法是读入后二分查找。

另一种做法是定义一个106的数组,每次读入一个ai的时候,将这段区间的下标初始化为区间段号即可。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
#include <time.h>
typedef long long LL;
const int INF = 500000001;
const double EPS = 1e-9;
const double PI = acos(-1.0);
using namespace std;
int main()
{
#ifdef _1Test
freopen("test0.in", "r", stdin);
freopen("test0.out", "w", stdout);
srand(time(NULL));
#endif
int n, ans[1000005];
int s, e, tmp, m, k, cnt;
while(~scanf("%d", &n))
{
k = cnt = 1;
for(int i = 0; i < n; i++)
{
scanf("%d", &tmp);
for(int j = k; j < k + tmp; j++)
{
ans[j] = cnt;
}
k = k + tmp;
++cnt;
}
scanf("%d", &m);
for(int i = 0; i < m; i++)
{
scanf("%d", &tmp);
printf("%d\n", ans[tmp]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息