HDU 4020 Ads Proposal(很巧妙的哈希)

来源:互联网 发布:上海黑桃互动 知乎 编辑:程序博客网 时间:2024/05/24 07:26

题目的意思是:一共有n个人,一共在百度上发布m个广告。每个广告的时间长度为lenth。有q次询问让你求出每次询问下每个人点击量排前k个的时间长度的和。(这里要注意有可能这个人发的广告的次数少于k个。这样和就是他发布的所有的时间和)。

解题的思路是:先对点击数量进行排序同时记录原先的下标。以点击量的数量数组排序后的下标进行hash。然后用vis标记数组记录每个ower出现的次数(vis中每个ower的次数依次增加,排序后会保证点击数量的顺序从大到小,所以每次的ower对应的个数的前vis个都会是前边最大的点击数)。用ans数组保存当每种ower出现为1,2,3,……时候的和。然后用另一个数组预处理出所有的情况,然后直接输出前k个的和,就OK了啊。

Ads Proposal

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1849    Accepted Submission(s): 625


Problem Description
There are N customers set their M different advertisements on Baidu. Each advertisement is owned by single customer. The ads system of Baidu records the number of clicks of each advertisement this year. The proposal system wants to analysis the advertisements about the relativity of the description length and the number of clicks of the advertisements. During the analysis, it is very important to do such a query to ask the total length of the advertisements with top k clicking times for each customer. You may assume the number of clicks of all advertisements are distinct.
Your task is to help Baidu to design this little toolkit.
 

Input
The input consist multiple test cases. The number of test cases is given in the first line of the input.
  For each test case, the first line contains three integers N, M and Q, denoting the number customer, the number of advertisement instances and the number of queries. (N <= 100000, M <= 500000, Q <= 100000)
  Then M lines follow, each line contains three numbers, U, C and L, indicating the owner of this advertisement, the clicks for this advertisement and the length. (1 <= U <= N, 0 <= C, L <= 1000000000)
  Finally Q lines come. Each line contains only one integer k, representing the query for top k clicking advertisements for each customer.
 

Output
For each test case, output Q lines, each line contains only one integer, denoting the sum of total length of the top k number of clicks for each customer.
 

Sample Input
22 4 31 12 132 23 411 21 461 22 311236 15 35 2677139 7313589282 347112028 2390951836 27407970 859947896 767687908 7349357646 255454855 1101933533 39860954 8131586715 617524049 554135903 338773814 79076526 810348880 7366441782 777664288 638114226 590330120 6164903615 552407488 1364921901 416295130 4482980605 811513162 2324370614 43273262 8749012094913
 

Sample Output
Case #1:72118131Case #2:580113762258871324115887132411
 

#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-8#define M 1000100#define LL __int64//#define LL long long#define INF 0x3f3f3f3f#define PI 3.1415926535898using namespace std;const int maxn = 500010;int vis[maxn];int p[maxn];double ans[maxn];double dp[maxn];LL ower[maxn];LL lenth[maxn];struct node{    int flag;    int click;} f[maxn];int cmp(node a, node b){    return a.click > b.click;}int main(){    int T;    int Case = 1;    cin >>T;    while(T--)    {        int n, m, q;        LL u, c, l;        scanf("%d %d %d",&n, &m, &q);        for(int i = 0; i < m; i++)        {            scanf("%lld %lld %lld",&ower[i], &f[i].click, &lenth[i]);            f[i].flag = i;        }        sort(f, f+m, cmp);        for(int i = 0; i <= n; i++)            vis[i] = 0;        for(int i = 0; i <= m; i++)            ans[i] = 0;        for(int i = 0; i < m; i++)        {            vis[ower[f[i].flag]]++;            ans[vis[ower[f[i].flag]]] += lenth[f[i].flag];        }        dp[0] = 0;        for(int i = 1; i <= m; i++)            dp[i] = dp[i-1]+ans[i];        int k;        printf("Case #%d:\n",Case++);        for(int i = 1; i <= q; i++)        {            scanf("%d",&k);            if(k > m)                printf("%.0lf\n",dp[m]);            else                printf("%.0lf\n",dp[k]);        }    }    return 0;}


0 0