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;
  }
 } 
相关文章
|
JSON 前端开发 API
fetchEventSource源码解析
fetchEventSource源码解析
3823 1
|
9月前
|
SQL 关系型数据库 MySQL
击穿 MySQL 事务隔离级别:底层实现原理 + 生产级架构选型避坑指南
本文深度解析MySQL事务隔离级别,从SQL标准定义出发,结合InnoDB底层的MVCC、undo log、Read View与锁机制,详解脏读、不可重复读、幻读三大异常及4种隔离级别的实现差异,辅以可复现SQL示例与Spring Boot实战代码,提供生产环境选型指南与避坑方案。
564 1
|
机器学习/深度学习 自然语言处理 测试技术
模型上新!来通义灵码体验 QwQ-32B 推理模型!
今天,阿里云发布并开源全新的推理模型通义千问QwQ-32B。通过大规模强化学习,千问QwQ-32B在数学、代码及通用能力上实现质的飞跃,整体性能比肩DeepSeek-R1。在保持强劲性能的同时,千问QwQ-32B还大幅降低了部署使用成本,在消费级显卡上也能实现本地部署。
3402 58
Three添加天空盒子
这篇文章详细说明了如何在Three.js中添加天空盒(Skybox)以增强3D场景的真实感和沉浸体验。
949 6
Three添加天空盒子
|
机器学习/深度学习 人工智能 运维
智能化运维:从传统到AIOps的转型之路####
本文探讨了智能化运维(AIOps)的兴起背景、核心价值及其对现代IT运维模式的深刻影响。通过分析传统运维面临的挑战,阐述了AIOps如何利用大数据、机器学习技术实现故障预测、自动化处理与决策支持,进而提升运维效率和服务质量。文章还概述了实施AIOps的关键步骤与面临的主要挑战,为组织向智能化运维转型提供参考路径。 ####
|
人工智能 缓存 安全
什么是AI网关?AI网关在企业系统中承担什么角色?
AI大模型的快速发展正推动各行业增长,预计未来十年年均增长率达37.3%,2027年前全球企业在AI领域的投资将达8000亿美元。这促使企业进行战略转型,调整AI应用构建与保护方式。为应对AI创新需求,AI网关概念应运而生,它帮助企业随时随地控制和管理应用流量,提供更高的安全性。AI网关不仅支持多AI模型集成,还提供统一端点、应用程序配置与部署、安全与访问管理等核心功能。面对未来挑战,AI网关需支持模型故障转移、语义缓存等功能,确保AI应用的可靠性和效率。开源项目APIPark.COM为企业提供了一站式AI网关解决方案,简化大型语言模型的调用过程,保障企业数据安全。
1192 1
|
编解码 Linux API
【Camera基础(一)】Camera摄像头工作原理及整机架构
【Camera基础(一)】Camera摄像头工作原理及整机架构
|
Ubuntu Linux Docker
Docker容器运行时权限和Linux系统功能
Docker容器运行时权限和Linux系统功能 相关Docker参数 --cap-add: Add Linux capabilities --cap-drop: Drop Linux capabilities --privileged=false: Give extended privileges...
11620 0
|
搜索推荐 算法 UED
基于Python的推荐系统算法实现与评估
本文介绍了推荐系统的基本概念和主流算法,包括基于内容的推荐、协同过滤以及混合推荐。通过Python代码示例展示了如何实现基于内容的推荐和简化版用户-用户协同过滤,并讨论了推荐系统性能评估指标,如预测精度和覆盖率。文章强调推荐系统设计的迭代优化过程,指出实际应用中需考虑数据稀疏性、冷启动等问题。【6月更文挑战第11天】
2870 3
|
小程序 程序员 开发工具
微信web开发者工具无法打开的六种解决方法
微信web开发者工具无法打开的六种解决方法
3068 0