Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u

Description

Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are true silver dollars; one coin is counterfeit even though its color and size make it indistinguishable from the real silver dollars. The counterfeit coin has a different weight from the other coins but Sally does not know if it is heavier or lighter than the real coins.
Happily, Sally has a friend who loans her a very accurate balance scale. The friend will permit Sally three weighings to find the counterfeit coin. For instance, if Sally weighs two coins against each other and the scales balance then she knows these two coins are true. Now if Sally weighs
one of the true coins against a third coin and the scales do not balance then Sally knows the third coin is counterfeit and she can tell whether it is light or heavy depending on whether the balance on which it is placed goes up or down, respectively.
By choosing her weighings carefully, Sally is able to ensure that she will find the counterfeit coin with exactly three weighings.

Input

The first line of input is an integer n (n > 0) specifying the number of cases to follow. Each case consists of three lines of input, one for each weighing. Sally has identified each of the coins with the letters A–L. Information on a weighing will be given by two strings of letters and then one of the words “up”, “down”, or “even”. The first string of letters will represent the coins on the left balance; the second string, the coins on the right balance. (Sally will always place the same number of coins on the right balance as on the left balance.) The word in the third position will tell whether the right side of the balance goes up, down, or remains even.

Output

For each case, the output will identify the counterfeit coin by its letter and tell whether it is heavy or light. The solution will always be uniquely determined.

Sample Input

1 
ABCD EFGH even 
ABCI EFJK up 
ABIJ EFGH even

Sample Output

K is the counterfeit coin and it is light.

Source


思路:刚看到题的时候完全没有想法,后来看了下网上大牛的题解才恍然大悟。
首先明确以下三个条件对应的逻辑:
1)even,说明两边都是真币(想不通请面壁)
2)up,说明右边有一枚轻的假币,左边全为真币,或者右边全为真币,左边有一枚重的假币
3)down,与up相反,说明右边有一枚重的假币,左边全为真币,或者右边全为真币,左边有一枚轻的假币
接下来考虑代码实现。把even时两边的代号全部标记,因为可以保证它们都是真的。开一个数组表示每个字母是假币的可能程度,up时就把右边所有未标记字母的这个值减1,左边未标记的都加1。这个值越大说明heavy的可能性越大,light的可能性越小。而heavy和light都是假币,所以绝对值表示是假币的可能性。三条消息处理完后把这个值扫一遍,绝对值最大的就是假币,然后根据符号判断是heavy还是light。
AC代码:
#include <cstdio>
#include <cstring>
using namespace std;
int possibility[15];//0 -- true <0 -- light >0 -- heavy
bool okay[15];
struct node
{
	char left[10], right[10], status[5];
}info[3];
void input()
{
	for (int i = 0; i < 3; i++)
		scanf("%s %s %s\n", info[i].left, info[i].right, info[i].status);
}
inline int Abs(int x)
{
	if (x < 0)
		return -x;
	return x;
}
void solve()
{
	memset(possibility, 0, sizeof(possibility));
	memset(okay, 0, sizeof(okay));
	for (int i = 0; i < 3; i++)
	{
		if (!strcmp(info[i].status, "even"))
			for (int j = 0; j < strlen(info[i].left); j++)
			{
				possibility[info[i].left[j] - 'A'] = possibility[info[i].right[j] - 'A'] = 0;
				okay[info[i].left[j] - 'A'] = okay[info[i].right[j] - 'A'] = 1;
			}
		else if (!strcmp(info[i].status, "up"))
			for (int j = 0; j < strlen(info[i].left); j++)
			{
				if (!okay[info[i].left[j] - 'A'])
					possibility[info[i].left[j] - 'A']++;
				if (!okay[info[i].right[j] - 'A'])
					possibility[info[i].right[j] - 'A']--;
			}
		else if (!strcmp(info[i].status, "down"))
			for (int j = 0; j < strlen(info[i].left); j++)
			{
				if (!okay[info[i].left[j] - 'A'])
					possibility[info[i].left[j] - 'A']--;
				if (!okay[info[i].right[j] - 'A'])
					possibility[info[i].right[j] - 'A']++;
			}
	}
	int MaxPo = -0x3f3f3f3f, MaxNum;
	for (int i = 0; i < 12; i++)
		if (Abs(possibility[i]) > MaxPo)
		{
			MaxPo = Abs(possibility[i]);
			MaxNum = i;
		}
	printf("%c is the counterfeit coin and it is %s.\n", MaxNum + 'A', possibility[MaxNum] < 0 ? "light" : "heavy");
}
int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		input();
		solve();
	}
	return 0;
}

 

分类: ACM/ICPC

发表评论

电子邮件地址不会被公开。 必填项已用*标注