1054:The Dominant Color

Posted 0kk470

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1054:The Dominant Color相关的知识,希望对你有一定的参考价值。

1054. The Dominant Color (20)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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, 800x600), 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

思路
map模拟一个字典,把出现的颜色计数,然后遍历字典把出现次数最多的颜色打印出来就行。

注:
1.原本是用的unordered_map容器,但是有两个用例会超时,应该是搜索的时候数据太大超时了,换成map就AC掉了。
2.map内部结构是红黑树,每次插入会自动平衡树和对插入的键排序,unordered_map内部是哈希表。超时的测试用例数据应该是具有一定顺序性使得map的查找效率比unordered_map更高了。

代码
#include<iostream>
#include<map>
using namespace std;
int main()
{
    int N,M;
    while(cin >> N >> M)
    {
     map<int,int> dic;
     for(int i = 0;i < N;i++)
     {
        for(int j = 0;j < M;j++)
        {
           int value;
           cin >> value;
           if(dic.count(value) > 0)
           {
             dic[value]++;
           }
           else
             dic.insert(pair<int,int>(value,1));
        }
     }
    pair<int,int> cur(0,0);
    for(map<int,int>::iterator it = dic.begin(); it != dic.end();it++)
    {
        if(it->second > cur.second)
        {
            cur.first = it->first;
            cur.second = it->second;
        }
    }

    cout << cur.first;
  }
}

 

以上是关于1054:The Dominant Color的主要内容,如果未能解决你的问题,请参考以下文章

pat 1054 The Dominant Color(20 分)

1054 The Dominant Color (20)(20 分)

PAT1054. The Dominant Color (20)

PAT 甲级 1054 The Dominant Color (20 分)(简单题)

PAT Advanced 1054 The Dominant Color (20分)

1054 The Dominant Color