当前位置: 首页 > 工具软件 > Cupid > 使用案例 >

Help cupid(UVALive 6828)

韩耘豪
2023-12-01
Description

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

题解:

地球被划分为24个时区(-11~23),现在给出n个人的时区,将这n个人两两配对,使得n/2对配偶的时区差值之和最小。先把相同的一对排除掉,剩下的用一个数组存,因为题目要求相邻的时区,所以遍历一遍取最小即可。

代码如下:

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define maxn 27
#define N 100000000
#define INF 0x3f3f3f3f
#define mod 1001113
#define e  2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define put(x) prllf("%d\n",x)
#define memset(x,y) memset(x,y,sizeof(x))
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
#define ll long long
//std::ios::sync_with_stdio(false);
//cin.tie(NULL);
//const int maxn=;
using namespace std;

int a[222];
int b[60];
int main()
{
    int n;
    while(cin>>n)
    {
        memset(a,0);
        memset(b,0);
        int x;
        for(int i=0;i<n;i++)
        {
            cin>>x;
            a[x+11]++;
        }
        for(int i=0;i<=24;i++)
            a[i]%=2;
        int k=0;
        for(int i=0;i<=24;i++)
        {
            if(a[i]==1)
                b[k++]=i-11;
        }
        for(int i=k;i<k*2;i++)
            b[i]=b[i-k];
        int maxx=INF;
        for(int i=0;i<k;i++)
        {
            int sum=0;
            for(int j=i;j<i+k;j+=2)
                sum+=min(abs(b[j]-b[j+1]),24-abs(b[j]-b[j+1]));
            maxx=min(sum,maxx);
        }
        if(maxx!=INF)
            cout<<maxx<<endl;
        else
            cout<<"0"<<endl;
    }
    return 0;
}
 类似资料:

相关阅读

相关文章

相关问答