ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

ACM - False Coin

2020-02-20 20:45:58  阅读:309  来源: 互联网

标签:include False weight int coins ACM book Coin pan


False Coin

The "Gold Bar"bank received information from reliable sources that in their last group of N coins exactly one coin is false and differs in weight from other coins (while all other coins are equal in weight). After the economic crisis they have only a simple balance available (like one in the picture). Using this balance, one is able to determine if the weight of objects in the left pan is less than, greater than, or equal to the weight of objects in the right pan.In order to detect the false coin the bank employees numbered all coins by the integers from 1 to N, thus assigning each coin a unique integer identifier. After that they began to weight various groups of coins by placing equal numbers of coins in the left pan and in the right pan. The identifiers of coins and the results of the weightings were carefully recorded.You are to write a program that will help the bank employees to determine the identifier of the false coin using the results of these weightings.

Input
The first line of the input file contains two integers N and K, separated by spaces, where N is the number of coins (2<=N<=1000 ) and K is the number of weightings fulfilled (1<=K<=100). The following 2K lines describe all weightings. Two consecutive lines describe each weighting. The first of them starts with a number Pi (1<=Pi<=N/2), representing the number of coins placed in the left and in the right pans, followed by Pi identifiers of coins placed in the left pan and Pi identifiers of coins placed in the right pan. All numbers are separated by spaces. The second line contains one of the following characters: ‘<’, ‘>’, or ‘=’. It represents the result of the weighting:
‘<’ means that the weight of coins in the left pan is less than the weight of coins in the right pan,
‘>’ means that the weight of coins in the left pan is greater than the weight of coins in the right pan,
‘=’ means that the weight of coins in the left pan is equal to the weight of coins in the right pan.

Output
Write to the output file the identifier of the false coin or 0, if it cannot be found by the results of the given weightings.

Sample Input
5 3
2 1 2 3 4
‘<’
1 1 4
‘<’
1 2 5
'>'

Sample Output
3

暴力模拟:
思路:比较结果为“=”时,天平两边的砝码都是正常的。比较结果是“<”,或是“>”时,坏砝码肯定出现了,而且那个坏的砝码只能在一边(重的一边或者是轻的一边)。我们可以用一个book数组记录哪些砝码是好的,哪些还不能判断;用数组p记录砝码在天平的哪一边,如果在轻的那边p[i]–,否则p[i]++。因为坏砝码只能出现在“>”或“<”中,且只能出现在一边,因此坏砝码的p[坏]==±(“>”和“<”的出现次数)。
原文链接:https://blog.csdn.net/qq_41890797/article/details/81541082

代码:

#include<iostream>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;
int f[1010], book[1010], p[1010];
int main()
{
	int n, m, k;
	int ans = 0;
	memset(f, 0, sizeof(f));
	memset(book, 0, sizeof(book));
	cin >> n >> k;
	while (k--)
	{
		scanf("%d", &m);
		for (int i = 0; i < 2 * m; i++)
			scanf("%d", &p[i]);
		getchar();
		char c;
		scanf("%c", &c);
		if (c == '=')
			for (int i = 0; i < 2 * m; i++)
				book[p[i]] = 1;
		else if (c == '<')
		{
			ans++;
			for (int i = 0; i < m; i++)  //轻的一边
				f[p[i]]--;
			for (int i = m; i < 2 * m; i++)  //重的一边
				f[p[i]]++;
		}
		else
		{
			ans++;
			for (int i = 0; i < m; i++) //重的一边
				f[p[i]]++;
			for (int i = m; i < 2 * m; i++)  //轻的一边
				f[p[i]]--;
		}
	}
	int ces = 0, flag;
	for (int i = 1; i <= n; i++)
	{
		if (!book[i])
		{
			if (f[i] == ans || f[i] == -ans)
			{
				ces++;
				flag = i;
			}
		}
	}
	if (ces == 1)
		printf("%d\n", flag);
	else
		printf("0\n");
}
Black_xtree 发布了7 篇原创文章 · 获赞 0 · 访问量 69 私信 关注

标签:include,False,weight,int,coins,ACM,book,Coin,pan
来源: https://blog.csdn.net/times_cat/article/details/104383291

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有