Barrels (codeforces 1430B )(拆分思想和模拟控制)

简介: Barrels (codeforces 1430B )(拆分思想和模拟控制)

You have n barrels lined up in a row, numbered from left to right from one. Initially, the ii -th barrel contains a_iai liters of water.


You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels xx and yy (the xx -th barrel shouldn't be empty) and pour any possible amount of water from barrel xx to barrel yy (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them.


Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most kk times.


Some examples:


  • if you have four barrels, each containing 5 liters of water, and k=1 , you may pour 55 liters from the second barrel into the fourth, so the amounts of water in the barrels are [5, 0, 5, 10], and the difference between the maximum and the minimum is 10 ;
  • if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still 0 .


输入格式



The first line contains one integer t( 1≤t≤1000 ) — the number of test cases.


The first line of each test case contains two integers nn and kk ( 1 <=k < n<=2⋅10^5 ) — the number of barrels and the number of pourings you can make.


The second line contains nn integers a1,a2,…,an ( 0≤ai≤109 ), where a_iai is the initial amount of water the ii -th barrel has.


It's guaranteed that the total sum of nn over test cases doesn't exceed 2⋅10^5 .


输出格式



For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most k times.


题意翻译



T次询问,对于每一次询问:


有 n 个水桶,第 i 个水桶有  ai 单位的水,有 k  次机会把一个桶里的水倒在另一个桶里(任意单位,可以不执行完),询问最后最大水桶水量和最小水桶水量的差的最大值。


输入输出样例



输入 #1复制


1. 2
2. 4 1
3. 5 5 5 5
4. 3 2
5. 0 0 0


输出 #1复制


 10
 0


额,水题一道,直接上代码;写了2种,喜欢那一个就看那一个都可ac。

下面是ac代码,关建点都有注释。

f58230e9f063709cf3167704f4efdf14.gif


有事你就q我;QQ2917366383


学习算法

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 7;
int a[maxn];
int main() {
    int T;
  scanf("%d",&T);
    while(T--) {
        int n,k;
    scanf("%d%d",&n,&k);
        for(int i = 1;i <= n;i++) {//几个桶 
            scanf("%d",&a[i]);
        }
        sort(a + 1,a + 1 + n);//默认小到大 
        ll num = a[n];//最后一个倒入 
        for(int i = n - 1;i >= 1;i--) {//由大到小倒 
            if(k) //!=0
      {
                num += a[i]; 
                k--;//倒几次水 
            }
        }
        printf("%lld\n",num);       
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef  long long ll;
const ll maxn=1e6+8;
ll p[maxn],sum;
int main()
{
  int t;
  cin>>t;
  while(t--)
  {
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    cin>>p[i];
    sort(p+1,p+1+n);
    sum=p[n];
    for(int i=n-1;i>=0;i--)
    {
      if(m)
      {
        sum+=p[i];
        m--;
      }
    }
    cout<<sum<<endl;
  }
 } 
相关文章
|
21天前
7-7 念数字 (15 分)(用数组简化判断过程)
7-7 念数字 (15 分)(用数组简化判断过程)
23 0
|
21天前
【每日一题Day257】LC2178拆分成最多数目的正偶数之和 | 贪心
【每日一题Day257】LC2178拆分成最多数目的正偶数之和 | 贪心
22 2
|
21天前
|
监控
画图解释FHSS、DSSS扩频原理以及计算规则
画图解释FHSS、DSSS扩频原理以及计算规则
100 0
|
6月前
|
算法 测试技术 C#
C++前缀和算法的应用:石头游戏 VIII 原理源码测试用例
C++前缀和算法的应用:石头游戏 VIII 原理源码测试用例
|
Python
用Python语言对任意图像进行m*n的均匀分块(思路非常清晰,步骤简单)
用Python语言对任意图像进行m*n的均匀分块(思路非常清晰,步骤简单)
150 0
|
存储 算法
算法设计与分析/数据结构与算法实验5:找新数最小的删除方案
算法设计与分析/数据结构与算法实验5:找新数最小的删除方案
103 0
算法设计与分析/数据结构与算法实验5:找新数最小的删除方案
|
数据安全/隐私保护
【数字IC手撕代码】Verilog伪随机数生成器|线性反馈移位寄存器|题目|原理|设计|仿真
【数字IC手撕代码】Verilog伪随机数生成器|线性反馈移位寄存器|题目|原理|设计|仿真
【数字IC手撕代码】Verilog伪随机数生成器|线性反馈移位寄存器|题目|原理|设计|仿真
【CCCC】L2-025 分而治之 (25分),图的度数,使节点独立的方案
【CCCC】L2-025 分而治之 (25分),图的度数,使节点独立的方案
74 0
|
Scala 开发者
集合化简的流程示意图 | 学习笔记
快速学习集合化简的流程示意图
198 0
集合化简的流程示意图 | 学习笔记

热门文章

最新文章