您的位置:首页 > 其它

ICPC 6828 Help cupid(Regionals 2014 >> Latin America)

2015-09-03 16:39 288 查看
Cupid’s job is getting harder, so he is adopting new technologies to help him with his difficult task of matching people into happy couples. He appointed the best programmers in his staff to a new project called Advanced Couples Matching (ACM). For this project,
the programmers need to produce an algorithm that takes a set of an even number of N lonely persons and matches them into N/2 couples, such that each person is in exactly one couple. Sadly, the data available about each person is limited. In this modern world,
using gender, ethnicity, age or nationality as criteria to form couples is not a sensible option, so the programmers can only use data about the internet connection of each candidate. They decided to focus this stage on time zones. People living in closer
time zones are more likely to find time to interact with each other. Thus, the programmers decided to create couples so as to minimize the total time difference. Each time zone is identified by an integer between -11 and 12, inclusive, representing its difference
in hours from a particular time zone called Coordinated Universal Time (or UTC). The time difference of two people living in time zones represented by integers i and j is the
minimum between |i−j| and 24−|i−j|. Given a partition of a set of an even number N of candidates into N/2 couples, its total time difference is the sum of the time difference of each couple. You are asked to write
a program that receives as input the time zones of a set of N candidates. The output of the program must be
the minimum total time difference among all possible partitions of the set into couples.

Input

The input contains several test cases; each test case is formatted as follows. The first line contains an even integer N (2 ≤ N ≤ 1000) representing the number of candidates to be coupled. The second line contains N integers T1, T2, ..., TN (−11 ≤ Ti ≤ 12 for
i = 1,2,...,N) indicating the time zones of the candidates.

Output

For each test case in the input, output a line with an integer representing the minimum total time difference among all possible partitions of the set of candidates into couples.

Sample Input

6

-3 -10 -5 11 4 4

2

-6 6

8

0 0 0 0 0 0 0 0

Sample Output

5

12

0

给定一组数字。你让他们两两配对,其中,配对的两个数字可以产生一个值。minimum between |i−j| and 24−|i−j|.

求所有数字配对完之后,产生的最小的总值。

思路:如果数字1有三个,那么有两个就可以自己配对,值最小。所以按照这样子先处理第一步,剩下的数都是落单的。

接下来处理落单的那些数字。可以得知,某数字和附近的值配对最优→_→。遍历一下两两配对的起点,然后后面的数字都是两两配对的。这里需要用到循环数组

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int n;
	while(cin >> n)
	{
		int an[30],num[30]={0},x;
		for(int i=0;i<n;i++)
		{
			cin >> x;
			num[x+11]++;
		}
		int k = 0;
		for(int i=0;i<=23;i++)
		{
			num[i] = num[i]%2;
			if(num[i]==1)an[k++] = i-11;
		}
		int ans = 10000000;
		for(int i=0;i<k;i++)//两两配对的起点 
		{
			int tmp = 0;
			for(int j=i,p=0;p<k;j+=2,p+=2)//p计数 
			{
				int y = abs(an[j%k]-an[(j+1)%k]); 
				tmp += min(y,24-y);
			}
			ans = min(ans,tmp);
		}
		if(ans==10000000)cout<<0<<endl;
		else
		cout<<ans<<endl; 
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: