1054 The Dominant Color (20)(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 strictlydominant 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, 2^24^). 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

题目大意:计算矩阵中出现次数超过半数的数字,数字范围是1-2^24.

//一开始我的提交是这样的,牛客网上可以通过,但是pat上运行测试点3超时。

#include <iostream>
#include<stdio.h>
#include<map>
using namespace std;
map<int,int> mp;
int main() {
int m,n,pix;
int mx=,mpx;
cin>>m>>n;
for(int i=;i<n;i++)
for(int j=;j<m;j++){
cin>>pix;
if(mp[pix]==)
mp[pix]=;
else
mp[pix]++;
if(mp[pix]>mx){
mpx=pix;
mx=mp[pix];
}
}
cout<<mpx;
return ;
}

1.参考了大佬的代码,发现大佬是直接判断如果出现超过半数,那么就直接Return 0;

2.修改return 0;之后提交仍是超时,就将mp==0去掉,直接出现就mp[pix]++;

3.提交之后还是超时,以为是数据的问题,改为map<long,int>还是不行;

4.最终将cin改为scanf即可,数据量大的时候cin读入会超时啊!

最终AC代码如下:

#include <iostream>
#include<stdio.h>
#include<map>
using namespace std;
map<int,int> mp;
int main() {
int m,n;
int pix;
scanf("%d %d",&m,&n);
int len=m*n/;
for(int i=;i<n;i++)
for(int j=;j<m;j++){
scanf("%d",&pix);
mp[pix]++;
if(mp[pix]>len){
printf("%d",pix);
return ;
}
} return ;
}

//第一次体会到,cin真的会超时。

PAT 1054 The Dominant Color[简单][运行超时的问题]的更多相关文章

  1. PAT 1054 The Dominant Color

    1054 The Dominant Color (20 分)   Behind the scenes in the computer's memory, color is always talked ...

  2. pat 1054 The Dominant Color(20 分)

    1054 The Dominant Color(20 分) Behind the scenes in the computer's memory, color is always talked abo ...

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

    1054 The Dominant Color (20 分)   Behind the scenes in the computer's memory, color is always talked ...

  4. PAT 甲级 1054 The Dominant Color (20 分)

    1054 The Dominant Color (20 分) Behind the scenes in the computer's memory, color is always talked ab ...

  5. PAT 甲级 1054 The Dominant Color

    https://pintia.cn/problem-sets/994805342720868352/problems/994805422639136768 Behind the scenes in t ...

  6. PAT (Advanced Level) Practice 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 i ...

  7. 1054. The Dominant Color (20)

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

  8. 1054 The Dominant Color (20)(20 分)

    Behind the scenes in the computer's memory, color is always talked about as a series of 24 bits of i ...

  9. 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 i ...

随机推荐

  1. linux下的shell操作mysql

    (1)MySQL的启动 重启了一次服务器后,使用> mysql -u root -p登陆是出现下面的错误: ERROR 2002 (HY000): Can't connect to local ...

  2. 【推荐系统论文笔记】Introduction To Recommender Systems: Algorithms and Evaluation

    这篇论文比较短,正如题目所说,主要还是简单地介绍了一下推荐系统的一些算法以及评估的方法. 推荐系统之前是基于关键字信息的过滤系统,后来发展成为协同过滤系统,解决了两个问题:1.通过人工审核去评价那些具 ...

  3. javascript的fn方法(转)

    jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(object); jQuery.extend(object); jQuery.extend(object); 为扩展j ...

  4. 【Eclipse】启动时报错:No Java virtual machine (已解决)

    在 Ubuntu 上下了个最新的 Eclipse ,解压后运行报这样的错误: 当然 JDK 的安装及环境变量的配置是没有问题的.使用 java -version 查询本机的JDK版本是可以的. 如果不 ...

  5. enum hack

    关于占用内存的大小,enum类型本身是不占内存的,编译器直接替换.但是enum类型的变量肯定是占内存的. class A{ public: //enum类型本身不占内存 enumEnumTest{ a ...

  6. MySQL对数据表进行分组查询(GROUP BY)

    MySQL对数据表进行分组查询(GROUP BY) GROUP BY关键字可以将查询结果按照某个字段或多个字段进行分组.字段中值相等的为一组.基本的语法格式如下: GROUP BY 属性名 [HAVI ...

  7. SPOJ1007 VLATTICE - Visible Lattice Points

    VLATTICE - Visible Lattice Points no tags  Consider a N*N*N lattice. One corner is at (0,0,0) and th ...

  8. TX大手笔做业务必然失败的原因

    首先说一个伪命题: 物体会向下落这是一个基本的定律,一个小小的物理规则会覆盖所有物体的行为准则. 那么,当地球上的所有东西都下落的时候,你指望整个地球,月球,太阳也会下落么? 事实上大家都知道星球在宇 ...

  9. 关于windows 7系统下开启休眠功能的方法

    今天笔者新装了一个windows 7操作系统,装完后,点击开始按钮.鼠标放到关机处的左边扩展选项时,没有发现休眠选项. 于是开始上网查询解决方法,并将过程记录如下: 首先简单的介绍一下休眠功能:休眠( ...

  10. Linux渗透之反弹Shell

    前言 当我们在渗透Linux主机时,反弹一个交互的shell是非常有必要的.在搜索引擎上搜索关键字“Linux 反弹shell”,会出现一大堆相关文章,但是其内容不但雷同,而且都仅仅是告诉我们执行这个 ...