[NERC2018] Lazyland
进了最优解第一,不得交发题解庆祝一下。
Description
现有
Solution
显然若职业
若题目给定的
时间复杂度 排序的 log 显然躲不掉。
Code
排序的方法有很多种,既然其他题解都是直接 sort(),那我就来一发优先队列 (带点小常数)。
#include <iostream>
#include <queue>
using namespace std;
const int N = 100005;
int n, k, a[N], b, maxx[N];
long long ans;
priority_queue <int, vector <int>, greater <int>> q;
int read(){
int x = 0;
char a = getchar();
while(a < '0' || '9' < a) a = getchar();
while('0' <= a && a <= '9') x = (x << 1) + (x << 3) + (a ^ 48), a = getchar();
return x;
}
void write(long long x){
if(x > 9) write(x / 10);
putchar(x % 10 | 48);
}
int main(){
n = read(), k = read();
for(int i = 1; i <= n; ++ i) a[i] = read();
for(int i = 1; i <= n; ++ i){
b = read();
if(!maxx[a[i]]) maxx[a[i]] = b, k --;
else if(maxx[a[i]] < b) q.push(maxx[a[i]]), maxx[a[i]] = b;
else q.push(b);
}
while(k --) ans += q.top(), q.pop();
write(ans);
return 0;
}
Similar
CF1089L Lazyland