Codeforces Round #527-B. Teams Forming(贪心)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
There are nn students in a university. The number of students is even. The ii-th student has programming skill equal to aiai.
The coach wants to form n2n2 teams. Each team should consist of exactly two students, and each student should belong to exactly one team. Two students can form a team only if their skills are equal (otherwise they cannot understand each other and cannot form a team).
Students can solve problems to increase their skill. One solved problem increases the skill by one.
The coach wants to know the minimum total number of problems students should solve to form exactly n2n2 teams (i.e. each pair of students should form a team). Your task is to find this number.
Input
The first line of the input contains one integer nn (2≤n≤100) — the number of students. It is guaranteed that nn is even.
The second line of the input contains nn integers a1,a2,…,an (1≤ai≤100), where ai is the skill of the ii-th student.
Output
Print one number — the minimum total number of problems students should solve to form exactly n2n2 teams.
Examples
input
Copy
6
5 10 2 3 14 5
output
Copy
5
input
Copy
2
1 100
output
Copy
99
Note
In the first example the optimal teams will be: (3,4)(3,4), (1,6)(1,6) and (2,5)(2,5), where numbers in brackets are indices of students. Then, to form the first team the third student should solve 11 problem, to form the second team nobody needs to solve problems and to form the third team the second student should solve 44 problems so the answer is 1+4=5.
In the second example the first student should solve 9999 problems to form a team with the second one.
题解:
这个题比第一题还水,求几个人需要做多少题才能一样,肯定就是排好序的从小到大,两个相邻的差值最小,这样最后的和才会最小
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int sum=0;
int a[105];
for(int t=0;t<n;t++)
{
scanf("%d",&a[t]);
}
sort(a,a+n);
for(int t=0;t<n;t+=2)
{
sum+=a[t+1]-a[t];
}
cout<<sum<<endl;
return 0;
}
Codeforces Round #527-B. Teams Forming(贪心)的更多相关文章
- Codeforces Round #527 (Div. 3) ABCDEF题解
Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入 ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- Codeforces Round #527 (Div. 3)
一场div3... 由于不计rating,所以打的比较浪,zhy直接开了个小号来掉分,于是他AK做出来了许多神仙题,但是在每一个程序里都是这么写的: 但是..sbzhy每题交了两次,第一遍都是对的,结 ...
- CodeForces Round #527 (Div3) B. Teams Forming
http://codeforces.com/contest/1092/problem/B There are nn students in a university. The number of st ...
- Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心
C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...
- Codeforces Round #547 (Div. 3) F 贪心 + 离散化
https://codeforces.com/contest/1141/problem/F2 题意 一个大小为n的数组a[],问最多有多少个不相交的区间和相等 题解 离散化用值来做,贪心选择较前的区间 ...
- Educational Codeforces Round 12 C. Simple Strings 贪心
C. Simple Strings 题目连接: http://www.codeforces.com/contest/665/problem/C Description zscoder loves si ...
- Codeforces Round #595 (Div. 3)D1D2 贪心 STL
一道用STL的贪心,正好可以用来学习使用STL库 题目大意:给出n条可以内含,相交,分离的线段,如果重叠条数超过k次则为坏点,n,k<2e5 所以我们贪心的想我们从左往右遍历,如果重合部分条数超 ...
- Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索
https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...
- Codeforces Round #303 (Div. 2) D 贪心
D. Queue time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
随机推荐
- Matlab之rand(), randn(), randi()函数的使用方法
1. rand()函数用于生成取值在(0~1)之间均匀分布的伪随机数.rand(n):生成n*n的0~1之间的满足均匀分布的伪随机矩阵:rand(m,n):生成m*n的伪随机数:rand(m,n,' ...
- overflow:hidden真的失效了吗?
项目中常常有同学遇到这样的问题,现象是给元素设置了overflow:hidden,但超出容器的部分并没有被隐藏,难道是设置的hidden失效了吗? 其实看似不合理的现象背后都会有其合理的解释. 我们知 ...
- jsp日期插件My97DatePicker 强大的日期控件 使用方便简单(转)
本文属转载(希望对编程爱好者有所帮助)详情请访问官方网站 http://www.my97.net/dp/index.asp 一. 简介 1. 简介 目前的版本是:4.7 2. 注意事项 My97Dat ...
- mysql数据库表分区详解(数量过大的数据库表通过分区提高查询速度)
这篇文章主要介绍了MySQL的表分区,例如什么是表分区.为什么要对表进行分区.表分区的4种类型详解等,需要的朋友可以参考下 一.什么是表分区通俗地讲表分区是将一大表,根据条件分割成若干个小表.mysq ...
- 树——平衡二叉树插入和查找的JAVA实现
package com.tomsnail.data.tree; /** * AVL二叉平衡树 * @author tomsnail * @date 2015年3月30日 下午4:35:50 */ pu ...
- POJ3660(foyld闭包问题)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8794 Accepted: 4948 Descr ...
- Lagom学习 五 Hello world工程
用Maven创建一个Hello world的Lagom工程: 1: 在想创建工程的目下下,打开CMD 2: mvn archetype:generate -Dfilter=com.lightbend ...
- Python 同ip网站查询(制作网站接口)
老师专门打电话回来说不让玩电脑~~ 呵呵. LOL把被人坑死了. 五排果然不适合我. 去翻了下曾经的Python项目. 在爬虫文件夹下面找到了这个. 我也就厚脸皮的什么都不改就放上来了: #co ...
- Python3中内置类型bytes和str用法及byte和string之间各种编码转换,python--列表,元组,字符串互相转换
Python3中内置类型bytes和str用法及byte和string之间各种编码转换 python--列表,元组,字符串互相转换 列表,元组和字符串python中有三个内建函数:,他们之间的互相转换 ...
- <正则吃饺子>:关于java中对内存部分的简单总结整理
在项目和一些群讨论中,经常看到对内存的处理,但是,自己确是一知半解的,基于此,就把这部分的知识简单的整理了下,知识点来源于网络博文,也一一标明出处,谢谢. package com.love.malin ...