20130712 【南华大学 ACM】 新生赛第三场 【B. A simple problem】
2013-07-13 20:13
357 查看
Problem B: A simple problem
Time Limit: 1 Sec Memory Limit:128 MBDescription
Dreamone has a lovely cat. The cat can comfort you if you are very dejected, she can also play with you ifyou 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 ofthe 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
----------------------------------------------------------------
解题报告:
此题不难解:
用数组存储数据,排序,相邻两项之差(升序时后项-强项;降序时前项减后项)累加。
不过我把数据当成一维数组的序号存入,这样就少了排序这步。
我的解法:
输入的数有重复时:重复次数为偶数,把该数删去; 为奇数时,只保留一个数;
数组存储数据(数组初始化0,存储数据i时,则a[i]=1,这样可按顺序存储数据);
处理数组a的数据(保留为“1”项,删去为“0”项),存入数组aa中(数据单调存入);
然后相邻两项作差(后项-前项)。
代码如下:
----------------------------------------------------------------
代码:
#include<stdio.h> int main() { int N,n,i,j,x,sum,aa[1005]; scanf("%d",&N); while(N--) { int a[1005]={0}; scanf("%d",&n); n=2*n; for(i=0;i<n;i++) { scanf("%d",&x); a[x]=(a[x]+1)&1; } for(x=i=0;i<1000;i++) if( a[i] ) aa[x++]=i; for(sum=0,i=0,j=1; j<x ;i+=2,j+=2) sum+=aa[j]-aa[i]; printf("%d\n",sum); } return 0; }
相关文章推荐
- 20130712 【南华大学 ACM】 新生赛第三场 【F. Help Johnny】
- 南华大学暑假个人练习第三场 B. A simple problem
- 20130712 【南华大学 ACM】 新生赛第四场 【E. 机器人II】
- 20130709 【南华大学 ACM】 新生赛第二场 【D: The mell hall】
- ACM学习历程——POJ3468 A Simple Problem with Integers(线段树)
- “玲珑杯”ACM比赛 Round #19 A.A simple math problem【打表找规律】
- HDU 3468 HDOJ 3468 A Simple Problem with Integers ACM 3468 IN HDU
- 20130707 【南华大学 ACM】 新生赛第一场 【A.Eming】
- 20130707 【南华大学 ACM】 新生赛第一场 【D.Brackets】
- 20130707 【南华大学 ACM】 新生赛第一场 【E.Function】
- 2016ACM/ICPC亚洲区大连站 D - A Simple Math Problem(尚未通过)
- ACM(935)A Simple Problem(1)
- ACM刷题之POJ————A Simple Problem with Integers
- ACM-ICPC Asia Regional Contest HDU 5974 A Simple Math Problem(数论)
- 2016大连ACM D.ASimple Math Problem
- [ACM] poj 3468 A Simple Problem with Integers(线段树,成段更新,懒惰标记)
- ACM学习历程—SNNUOJ 1116 A Simple Problem(递推 && 逆元 && 组合数学 && 快速幂)(2015陕西省大学生程序设计竞赛K题)
- [ACM] poj 3468 A Simple Problem with Integers(线段树,成段更新,懒惰标记)
- 20130709 【南华大学 ACM】 新生赛第二场 【F. Little Sheep and a paper】
- [ACM] poj 3468 A Simple Problem with Integers(线段树,成段更新,懒惰标记)