应该是贪心+模拟吧
因该是比较好想的吧。
思路:
-
先把原宝石数组 按 (颜色) 把数组 按 排序
(后面有用)。 -
因为题目要求至少要有 种颜色,所以接下来模拟一遍把每种颜色最大值存到 里。(一定要存原下标!因为后面要看看有没有用到重复的)。
-
把 排序,选前 个最大的。
-
接下来就是选剩下 最大的,还要看看和之前有没有重复。
复杂度分析:
时间复杂度:
空间复杂度:
注意:
存下标时一定要存原来的(我被坑的好惨。
代码如下
Code
#include <bits/stdc++.h>
#define ll long long
#define IOS cin.tie(0)->ios::sync_with_stdio(0);
using namespace std;
const int N=2e5+5;
struct Node {
int c,v,idx;
void input(int i) {
cin >> c >> v;
idx=i;
}
bool operator < (const Node& o) const {
return c<o.c;
}
} a[N];
struct Node2 {
int v,idx;
bool operator < (const Node2& o) const {
return v>o.v;
}
} b[N],c[N];
int n,k,m,t=1,q,color,ma;
bool vis[N];
ll ans;
signed main() {
IOS
cin >> n >> k >> m;
for(int i=1;i<=n;++i) a[i].input(i),c[i].v=a[i].v,c[i].idx=i;
sort(a+1,a+n+1);
color=a[1].c;
ma=a[1].v;
b[t]={ma,a[1].idx};
for(int i=2;i<=n;++i) {
if(color==a[i].c) {
if(ma<a[i].v) {
ma=a[i].v;
b[t]={ma,a[i].idx};
}
}
else {
color=a[i].c;
ma=a[i].v;
b[++t]={ma,a[i].idx};
}
}
sort(b+1,b+t+1);
for(int i=1;i<=m;++i) ans+=b[i].v,vis[b[i].idx]=1;
k-=m;
sort(c+1,c+n+1);
for(int i=1;i<=n;++i) {
if(vis[c[i].idx]) continue;
if(++q>k) break;
ans+=c[i].v;
}
cout << ans;
return 0;
}
/*
4 3 2
2 20
1 40
1 10
1 30
*/