给定一个数组a,保证长度为偶数,每次可以任意选择两个数字,并取最小的那个,剩下的接着做如此的操作,最后将所有的最小的数字加起来,求这个和最大是多少。

可以发现,对于一个二元组来说答案只跟较小的那个元素有关,那么对于最大的那个元素取次大的可以使得次大的元素的值对答案产生贡献,如果选其它的元素的话,次大值也不会被加入贡献。

以此类推,可以得知将数组排序后相邻两项必定是组成一对的。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <vector>
 7 #include <queue>
 8 #include <cmath>
 9
10 using namespace std;
11
12 int n, a[305], ans;
13
14 int main(){
15     scanf("%d", &n);
16     n *= 2;
17     for(int i = 1 ; i <= n ; i++)scanf("%d", &a[i]);
18     sort(a + 1, a + 1 + n);
19     for(int i = 1 ; i <= n ; i += 2)ans += a[i];
20     printf("%d\n", ans);
21 }

View Code

转载于:https://www.cnblogs.com/KingSann/articles/7302433.html

AtCoder 001 A BBQ Easy相关推荐

  1. AGC001 补题小结

    Problem A BBQ Easy 简要题意:给定 \(2n\) 个数字,两两配对,一对的贡献为它们的 \(min\) ,求最大贡献和.\(n\le 100\) tag:小学生贪心 题解:排个序,求 ...

  2. AtCoder AGC001E BBQ Hard (DP、组合计数)

    题目链接: https://atcoder.jp/contests/agc001/tasks/agc001_e 题解: 求\(\sum^n_{i=1}\sum^n_{j=i+1} {A_i+A_j+B ...

  3. 20180716 [AtCoder]CF697 EASY+SoundHound HARD【值得琢磨实现细节】

    A - Romaji CodeForces - 1008A B - Turn the Rectangles CodeForces - 1008B C - Reorder the Array CodeF ...

  4. AtCoder Petrozavodsk Contest 001

    第一场apc,5H的持久战,我当然水几个题就睡了 A - Two Integers Time limit : 2sec / Memory limit : 256MB Score : 100 point ...

  5. NEC Programming Contest 2021(AtCoder Beginner Contest 229) B - Hard Calculation

    题目链接:B - Hard Calculation (atcoder.jp) Problem Statement You are given positive integers A and B. Le ...

  6. 我在atcoder打比赛

    我在atcoder打比赛 AtCoder Beginner Contest 177   比赛人数9636 AtCoder Beginner Contest 177 A Don't be late 化浮 ...

  7. Atcoder Beginner Contest 260D - Draw Your Cards 解题报告

    Atcoder Beginner Contest 260D - Draw Your Cards 解题报告 1 题目链接 abc260_d 2 题目大意 题目 : 抽牌 题目大意 : 给定NNN个数字, ...

  8. AtCoder Beginner Contest 192 A~D题解

    ABC192 A~D [A - Star](https://atcoder.jp/contests/abc192/tasks/abc192_a) 题目大意 输入格式 输出格式 样例 分析 代码 [B ...

  9. LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)

    LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...

最新文章

  1. jQuery弹出窗口浏览图片
  2. 【强化学习】DDPG
  3. java spring上传_SpringMVC上传文件的三种方式
  4. nginx给php做统一入口,Nginx如何来配置隐藏入口文件index.php(代码)
  5. 排序箭头,升序,降序简单实现
  6. 深入浅出聊聊 Rust WebAssembly(一)
  7. 使用 litmus 验证内存重排
  8. python删除字符串_Python3 - 删除字符串中不需要的字符
  9. 优雅的使用springboot集成任务调度
  10. xgboost算法原理_从XGB到SecureBoost:看联邦学习XGB的算法原理
  11. 编程解决素数环问题Java_回溯法解决素数环问题java实现
  12. 契税申报期限_纳税申报的5个小常识,会计不知道,真不适合干会计 D1
  13. 【51单片机】基于51单片机的时钟电子锁设计
  14. RPLIDAR A2 Windows 下开发
  15. Nacos6# Distro协议全量同步与校验
  16. 计算机圣诞节教案,2010我们的圣诞节(2010圣诞节教案)
  17. [NOI2015]荷马史诗
  18. 债券收益率建模(时间序列建模)时间序列相似度度量
  19. python SSLError HTTPSConnectionPool bad handshake
  20. MyBatis框架的使用及源码分析(八) MapperMethod

热门文章

  1. L3-012 水果忍者
  2. web程序设计(5)——个人主页(布局)
  3. 通过jsp向mysql批量导入数据_通过JSP+JavaBean对mysql进行添加数据的操作
  4. 设置暴风影音缓存文件夹位置的方法
  5. 【你好,windows】嵌入式win8.1 X86X64企业纯净版安装版2019.6.20
  6. 队列 如何 判断 已满
  7. P2918 [USACO08NOV]Buying Hay S(买干草)(ssl 2895)
  8. 自由枪骑兵修改服务器端口,自由枪骑兵存档修改器(全版本通用)
  9. Linux中的进程、线程和文件描述符
  10. 互联网大厂打工人的 16 个心酸瞬间:生活很现实,都是为了挣钱!