[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的时候,将这段区间的下标初始化为区间段号即可。
输入一个数字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; }
相关文章推荐
- CodeForces 474B(Worms)
- Codeforces 271 Div 2 B. Worms
- Codeforces 474 F. Ant colony
- Codeforces 474 D. Flowers
- Codeforces 474 E. Pillars
- Codeforces 474D (五一训练 F)+DP
- 【CODEFORCES】 B. Worms
- Codeforces 474 F. Ant colony
- Codeforces 474 E. Pillars
- codeforces—— 474B —— Worms
- Codeforces 474 C. Captain Marmot
- Codeforces 474B Worms 二分(水
- Codeforces 474 D Flowers
- Codeforces 474 C. Captain Marmot
- Codeforces 474 D. Flowers
- Codeforces 474 F. Ant colony
- CodeForces 474 E. Pillars
- Codeforces 474(#271 Div 2) 解题报告
- Codeforces 474 D Flowers(DP)
- Codeforces 474 A