HDU4020--Ads Proposal

来源:互联网 发布:java导出pdf文件 编辑:程序博客网 时间:2024/05/14 07:24
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

 

/*首先对这些海报按照编号进行排序。然后按照点击次数进行排序我这里的ans数组是用来存第i名点击的所有的总和sum就是前i名所有点击的总和*/#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;long long int ans[510008];long long int sum[510008];struct HB{int owner;long long int click,len;}hb[510008];bool cmp(HB a,HB b){if(a.owner>b.owner)return 0;if(a.owner<b.owner)return 1;if(a.owner==b.owner){return a.click>b.click;}}inline int max(int a,int b){return a>b?a:b;}int main(){int t;scanf("%d",&t);for(int i=1;i<=t;i++){int n,m,q;//N个人,M张海报,Q个询问scanf("%d%d%d",&n,&m,&q);for(int j=1;j<=m;j++){scanf("%d%I64d%I64d",&hb[j].owner,&hb[j].click,&hb[j].len);}sort(hb+1,hb+m+1,cmp);//快排。mlogmfor(int j=0;j<=m;j++)ans[j]=0;ans[1]+=hb[1].len;int k=1;int maxk=-1;for(int j=2;j<=m;j++)//m次循环,m上限50W{if(hb[j].owner!=hb[j-1].owner){k=0;}k++;ans[k]+=hb[j].len;maxk=max(maxk,k);}for(int j=0;j<=maxk;j++)sum[j]=0;for(int j=1;j<=maxk;j++)//MAXK上限也是50W{sum[j]=sum[j-1]+ans[j];}printf("Case #%d:\n",i);for(int j=1;j<=q;j++){scanf("%d",&k);if(k<=maxk)printf("%I64d\n",sum[k]);else printf("%I64d\n",sum[maxk]);}}return 0;}


 

原创粉丝点击