False coin
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21897 | Accepted: 6126 |
Description
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
样例解析:一共5个硬币,给出三组判定条件,第一组,每个盘中两个硬币,左盘中放置编号为1 2的硬币,右盘中放置编号为3 4的硬币,结果是左盘的质量小于右盘的质量,故而1 2 3 4中一定有一个是不合格的;再看第二组数据,每个盘中一个硬币,左边为编号为1的硬币,右边为编号为4的硬币,结果为两盘的质量相等,说明这1 4硬币是合格硬币;第三组数组同理可说明2 5硬币是合格的;故而只有一定是3号硬币。
思路:该题是让我们根据所给条件找出n个硬币中不符合条件的那一个。注意无法找到不合格硬币的情况,一:所有条件均为等号,且有两个或以上的硬币没有提到,此时无法判断;二:出现了"<"和">",但条件并不充分。总的思路是记录题目给出条件中不同硬币出现的次数,当结果为"<"或">"时 a[i]++,为"="时直接排除出现的硬币。
我的代码:
#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
int main()
{
// freopen("in.txt","r",stdin);
int n,k;
int a[1005];//记录每个硬币出现的次数
int b[1005];
bool flag[1005];
scanf("%d%d",&n,&k);
for(int i=1; i<=n; i++)
{
a[i]=0;
flag[i]=false;
}
while(k--)
{
int p;
scanf("%d",&p);
for(int i=0; i<2*p; i++)
{
scanf("%d",&b[i]);
}
getchar();
char ch;
scanf("%c",&ch);
if(ch=='<')
{
for(int i=0; i<p; i++)
a[b[i]]--,a[b[i+p]]++;
}
else if(ch=='>')
{
for(int i=0; i<p; i++)
a[b[i]]++,a[b[i+p]]--;
}
else
{
for(int i=0; i<p; i++)
a[b[i]]=0,a[b[i+p]]=0,flag[b[i]]=true,flag[b[i+p]]=true;
}
}
int maxx=0,ad=-1;
for(int i=1; i<=n; i++)
{
if(a[i]<0)
a[i]=-a[i];
if(maxx<a[i])
maxx=a[i],ad=i;
}
int cnt=0;
if(ad==-1)
{
for(int i=1; i<=n; i++)
if(flag[i]==false)
cnt++,ad=i;
}
else
{
for(int i=1; i<=n; i++)
if(a[i]==maxx)
cnt++;
}
if(cnt==1)
printf("%d\n",ad);
else
printf("0\n");
return 0;
}
若有不对,大佬们尽管指出,非常感谢!