Categories
不学无术

1054. The Dominant Color (20)

时间限制:100ms
Behind the scenes in the computer’s memory, color is always talked about as a series of 24 bits of information for each pixel. In an image, the color with the largest proportional area is called the dominant color. A strictly dominant color takes more than half of the total area. Now given an image of resolution M by N (for example, 800×600), you are supposed to point out the strictly dominant color.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive numbers: M (<=800) and N (<=600) which are the resolutions of the image. Then N lines follow, each contains M digital colors in the range [0, 224). It is guaranteed that the strictly dominant color exists for each input image. All the numbers in a line are separated by a space.
Output Specification:
For each test case, simply print the dominant color in a line.
Sample Input:

5 3
0 0 255 16777215 24
24 24 0 0 24
24 0 24 24 24

Sample Output:

24

==============================
这题目就是个投票选班长的题目,上个学期数据库老师课上讲过(可惜了,数据库后来没考好),瞬间犹如醍醐灌顶~

醍醐灌顶,出于《敦煌变文集·维摩诘经讲经文》:“令问维摩,闻名之如露入心,共语似醍醐灌顶。”佛教指灌输智慧,使人彻底觉悟。比喻听了高明的意见使人受到很大启发。

最笨的办法是每个人画正字,浪费空间,最后还要统计票数
最终的目的是要选出票数最多的一个人,那么可以这样做:

初始化:
班长候选人:叉叉叉
当前差额票数:1
然后拿一张选票,票面上的名字是Name
if Name == 当前的候选人:
差额票数++
else:
差额票数--
if 差额票数==0:
候选人 = Name
差额票数 = 1

这样的话,省时间也省空间:)
原理其实想通了很简单,因为我们只选一个人啊,只算净胜票数就行
净胜票数为0了,那就要被赶下台了!
从测试点的结果来看,的确有个测试点数据量挺大的…估计之前说的笨办法就要超时了..
=================================

#include
int main()
{
int M, N;
scanf("%d %d", &M, &N);
int resolution = M * N;
int currBest = -1;
int currCount = 1;
for(int i=0; i

测试点	结果	用时(ms)	内存(kB)	得分/满分
0	答案正确	0	790	12/12
1	答案正确	0	790	2/2
2	答案正确	60	780	2/2
3	答案正确	0	790	2/2
4	答案正确	0	710	2/2

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.