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;
  }
 } 
相关文章
|
Go Windows
windows安装scoop/annie下载B站等视频
windows安装scoop/annie下载B站等视频
385 0
|
3月前
|
敏捷开发 人工智能 算法
AI 时代,这 3 个智能工具正在改变项目管理方式
在 AI 时代,项目管理面临高频变更、跨团队协作等新挑战。传统工具难以应对动态性与复杂性,智能工具则通过 AI 决策、自动化协作与智能排期,减少重复劳动,提升效率。本文解析智能工具如何破解管理痛点,并提供选型建议,助力团队从“被动救火”转向“主动规划”。
AI 时代,这 3 个智能工具正在改变项目管理方式
|
4月前
|
JavaScript 开发工具 虚拟化
配置DevEco Studio的开发环境时,需要注意什么?
配置DevEco Studio的开发环境时,需要注意什么?
|
人工智能 自然语言处理 Serverless
阿里云函数计算 x NVIDIA 加速企业 AI 应用落地
阿里云函数计算与 NVIDIA TensorRT/TensorRT-LLM 展开合作,通过结合阿里云的无缝计算体验和 NVIDIA 的高性能推理库,开发者能够以更低的成本、更高的效率完成复杂的 AI 任务,加速技术落地和应用创新。
792 14
|
存储 IDE 开发工具
|
域名解析 网络协议 安全
【域名解析DNS专栏】进阶DNS管理:利用DNSSEC加强域名安全
【5月更文挑战第23天】DNSSEC使用公钥加密为DNS记录添加数字签名,防止DNS欺骗和中间人攻击。它涉及密钥对生成、记录签名、公钥发布和验证过程。部署DNSSEC需要选择支持的DNS提供商,管理密钥并配置签名区域。尽管面临复杂性、性能影响等挑战,DNSSEC的普及和与TLS、HTTPS结合将提升DNS安全性,构建更可信的互联网环境。通过实践DNSSEC,我们可以强化域名安全防线。
644 1
|
Java Maven
Maven 依赖管理
Maven 一个核心的特性就是依赖管理。当我们处理多模块的项目(包含成百上千个模块或者子项目),模块间的依赖关系就变得非常复杂,管理也变得很困难。针对此种情形,Maven 提供了一种高度控制的方法。
418 5
|
10月前
|
人工智能 算法 API
构建基于 Elasticsearch 的企业级 AI 搜索应用
本文介绍了基于Elasticsearch构建企业级AI搜索应用的方案,重点讲解了RAG(检索增强生成)架构的实现。通过阿里云上的Elasticsearch AI搜索平台,简化了知识库文档抽取、文本切片等复杂流程,并结合稠密和稀疏向量的混合搜索技术,提升了召回和排序的准确性。此外,还探讨了Elastic的向量数据库优化措施及推理API的应用,展示了如何在云端高效实现精准的搜索与推理服务。未来将拓展至多模态数据和知识图谱,进一步提升RAG效果。
393 1
|
11月前
|
机器学习/深度学习 存储 缓存
ATB概念之:算子tiling
算子 tiling 是一种优化技术,用于提高大规模张量运算的计算效率。它通过将大任务分解为小块,优化内存使用、支持并行计算,并防止内存溢出。在ATB中,tiling data指kernel的分片参数,用于指导计算。ATB提供了三种 tiling data 搬移策略:整体搬移、多stream搬移及随kernel下发搬移,旨在优化内存拷贝任务,提高计算效率。
|
机器学习/深度学习 计算机视觉
目标检测笔记(六):如何结合特定区域进行目标检测(基于OpenCV的人脸检测实例)
本文介绍了如何使用OpenCV进行特定区域的目标检测,包括人脸检测实例,展示了两种实现方法和相应的代码。
371 1
目标检测笔记(六):如何结合特定区域进行目标检测(基于OpenCV的人脸检测实例)