Codeforces Round #623 (Div. 2, based on VK Cup 2019-2020 - Elimination Round, Engine) 比赛人数3742
[codeforces 1315D] Recommendations 大根堆
总目录详见https://blog.csdn.net/mrcrack/article/details/103564004
在线测评地址https://codeforces.com/contest/1315/problem/D
Problem | Lang | Verdict | Time | Memory |
---|---|---|---|---|
D - Recommendations | GNU C++11 | Accepted | 124 ms | 3000 KB |
//因 the number of publications of i-th category selected by the batch algorithm只能被增加,而不能被减小,故按数值自小到大排序,若数值相等,按耗时自小到大排序
算法如下
输入
3
1 1 1
1 2 3
输出
4
变化2 1 0
结果3 2 1
开始1 1 1
时间1 2 3
3*0+1*2+2*1=4
输入
4
1 1 1 2
1 2 3 4
输出
7
堆中数据
1 1 1
1 2 3
开始1 1 1 2
时间1 2 3 4
堆中数据
2 2 2
1 2 4
变化 1 1 0 0
中间过程 2 2 1 2
开始 1 1 1 2
时间 1 2 3 4
(1+2)*1=3
堆中数据
无
变化2 1 0 0
结果4 3 1 2
中间2 2 1 2
开始1 1 1 2
时间1 2 3 4
3+1*2+2*1=7
AC代码如下
#include <cstdio>
#include <queue>
#include <algorithm>
#define LL long long
using namespace std;
LL ans;
struct node{
int v,t;
}a[200010];
priority_queue<int> pq;//pq用来记录大根堆
int cmp(node a,node b){
return a.v==b.v?a.t<b.t:a.v<b.v;
}
int main(){
int n,i,j,mx,tmp;
LL sum=0;//sum用来记录大根堆中数据之和
scanf("%d",&n);
for(i=1;i<=n;i++)scanf("%d",&a[i].v);
for(i=1;i<=n;i++)scanf("%d",&a[i].t);
n++,a[n].v=2000000010,a[n].t=0;//添一个达不到的数据,便于之前数据的处理。
sort(a+1,a+1+n,cmp);
pq.push(a[1].t),sum=a[1].t;//sum用来记录,在堆中v值相同的时间和。
for(i=2;i<=n;i++){
tmp=a[i].v-a[i-1].v;//tmp功能多,0表示两个数据相同,>0
for(j=0;j<tmp&&!pq.empty();j++){
mx=pq.top();//大根堆
pq.pop(),sum-=mx;//用掉的数据,要从堆中减去
ans+=(LL)j*mx;//v相等数据中,自右向左更新。
}
ans+=(LL)sum*tmp;//v相等数据中,剩下的整体更新到与a[i].v相同
pq.push(a[i].t),sum+=a[i].t;//a[i]加入堆,a[i].v的值与堆中的值相等了
}
printf("%lld\n",ans);
return 0;
}