Coder-Strike 2014 - Finals (online edition, Div. 2)

本文介绍了解决两个有趣的问题的方法:一是帮助Pasha将苹果公平地分配给两只仓鼠,确保每只仓鼠只能获得自己喜欢的苹果;二是判断一个单词是否为镜像单词,即该单词能在镜中完美反射自己。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. Pasha and Hamsters
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Pasha has two hamsters: Arthur and Alexander. Pasha put n apples in front of them. Pasha knows which apples Arthur likes. Similarly, Pasha knows which apples Alexander likes. Pasha doesn't want any conflict between the hamsters (as they may like the same apple), so he decided to distribute the apples between the hamsters on his own. He is going to give some apples to Arthur and some apples to Alexander. It doesn't matter how many apples each hamster gets but it is important that each hamster gets only the apples he likes. It is possible that somebody doesn't get any apples.

Help Pasha distribute all the apples between the hamsters. Note that Pasha wants to distribute all the apples, not just some of them.

Input

The first line contains integers n, a, b (1 ≤ n ≤ 100; 1 ≤ a, b ≤ n) — the number of apples Pasha has, the number of apples Arthur likes and the number of apples Alexander likes, correspondingly.

The next line contains a distinct integers — the numbers of the apples Arthur likes. The next line containsb distinct integers — the numbers of the apples Alexander likes.

Assume that the apples are numbered from 1 to n. The input is such that the answer exists.

Output

Print n characters, each of them equals either 1 or 2. If thei-h character equals 1, then thei-th apple should be given to Arthur, otherwise it should be given to Alexander. If there are multiple correct answers, you are allowed to print any of them.

不解释,直接上代码:

void Solution()
{
	while(cin >> n >> a >> b)
	{
		CLS(ret, 0);
		REP(i, a)
			cin >> a1[i];
		REP(i, b)
			cin >> a2[i];
		REP(i, a)
		{
			ret[a1[i]] = 1;
		}
		REP(i, b)
		{
			ret[a2[i]] = 2;
		}
		REPA(i, 1, (n+1))
			cout << ret[i] << " ";
		cout << endl;
	}
}

B. Start Up
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently, a start up by two students of a state university of city F gained incredible popularity. Now it's time to start a new company. But what do we call it?

The market analysts came up with a very smart plan: the name of the company should be identical to its reflection in a mirror! In other words, if we write out the name of the company on a piece of paper in a line (horizontally, from left to right) with large English letters, then put this piece of paper in front of the mirror, then the reflection of the name in the mirror should perfectly match the line written on the piece of paper.

There are many suggestions for the company name, so coming up to the mirror with a piece of paper for each name wouldn't be sensible. The founders of the company decided to automatize this process. They asked you to write a program that can, given a word, determine whether the word is a 'mirror' word or not.

Input

The first line contains a non-empty name that needs to be checked. The name contains at most105 large English letters. The name will be written with the next sans serif font:

Output

Print 'YES' (without the quotes), if the given name matches its mirror reflection. Otherwise, print 'NO' (without the quotes).

当时写复杂了。。。 还是菜。

char in[NUM];
int len;

bool is_ok(char c)
{
	char temp[] = {'A', 'H', 'I', 'M', 'O', 'T', 'U', 'V', 'W', 'X', 'Y'};
	REP(i, 11)
	{
		if(c == temp[i])
			return true;
	}
	return false;
}
void Solution()
{
	while(gets(in)!=NULL)
	{
		bool f = true;
		len = strlen(in);
		REP(i, len)
		{
			if(!is_ok(in[i]))
			{
				f = false;
				break;
			}
		}
		if(f){
			REP(i, (len/2))
			{
				if(in[i] != in[len-1-i])
				{
					f = false;
					break;
				}
			}
		}
		if(f)
			puts("YES");
		else
			puts("NO");
//		getchar();
	}
}

D. Bug in Code
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently a serious bug has been found in the FOS code. The head of the F company wants to find the culprit and punish him. For that, he set up an organizational meeting, the issue is: who's bugged the code? Each of then coders on the meeting said: 'I know for sure that eitherx ory did it!'

The head of the company decided to choose two suspects and invite them to his office. Naturally, he should consider the coders' opinions. That's why the head wants to make such a choice that at leastp ofn coders agreed with it. A coder agrees with the choice of two suspects if at least one of the two people that he named at the meeting was chosen as a suspect. In how many ways can the head of F choose two suspects?

Note that even if some coder was chosen as a suspect, he can agree with the head's choice if he named the other chosen coder at the meeting.

Input

The first line contains integers n and p (3 ≤ n ≤ 3·105; 0 ≤ p ≤ n) — the number of coders in the F company and the minimum number of agreed people.

Each of the next n lines contains two integersxi,yi(1 ≤ xi, yi ≤ n) — the numbers of coders named by thei-th coder. It is guaranteed thatxi ≠ i,  yi ≠ i,  xi ≠ yi.

Output

Print a single integer — the number of possible two-suspect sets. Note that the order of the suspects doesn't matter, that is, sets(1, 2) and(2, 1) are considered identical.

思路: 统计每个人得到票数,然后排序。 穷举找出所有候选人对,算出他们的支持人数。( 这里需要减去同一人同时支持两个人的情况)。 注意:结果可能超过int范围; 枚举的时候需要优化,不然会超时。

代码:

typedef pair<int, int> mpair;

struct _id{
    int id;
    int num;
}r[NUM];

map<mpair, int> store;


int n, p;
long long ans;

bool cmp(_id a, _id b)
{
    if(a.num != b.num)
        return a.num < b.num;
    else
        return a.id < b.id;
}

void Solution()
{
    while(cin >> n >> p)
    {
        ans = 0;
        REPA(i, 1, (n+1))
        {
            r[i].id = i;
            r[i].num = 0;
        }
        REP(i, n)
        {
            int a, b;
            cin >> a >> b;
            (r[a].num) ++;
            (r[b].num) ++;
            int mn = min(a, b);
            int mx = max(a, b);
            mpair temp = make_pair(mn, mx);
            if( store.find(temp) != store.end())
            {
                store[temp] ++;
            }
            else
            {
                store.insert(make_pair(temp, 1));
            }
        }

        for(map<mpair, int>::iterator it = store.begin(); it != store.end(); it++)
        {
            mpair temp = it->first;
            int value = r[temp.first].num + r[temp.second].num;
            if(value >= p && (value - it->second) < p)
                ans --;
        }
        sort(r + 1, r + n + 1, cmp);
        int last = -1, value = 0;
        REPA(i, 1, n)
        {
            if(r[i].num != last)
            {
                REPA(j, (i+1), (n+1)){
                    if(r[i].num + r[j].num >= p)
                    {
                        int temp = n - j + 1;
                        ans += temp;
                        last = r[i].num;
                        if(r[i].num * 2 >= p)
                            value = temp - 1;
                        else
                            value = temp;
                        break;
                    }
                }
            }
            else
            {
                ans += value;
                if(r[i].num * 2 >= p)
                    value --;
            }
        }
        cout << ans << endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值