Codeforces Round 760 (Div. 3) D. Array and Operations(贪心)
https://codeforces.com/contest/1618/problem/D
题目大意:
给定一个长度为n的数组a,我们可以进行m次操作:
每次操作可以任意选择两个不同的下标的数字x和y,并把它两删除,替换成x/y(但是x/y不可以再被选择进行除数运算了)。
问我们这样剩下来的数列的最小值是多少?
input
5
7 3
1 1 1 2 1 3 1
5 1
5 5 5 5 5
4 2
1 3 3 7
2 0
4 2
9 2
1 10 10 1 10 2 7 10 3
output
2
16
0
6
16
详解见代码内部注释
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18,MINN=-MAXN;
const LL N=2e6+10,M=4004;
const LL mod=100000007;
const double PI=3.1415926535;
#define endl '\n'
LL a[N];
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
int T=1;
cin>>T;
while(T--)
{
LL n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+1+n);
LL res=0;
//前面的数字从贪心的角度来说一定都是最小的,如果操作了是一定会亏损的
for(int i=1;i<=n-2*m;i++)
{
res+=a[i];
}
map<int,int> mp;
for(int i=n-2*m+1;i<=n;i++)
{
mp[a[i]]++;
}
int maxn=0;
for(auto i:mp)
{
maxn=max(maxn,i.second);//找到相同数字的最大数量
//其它的数量可以彼此互相抵消
}
if(maxn>m) res+=maxn-m;//如果数量会超过m个,说明这m个可以全部抵消,其它的数量相除==1
cout<<res<<endl;
}
return 0;
}
本文作者:Vivian-0918
本文链接:https://www.cnblogs.com/Vivian-0918/p/17247844.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
分类:
贪心
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步