zoukankan      html  css  js  c++  java
  • 【算法笔记】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 }
  • 相关阅读:
    C# extern关键字的用法
    C#自定义集合类(二)
    C#自定义集合类(一)
    LINQ中交集、并集、差集、去重(十四)
    LINQ中转换操作符(十三)
    Oracle实现连乘和求和
    适配器模式
    HTTP网络协议与手写Web服务容器
    代理模式
    设计模式的几条家规
  • 原文地址:https://www.cnblogs.com/chunlinn/p/10743935.html
Copyright © 2011-2022 走看看