poj 1029 False coin

冷不防 2022-05-30 07:43 295阅读 0赞

False coin














Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20128   Accepted: 5683

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

  1. 5 3
  2. 2 1 2 3 4
  3. <
  4. 1 1 4
  5. =
  6. 1 2 5
  7. =

Sample Output

  1. 3

题目解析:

这是很水的一个题目,直接暴力

代码;

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. using namespace std;
  5. const int maxn = 2000;
  6. int a[maxn][maxn];
  7. int b[maxn][maxn];
  8. char c[maxn];
  9. int main()
  10. {
  11. //freopen("in.txt","r",stdin);
  12. int n,m,s;
  13. while(cin>>n>>m)
  14. {
  15. int num = 0;
  16. int ok = 0;
  17. int flag = 1;
  18. int ans;
  19. memset(a,0,sizeof(a));
  20. memset(b,0,sizeof(b));
  21. int cnt = 1;
  22. while(m--)
  23. {
  24. cin>>s;
  25. for(int i=1; i<=s; i++)
  26. {
  27. int temp;
  28. cin>>temp;
  29. a[cnt][temp] = 1;
  30. }
  31. for(int i=1; i<=s; i++)
  32. {
  33. int temp;
  34. cin>>temp;
  35. b[cnt][temp] = 1;
  36. }
  37. cin>>c[cnt++];
  38. }
  39. for(int i=1; i<=n; i++)
  40. {
  41. int flag1 = 1,flag2 = 1;
  42. for(int j=1; j<cnt; j++)
  43. {
  44. if(c[j]=='='&&(a[j][i]!=0||b[j][i]!=0))
  45. {
  46. flag1 = 0;
  47. break;
  48. }
  49. if(c[j]=='>'&&a[j][i]!=1)
  50. {
  51. flag1 = 0;
  52. break;
  53. }
  54. if(c[j]=='<'&&b[j][i]!=1)
  55. {
  56. flag1 = 0;
  57. break;
  58. }
  59. }
  60. for(int j=1; j<cnt; j++)
  61. {
  62. if(c[j]=='='&&(a[j][i]!=0||b[j][i]!=0))
  63. {
  64. flag2 = 0;
  65. break;
  66. }
  67. if(c[j]=='<'&&a[j][i]!=1)
  68. {
  69. flag2 = 0;
  70. break;
  71. }
  72. if(c[j]=='>'&&b[j][i]!=1)
  73. {
  74. flag2 = 0;
  75. break;
  76. }
  77. }
  78. if(flag1||flag2)
  79. {
  80. ok = 1;
  81. ans = i;
  82. num++;
  83. }
  84. }
  85. if(ok&&num==1)
  86. cout<<ans<<endl;
  87. else
  88. cout<<"0"<<endl;
  89. }
  90. return 0;
  91. }

发表评论

表情:
评论列表 (有 0 条评论,295人围观)

还没有评论,来说两句吧...

相关阅读

    相关 sicily 1029

    思路:首先创建两个函数operate() 和cycle(),其中operate()求得两个大数之和,cycle()将数组向后平移一位。首先初始化数组num,数组num存储兔子的

    相关 PAT乙级1029

    1029 旧键盘 (20 分) 旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及实际被输入的文字,请你列出肯定坏掉的那些键。

    相关 PAT A1029

    ![clipboard.png][] 起先自己想尝试性的直接排序找中位数,内存直接超限; 其实这道题可以采用归并排序的思路来做: 但是示例依旧白给。。。不过还是展现了

    相关 URAL 1029

    题目大意:M层N列的矩阵(各元素均为正整数),找出一个路径从第一层到达第M层,使得路径上的所有数的和是所有可达路径中最小的,每次上到下一层以后就不能再上去,依次输出路径上的各点