【算法笔记】A1054 The Dominant Color

1054 The Dominant Color (20 分)
 

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, 8), 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 (≤) and N (≤) which are the resolutions of the image. Then N lines follow, each contains M digital colors in the range [0). 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

题意

  给出一张图片的分辨率,在下面的n行m列中给出每个像素点的颜色编码,最后输出数量最多的颜色的编码。

思路:

  用map记录每个颜色出现的次数,很容易实现输出出现次数最多颜色。

  第一次失误写成了 cout<<color; 竟然AC了。。。难道每个测试用例最后输入的颜色就是答案?把map去掉只留下输入color输出color,还真的全部AC。可惜输入m*n个数时间复杂度还是O(m*n),跟原来的耗时没什么区别,暂时还没什么方法把时间复杂度降到O(1)。

code:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 map<int, int> colorMap;
 4 int main(){
 5     int m, n, color, domin = 0;
 6     cin>>m>>n;
 7     for(int i = 0; i < n; i++){
 8         for(int j = 0; j < m; j++){
 9             cin>>color;
10             colorMap[color]++;
11             if(colorMap[domin] < colorMap[color]) domin = color;
12         }
13     }
14     cout<<domin;
15     return 0;
16 }
posted @ 2019-04-21 10:03  Resfeber  阅读(180)  评论(0编辑  收藏  举报