码迷,mamicode.com
首页 > 其他好文 > 详细

1054:The Dominant Color

时间:2017-10-04 17:59:44      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:++   tput   文本对比   style   打印   颜色   模拟   排序   std   

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

标签:++   tput   文本对比   style   打印   颜色   模拟   排序   std   

原文地址:http://www.cnblogs.com/0kk470/p/7625908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!