ZCMU-1229-Two bags

Description

Xiaoming has 2*n cubes,each cube contains an integer from 10 to 99.He arbitrarily chooses n cubes and puts them in the first bag.The remaining cubes form the second bag.

Xiaoming decided to play with cubes.During the game he takes a cube from the first bag and writes down the number it has.Then he takes a cube from the second bag and write out its two digits near two digits he had written(to the right of them).In the end he obtained a single fourdigit integer --the first two digits of it is written on the cube from the first bag,and the second two digits of it is written on the second cube from the second bag.

Xiaoming is not good at arithmetic.So,he can not easily count the largest number of distinct fourdigit numbers he can get in the game.Can you help him?

Input

The first line contains integer n(0

Output

In the first line print a single number–the maximum possible number of distinct four-digit numbers Xiaoming can obtain.

Sample Input

1
10 99
2
13 24 13 45

Sample Output

1
4

题意就是把2n个数分成两组,然后从第一组中选一个数ab(两位数a是十位b是个位),从第二组中选一个数cd,组成abcd,求最多能组合出几个不同abcd。我的思路是先找相同的如果相同的个数多于2个就保留2个,其他的多的就去掉,总数也相应的减掉,最后如果个数cnt是奇数,就是 (cnt+1)/2*(cnt/2),是偶数的话就是cnt*cnt/4。

#include
using namespace std;
#define ll long long
#define mem(a) memset(a,0,sizeof(a))
#define N 1000000007
 
int s[300];
 
int main()
{
    int n;
    while(~scanf("%d", &n)){
        for(int i = 0; i < 2*n; i++) scanf("%d", &s[i]);
        sort(s,s+2*n);
        int num = n*2;
        int cnt = 0;
        int x = 1;
        for(int i = 0; i < 2*n-1; i++){
            if(s[i]==s[i+1]){
                x = 1;
                while(s[i]==s[i+1]&&i<2*n-1){
                    i++;
                    x++;
                }
                if(x>2){
                    num -= (x-2);
                }
                cnt++;
            }
        }
        int sum = 0;
        if(num%2==1){
            sum = ((num+1)/2)*(num/2);
        }else{
            sum = num*num/4;
        }
        printf("%d\n", sum);
    }
    return 0;
}

你可能感兴趣的:(题解)