首页 > 其他分享 >UVa 11205 The broken pedometer (枚举好题&巧用二进制)

UVa 11205 The broken pedometer (枚举好题&巧用二进制)

时间:2023-04-12 11:37:10浏览次数:56  
标签:symbols LEDs codification 好题 number pedometer broken active problem


11205 - The broken pedometer

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=107&page=show_problem&problem=2146

The Problem

A marathon runner uses a pedometer with which he is having problems. In the pedometer the symbols are represented by seven segments (or LEDs):


But the pedometer does not work properly (possibly the sweat affected the batteries) and only some of the LEDs are active. The runner wants to know if all the possible symbols:


can be correctly identified. For example, when the active LEDs are:


numbers 2 and 3 are seen as:


so they cannot be distinguished. But when the active LEDs are:


the numbers are seen as:


and all of them have a different representation.

Because the runner teaches algorithms at University, and he has some hours to think while he is running, he has thought up a programming problem which generalizes the problem of his sweat pedometer. The problem consists of obtaining the minimum number of active LEDs necessary to identify each one of the symbols, given a number P of LEDs, and N symbols to be represented with these LEDs (along with the codification of each symbol).

For example, in the previous sample P = 7 and N = 10. Supposing the LEDs are numbered as:


The codification of the symbols is: "0" = 1 1 1 0 1 1 1; "1" = 0 0 1 0 0 1 0; "2" = 1 0 1 1 1 0 1; "3" = 1 0 1 1 0 1 1; "4" = 0 1 1 1 0 1 0; "5" = 1 1 0 1 0 1 1; "6" = 1 1 0 1 1 1 1; "7" = 1 0 1 0 0 1 1; "8" = 1 1 1 1 1 1 1; "9" = 1 1 1 1 0 1 1. In this case, LEDs 5 and 6 can be suppressed without losing information, so the solution is 5.

The Input

The input file consists of a first line with the number of problems to solve. Each problem consists of a first line with the number of LEDs (P), a second line with the number of symbols (N), and N lines each one with the codification of a symbol. For each symbol, the codification is a succession of 0s and 1s, with a space between them. A 1 means the corresponding LED is part of the codification of the symbol. The maximum value of P is 15 and the maximum value of N is 100. All the symbols have different codifications.

The Output

The output will consist of a line for each problem, with the minimum number of active LEDs necessary to identify all the given symbols.

Sample Input


2
7
10
1 1 1 0 1 1 1
0 0 1 0 0 1 0
1 0 1 1 1 0 1
1 0 1 1 0 1 1
0 1 1 1 0 1 0
1 1 0 1 0 1 1
1 1 0 1 1 1 1
1 0 1 0 0 1 0
1 1 1 1 1 1 1
1 1 1 1 0 1 1
6
10
0 1 1 1 0 0
1 0 0 0 0 0
1 0 1 0 0 0
1 1 0 0 0 0
1 1 0 1 0 0
1 0 0 1 0 0
1 1 1 0 0 0
1 1 1 1 0 0
1 0 1 1 0 0
0 1 1 0 0 0


Sample Output


5
4



巧用二进制判断两个数在某一种有效LED组(active LEDs)下是否显示是一样的。

都写在注释里了。


完整代码:

/*0.116s*/

#include <cstdio>
#include <cstring>
const int P = 1 << 15, maxn = 105;

bool vis[P + 5];
int a[maxn];

int count(int n)///统计n的二进制中1的个数
{
	int sum = 0;
	while (n)
	{
		if (n & 1)
			++sum;
		n >>= 1;
	}
	return sum;
}

int main()
{
	int t, p, n, x, i, j, ans, up;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d", &p, &n);
		for (i = 0; i < n; i++)
		{
			scanf("%d", &a[i]);
			for (j = 1; j < p; j++)
			{
				scanf("%d", &x);
				a[i] = (a[i] << 1) + x;///把01串压缩成一个数
			}
		}
		ans = maxn, up = 1 << p;
		for (i = 0; i < up; i++)///枚举
		{
			int bits = count(i);
			if (bits >= ans) continue;
			memset(vis, 0, sizeof(vis));
			for (j = 0; j < n; ++j)
			{
				int pos = a[j] & i;///a[j]在i下的显示结果
				if (vis[pos])///说明有两个数显示是一样的
					break;
				vis[pos] = true;
			}
			if (j < n) continue;
			ans = bits;
		}
		printf("%d\n", ans);
	}
	return 0;
}




标签:symbols,LEDs,codification,好题,number,pedometer,broken,active,problem
From: https://blog.51cto.com/u_5535544/6185333

相关文章

  • UVa 129 Krypton Factor (回溯好题)
    129-KryptonFactorTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=65YouhavebeenemployedbytheorganisersofaSuperKryptonFactorContestinwhichcontestantshaveveryhighmental......
  • beaten but not broken
    儿时懵懂梦,长大依旧留。岁月忽五载,抹去方重来。天花乱欲坠,人草昏欲睡。沉默是金子,冷漠为眼泪。秒速五厘米,樱花碎一地。韵脚无雅兴,长谈难诉心。记梦:热闹的团聚是散场的开始。一切终有竟时。不过,当一切巨星陨落,万物终再临。......
  • N04、重建二叉树(给出前序中序,重建二叉树,好题 绝对的好题)
    ​​4、重建二叉树​​(给出前序中序,重建二叉树)好题绝对的好题输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重......
  • P4170 [CQOI2007]涂色(思维好题)
    P4170[CQOI2007]涂色-洛谷|计算机科学教育新生态(luogu.com.cn)设DP[i][j]为完成(i-j)区间的最少涂鸦次数。考虑dp[i][j]的转移:重点:如果s[i]==s[j],dp[i][j]=min(dp[......
  • 超级书架2 计蒜客 - T1736(01背包应用,好题)
    题意:FarmerJohn最近为奶牛们的图书馆添置了一个巨大的书架,尽管它是如此的大,但它还是几乎瞬间就被各种各样的书塞满了。现在,只有书架的顶上还留有一点空间。所有N(1≤N≤......
  • Codeforces Round #521 (Div. 3) D. Cutting Out 好题字符串
    D.CuttingOuttimelimitpertest3secondsmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputYouaregivenanarray ss consistingof n......
  • Codeforces Round #520 (Div. 2) A. A Prank 好题
    A.APranktimelimitpertest1secondmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputJATCandhisfriendGiraffearecurrentlyinthei......
  • UVA12096 The SetStack Computer 栈的应用 好题
    题意翻译对于一个以集合为元素的栈,初始时栈为空。输入的命令有如下几种:PUSH:将空集{}压栈DUP:将栈顶元素复制一份压入栈中UNION:先进行两次弹栈,将获得的集合A和B取并集,将结......
  • Codeforces1059C. Sequence Transformation 好题 没做出来 数学思维
     C.SequenceTransformationtimelimitpertest2secondsmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputLet'scallthefollowingproce......
  • 51NOD 1278 相离的圆(二分 + 排序 好题)
    平面上有N个圆,他们的圆心都在X轴上,给出所有圆的圆心和半径,求有多少对圆是相离的。例如:4个圆分别位于1,2,3,4的位置,半径分别为1,1,2,1,那么{1,2},{1,3}{2,3}{2,4......