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 }
  • 相关阅读:
    Tomcat源码(二):tomcat启动之前的初始化
    Tomcat源码(一):整体架构
    SSH的三个组件ssh、sftp、scp介绍
    远程连接linux服务上的mysql
    linux系统上安装mysql5.6(详细步骤)
    一种基于数字证书的单点登录的实现方法(转)
    jacob操作word (转)
    解决fis3下载慢,没反应
    Spring框架集成mybatis框架的配置(笔记)
    eclipse导入lombok后打不开(如果你的lombok不是最新的,那就来下载最新的)
  • 原文地址:https://www.cnblogs.com/chunlinn/p/10743935.html
Copyright © 2011-2022 走看看