TO 3166: Best Messenger Tool――NKQQ

博客围绕NKQQ工具展开,用户每小时自动积累1分,达到一定分数可升级。给出人数及起始经验值,提出多个问题,询问经过若干小时后达到某等级的人数。作者起初用树状数组解题遇TLE,后调整思路,将超2e5的值转化,建立数组统计并计算区间和,分类讨论得出结果。

描述

Mr. Wysuperfly uses a messenger tool called NKQQ to communicate with his friends on the net. The NKQQ accumulates 1 point score every hour automatically. When the score reach certain level, there will be a level-up. The relationship between the score and the level is listed as below:
Level 1 0 - 100
Level 2 101 - 500
Level 3 501 - 2000
Level 4 2001 - 10000
Level 5 10001 - 50000
Level 6 50001 - 200000
Level 7 200001 - infinity
Mr. Wysuperfly finds that other ACM teammates also use NKQQ everyday like him. He wants to know that after several hours, how many teammates reach a certain level. Can you help him?

输入

The first line is an integer N (N<=10^5) which represents the amount of the teammates to be considered. The second line includes N numbers which represent the scores of the teammates. The third line is a positive integer Q (Q<=10^5) which represents how many questions in the input data. In the following Q lines, each line which includes two integers D (0<=D<=10^9) and L (1<=L<=7) stands for a question: ‘After D hours, how many teammates will reach level L?

输出

For every question, output one line to show the answer to the question (one nonnegative integer).

样例输入

3
1 2 3
2
98 1
98 2

样例输出

2
1

题目大意:
给出人数N,每个人都有起始经验值,接着在抛出Q个问题,每个问题问过D小时后到达L级的人有多少

思路:
一开始想太多了用了线状数组去做,想着这样会快点,但却一直TLE,后来转念一想线状数组优点是更新快,如果数组内值不变,那其效率还没直接算高,失策失策啊!!!归根结底还是对树状数组不够了解。
因为1~6级经验值最大为2e5,而7级经验值为大于2e5,所以可以把所有超过2e5的值转化成2e5+1;
故建立一个大小为2e5+2的数组re,先统计出每个数有几个;然后计算出0~i(i<2e5+2)的区间和;
L级的上下限分别减去D后进行分类讨论(7级的上限始终是2e5+1)
若上限小于0则输出0;
若下限小于等于0则输出re[上限];
否则输出re[上限]-re[下限-1]
代码:

#include<bits/stdc++.h>
using namespace std;
int dj[7]={0,101,501,2001,10001,50001,200001},re[200002];
int main()
{
    int n,q,d,l,x;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        if(x>200001)x=200001;
        re[x]++;
    }
    re[0];
    for(int i=1;i<=200001;i++)re[i]=re[i-1]+re[i];
    cin>>q;
    for(int i=0;i<q;i++)
    {
        scanf("%d%d",&d,&l);
        if(l==7)
        {
            int head=200001-d,tail=200001;
            if(head<=0)printf("%d\n",re[tail]);
            else printf("%d\n",re[tail]-re[head-1]);
        }
        else
        {
            int head=dj[l-1]-d,tail=dj[l]-1-d;
            if(tail<0)printf("0\n");
            else if(head<=0)printf("%d\n",re[tail]);
            else printf("%d\n",re[tail]-re[head-1]);
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值