[Codeforces] 474B - Worms

题意:

输入一个数字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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值