Ads Proposal
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 2427 Accepted Submission(s): 801
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.
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.
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
2 2 4 3 1 12 13 2 23 41 1 21 46 1 22 31 1 2 3 6 15 3 5 2677139 731358928 2 347112028 239095183 6 27407970 85994789 6 767687908 734935764 6 255454855 110193353 3 39860954 813158671 5 617524049 55413590 3 338773814 7907652 6 810348880 736644178 2 777664288 63811422 6 590330120 616490361 5 552407488 136492190 1 416295130 448298060 5 811513162 232437061 4 43273262 874901209 4 9 13
Sample Output
Case #1: 72 118 131 Case #2: 5801137622 5887132411 5887132411
Source
Recommend
lcy
题意:
每个客户有一些广告,每条广告有点击量和长度。问排每个客户排第K的广告的长度总和是多少。
思路:
用优先队列存下每个客户的广告,并且点击量多的赋予高的优先级。用一个数组来存下排在第K的广告的长度,然后求一下前缀和。
注意不要用memset来初始化sum数组,可以记录一下客户最多广告的个数cnt,把这个区间的sum都初始化,当询问到大于cnt的广告数,都按sum[cnt]来输出。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn=500005;
struct node
{
int id;
ll c,l;
friend bool operator <(node a,node b)
{
return a.c<b.c;
}
}q[maxn];
priority_queue<node> que[100005];
ll sum[maxn];
int main()
{
int t,n,m,qq,k,cas=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&qq);
int pos=0;
for(int i=1;i<=m;i++)
{
scanf("%d%lld%lld",&q[i].id,&q[i].c,&q[i].l);
que[q[i].id].push(q[i]);
int tmp=que[q[i].id].size();
pos=max(pos,tmp);
}
for(int i=0;i<=pos;i++)
sum[i]=0;
for(int i=1;i<=n;i++)
{
int pos=1;
while(que[i].size())
{
node tmp=que[i].top();
que[i].pop();
sum[pos]+=tmp.l;
pos++;
}
}
for(int i=1;i<=m;i++)
{
sum[i]=sum[i-1]+sum[i];
}
printf("Case #%d:\n",cas++);
for(int i=1;i<=qq;i++)
{
scanf("%d",&k);
if(k>pos)
printf("%lld\n",sum[pos]);
else
printf("%lld\n",sum[k]);
}
}
return 0;
}