A simple problem解题报告
2013-07-12 21:38
169 查看
题目摘要:Dreamone has a lovely cat. The catcan comfort you if you are very dejected, she can also play with you if
you are very bored. So Dreamone loves hervery much. Of course, you bet, she can also make you trouble if
she wants to be naughty. This time, shecomes up with a simple problem for fun, just like this:
She has 2n toys. In her heart, she has useda 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 ,whichare all from A1、
A2...A2n, to stand for Group Two. The catwants 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 MaJiangrecently, so he has no time to solve the simply problem. But he
knows the 6th program contest of SWUST ison, so he turns to you for help. Can you help him?
题目大意:输入一个整数N,给一组数,长度为2N,用A数组储存。将A里的数平分成2组,每组N个数,分别存在B和C数组里,计算S=|B1-C1|+|B2-C2|+|B3-C3|+...+|Bn-Cn|
的最小值
输入输出要求:
Input
The first line of input will be a positiveinteger C indicating how many data sets will be included. Each of
the C data sets will contain two parts:The first part contains a numbern(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 Sfor answer.
输入输出样例:
Sample Input
2
1
1 3
2
1 1 1 2
Sample Output
2
1
解题思路:将A数组元素排序,直接用sort函数即可,然后一个for循环解决问题。
代码:
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
const int maxn=200000;
int A[maxn]={0};
int main()
{
intC;
cin>>C;
while(C--)
{
intN;
intS=0;
cin>>N;
memset(A,0,sizeof(A));
for(inti=0;i<2*N;i++)
cin>>A[i];
sort(A,A+2*N);
for(intj=0;j<2*N;j=j+2)
{
S=S+(A[j+1]-A[j]);
}
cout<<S<<endl;
}
return0;
}
解题感想:如题所说,a simple problem!
you are very bored. So Dreamone loves hervery much. Of course, you bet, she can also make you trouble if
she wants to be naughty. This time, shecomes up with a simple problem for fun, just like this:
She has 2n toys. In her heart, she has useda 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 ,whichare all from A1、
A2...A2n, to stand for Group Two. The catwants 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 MaJiangrecently, so he has no time to solve the simply problem. But he
knows the 6th program contest of SWUST ison, so he turns to you for help. Can you help him?
题目大意:输入一个整数N,给一组数,长度为2N,用A数组储存。将A里的数平分成2组,每组N个数,分别存在B和C数组里,计算S=|B1-C1|+|B2-C2|+|B3-C3|+...+|Bn-Cn|
的最小值
输入输出要求:
Input
The first line of input will be a positiveinteger C indicating how many data sets will be included. Each of
the C data sets will contain two parts:The first part contains a numbern(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 Sfor answer.
输入输出样例:
Sample Input
2
1
1 3
2
1 1 1 2
Sample Output
2
1
解题思路:将A数组元素排序,直接用sort函数即可,然后一个for循环解决问题。
代码:
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
const int maxn=200000;
int A[maxn]={0};
int main()
{
intC;
cin>>C;
while(C--)
{
intN;
intS=0;
cin>>N;
memset(A,0,sizeof(A));
for(inti=0;i<2*N;i++)
cin>>A[i];
sort(A,A+2*N);
for(intj=0;j<2*N;j=j+2)
{
S=S+(A[j+1]-A[j]);
}
cout<<S<<endl;
}
return0;
}
解题感想:如题所说,a simple problem!
相关文章推荐
- poj 3468 A Simple Problem with Integers 解题报告
- [bzoj3489] A simple rmq problem 解题报告
- POJ 3468 A Simple Problem with Integers (线段树成段更新) 解题报告
- poj3468- A Simple Problem with Integers-解题报告-线段树
- POJ 3468 A Simple Problem with Integers 解题报告
- poj 3468 A Simple Problem with Integers 解题报告 线段树 数状数组两种实现
- UVALive 5861 Hidden Terminal Problem 解题报告
- 第四届吉林省大学生程序设计大赛个人解题报告 Problem H: New Feature in Windows 7
- HDU1016 Prime Ring Problem 解题报告--dfs
- LeetCode-The Skyline Problem-解题报告
- poj 1207 The 3n + 1 problem 解题报告
- 杭电OJ题 1002 A + B Problem II 解题报告
- hdu 1001 Sum Problem 解题报告 数据范围
- 2013年4月_武科大程序设计大赛(复赛)_解题报告(problem 1005)
- HDU 4716 A Computer Graphics Problem 解题报告
- 杭电 HOJ 1016 Prime Ring Problem 解题报告
- I - An Easy Problem!解题报告(来自网络)
- A-B Problem(Dec)解题报告 - 单调队列
- 2013年4月_武科大程序设计大赛(复赛)_解题报告(problem 1006)
- Croc Champ 2013 - Round 2 (Div. 2 Edition) E. Cube Problem 解题报告