PAT A 甲级 1054 The Dominant Color (20分)

1054 The Dominant Color (20分)

吐槽

说实话,我确信我的这个算法不能真正完美解决这个问题。我能简单地想到一个会导致它失效的极端数据。但是题目通过了。果然没有过分到使用那种情况(点头)。
(但是我还是回头去搞出了这道题的完美算法hhhhh)

以及果然cin改scanf是保留项目。

思路

看到题目的那一刻就明白肯定是个用时极限的题目。即使我认定自己的方法已经大幅缩减了题干用时,在把cin改成scanf前依然报了一个超时。

虽然确信第一种方法不完美,至少通过了,总之先说一下。
我统计了每一行是否有出现次数过半的数字,如果有,那么它就是这一行的主要颜色(没有那就没有)。然后我再统计成为一行的主要颜色的次数最多的颜色。

当然,这里“次数过半”和“次数最多”其实也可以交换,也许也能通过,但说实话,不管哪个都不完美。

比如:“次数过半”显然是一个比“次数最多”更严的要求(过半是最多的充分不必要)。所以最严的条件是两个都“次数过半”。然而假设这么一个场景:一个600800的区间内,有一个301401的方块是颜色A,剩下部分是颜色B。
这样的话,A有401行过半,B有399行过半。按照我的程序逻辑,会认为A更多。然而实际上,B的数量几乎是A的三倍。甚至可以更夸张:A的方块是301*797,并在剩下3行中每行占有34个,剩下的是B,这样程序逻辑上就是797对3,A压倒性,然而实际依然B更多(239999:240001)。
考虑到如此极端的状况,我个人着实无法想出除了老老实实计算每个元素的出现量以外的完美解决这个问题的方式。

所以……
就真的这么做就行了。事实证明600*800不是很大。
神奇的map函数可以用find函数确认一个键是否被录入过,如果没有,你就把那个键的值设为1,有就把值++。
然后用it遍历找到数量过半了的那个就行。或者你在录入途中直接检测值++后有没有大于总数的一半也行,似乎会更快,甚至不用cin改scanf。

(如果这道题颜色出现过多内存也极限的话……)

代码(不完美版)

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
	int m, n, i, ii, iii, temp;
	vector<int> v1;
	vector<int> v2;
	cin >> m >> n;
	for (i = 0; i < n; i++)
	{
		v1.clear();
		for (ii = 0; ii < m; ii++)
		{
			scanf("%d", &temp);
			v1.push_back(temp);
		}
		sort(v1.begin(), v1.end());
		temp = -1;
		for (ii = 0; ii < m;)
		{
			iii = 1;
			while (ii + iii < v1.size() && v1[ii + iii] == v1[ii])
				iii++;
			if (2 * iii > m)
			{
				temp = v1[ii];
				break;
			}
			else
				ii += iii;
		}
		if (temp != -1)
			v2.push_back(temp);
	}
	sort(v2.begin(), v2.end());
	int max = 0;
	int now;
	for (i = 0; i < v2.size(); i++)
	{
		ii = 1;
		while (i + ii < v2.size() && v2[i + ii] == v2[i + ii])
			ii++;
		if (ii > max)
		{
			max = ii;
			now = v2[i];
		}
	}
	cout << now;
	cin >> i;
	return 0;
}

代码(map版)

#include<iostream>
#include<map>
#include<algorithm>

using namespace std;

int main()
{
	int m, n, i, ii, iii, temp;
	map<int, int> ma;
	map<int, int>::iterator it;
	cin >> m >> n;
	for (i = 0; i < m * n; i++)
	{
		scanf("%d",&temp);
		it = ma.find(temp);
		if (it == ma.end())
			ma[temp] = 1;
		else
			ma[temp]++;
	}
	for (it = ma.begin(); it != ma.end(); it++)
		if (it->second * 2 >= m * n)
		{
			cout << it->first;
			break;
		}
	cin >> i;
	return 0;
}

题目

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,2
​24
​​ ). 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值