[第三次训练]A simple problem
来源:互联网 发布:java如何编译成class 编辑:程序博客网 时间:2024/05/16 06:59
A simple problem
Description
Dreamone has a lovely cat. The cat can comfort you if you are very dejected, she can also play with you if
you are very bored. So Dreamone loves her very much. Of course, you bet, she can also make you trouble if
she wants to be naughty. This time, she comes up with a simple problem for fun, just like this:
She has 2n toys. In her heart, she has used a number to stand for each toy, and for example, the 2n toys are
given like this:
A1、A2、A3、….A2n-1、A2n
She wants to divide them into two groups, where each group contains n toys. So we can use B1、
B2...Bn ,which are all from A1、 A2...A2n ,to stand for Group One ,use C1、 C2….Cn ,which are all from A1、
A2...A2n, to stand for Group Two. The cat wants to know the minimum value S for the expression below:
S=|B1-C1|+|B2-C2|+|B3-C3|+...+|Bn-Cn|
As we know Dreamone is studying MaJiang recently, so he has no time to solve the simply problem. But he
knows the 6th program contest of SWUST is on, so he turns to you for help. Can you help him?
Input
The first line of input will be a positive integer C indicating how many data sets will be included. Each of
the C data sets will contain two parts:The first part contains a number n(1<=n<=100000),and the second
parts contains 2n numbers ,which are A1、 A2、 A2n-1、 A2n (0<=Ai<=1000(1<=i<=2n)),represented 2n toys
Output
For each case, output the minimum value S for answer.
Sample Input
2 1 1 3 2 1 1 1 2
Sample Output
2 1
HINT
解题报告
此题题意并不难理解,就是从A2n个玩具中分成两堆,分别是B1-Bn和C1-Cn,然后套用公式算出最小的S。此题只需要用sort排序一下,然后相邻两个依次相减,即为最小的S,代码如下:
#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>using namespace std;int a[200010];int main(){ int n,x,i,S; scanf("%d",&n); while(n--) { S=0; scanf("%d",&x); for(i=0;i<2*x;i++) scanf("%d",&a[i]); sort(a,a+2*x); for(i=0;i<2*x;i+=2) S+=fabs(a[i]-a[i+1]); printf("%d\n",S); } return 0;}
- [第三次训练]A simple problem
- 第三次训练——Problem A.Ant on a Chessboard
- [第三次训练]Hard problem
- 训练之线段树A Simple Problem with Integers
- A simple problem
- A simple problem
- HOJ A Simple Problem
- hdu A simple problem
- A simple problem
- HDU2522:A simple problem
- A Simple Math Problem
- A simple problem 2522
- A Simple Problem
- A simple problem
- A Simple Math Problem
- A Simple Math Problem
- CUSOJ1170--A Simple Problem
- A simple problem
- Cocos2d-x中颜色选择盘类CCControlColourPicker的使用
- Could not find the main class: org.apache.catalina.startup.Boostrap. Program will exit
- UVA-11865 - Stream My Contest(最小树形图)
- Android 创建与解析XML
- 详解java中的抽象类和接口的区别
- [第三次训练]A simple problem
- 字符设备驱动程序开发之基于中断的按键驱动加去抖动
- zoj 3638 经典容斥原理
- 关于iOS 5 Could not instantiate class named NSLayoutConstraint错误
- stl的find、find_if简单demo
- 不弄微博 只想要积分下载
- C++中双冒号(::)和单冒号(:)的区别和作用
- 一个关于农历的算法----js实现【转】
- java 线程