B. Barrels(思维+贪心) Educational Codeforces Round 96 (Rated for Div. 2)

原题链接: https://codeforces.com/contest/1430/problem/B

在这里插入图片描述
测试样例

input
2
4 1
5 5 5 5
3 2
0 0 0
output
10
0

题意: 给你 n n n个桶的水量,现在你可以进行 k k k次倒水操作,问经过这倒水操作之后,使得这最大水量与最小水量的差值最大。

解题思路: 一道简单的思维贪心问题。我们想想,如果我们进行了倒水操作,那么最小水量一定是 0 0 0 所以我们关键是在于这最大水量,我们肯定想让这最大,那么要将这水量大的加在一起。所以我们对其水量排序,以最后一个元素作为最大水量存储桶,从后往前遍历依次执行倒水操作即可。 OK,具体看代码。

AC代码

/*
*邮箱:[email protected]
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>	//POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 2e5+3;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

ll t;
ll n,k;
ll v[maxn];
int main(){
    
    
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	IOS;
	while(cin>>t){
    
    
		while(t--){
    
    
			cin>>n>>k;
			rep(i,0,n-1){
    
    
				cin>>v[i];
			}
			sort(v,v+n);
			for(int i=n-2;i>=0;i--){
    
    
				if(k==0)break;
				v[n-1]+=v[i];
				k--;
			}
			cout<<v[n-1]-0<<endl;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/hzf0701/article/details/109015480