2017上海金马五校程序设计竞赛 O:An Easy Problem
来源:互联网 发布:excel相关系数矩阵 编辑:程序博客网 时间:2024/06/06 04:11
Description
Zhu Ge is a very clever boy. One day, he discovered 2*n numbers. He wanted to divide them into n groups, each group contains 2 integers, and minimize the sum of the absolute value of the difference of the numbers in each group.
The problem is too difficult to Zhu Ge, so he turned to you. He hopes you can calculate the minimum of the sum of absolute value of the difference among different division strategies.
Input
There are several test cases.
For each test case, there is an integer n (n < 10,000) at the first line. The second line contains 2*n integers. The input ends up with EOF.
Output
For each test case, output the minimum of sum.
Sample Input
310 3 4 5 1 6564 5 63 63 23 63 54 64 3 54
Sample Output
742
本题题意:
这题就是说给你 n 个数,让它们两两相减,将差值相加,求相加和最小为多少。
解题思路:
要使两两差值最小,将它们进行排序,相邻两个数相减的差值相加一定是最小的。
注意:
这道题看着简单,但是有一个大坑,让我WA了好多次 简直了。题上虽然说n 的数值最大是1000,但是你开数组是得用long long 才行。不说了,看代码。
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <string.h>#include <cmath>using namespace std;long long int a[20005];int main(){ int n; while(scanf("%d",&n)!=EOF) { long long int sum=0; for(int i=0; i<2*n; i++) { scanf("%lld",&a[i]); } sort(a,a+(2*n)); for(int i=0; i<2*n; i=i+2) { sum+=abs(a[i]-a[i+1]); } cout<<sum<<endl; } return 0;}
阅读全文
4 0
- 2017年上海金马五校程序设计竞赛 O : An Easy Problem 贪心
- 2017上海金马五校程序设计竞赛 O:An Easy Problem
- 2017上海金马五校程序设计竞赛 O:An Easy Problem
- 上海金马五校程序设计竞赛 Problem B : Sailing
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem B : Coach
- 2017年上海金马五校程序设计竞赛(网上资格赛) Problem J : Raising Bacteria
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem B : Coach
- 2017年上海金马五校程序设计竞赛(网上资格赛) Problem J : Raising Bacteria
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem H : DHU Club Festival
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem H : DHU Club Festival
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem J : Raising Bacteria
- 2017年上海金马五校程序设计竞赛:Problem A : STEED Cards
- 2017年上海金马五校程序设计竞赛:Problem B : Sailing
- 2017年上海金马五校程序设计竞赛:Problem C : Count the Number
- 2017年上海金马五校程序设计竞赛:Problem E : Find Palindrome
- 2017年上海金马五校程序设计竞赛:Problem G : One for You
- 2017年上海金马五校程序设计竞赛:Problem I : Frog's Jumping
- 2017年上海金马五校程序设计竞赛:Problem K : Treasure Map
- C++ string append方法的常用用法
- 设计模式-单例模式的Java代码体现Runtime类
- 初学Vue-cli
- 【计算机视觉】全景相机标定(MATLAB/opencv)
- 关于凸优化的一些简单概念
- 2017上海金马五校程序设计竞赛 O:An Easy Problem
- 面试题29数组中出现次数超过一半的数字
- 使用jUnit4测试Spring4
- 【HDU 1427 计算24点 DFS】
- 两数之和
- 数据结构与算法之线性表(一)(笔记)
- 了解CSS属性font-kerning,font-smoothing,font-variant
- java:控制台输入方法
- 湫湫买火车票 最短路