PAT 1054 The Dominant Color[简单][运行超时的问题]
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[简单][运行超时的问题]的更多相关文章
- PAT 1054 The Dominant Color
1054 The Dominant Color (20 分) Behind the scenes in the computer's memory, color is always talked ...
- pat 1054 The Dominant Color(20 分)
1054 The Dominant Color(20 分) Behind the scenes in the computer's memory, color is always talked abo ...
- PAT 甲级 1054 The Dominant Color (20 分)(简单题)
1054 The Dominant Color (20 分) Behind the scenes in the computer's memory, color is always talked ...
- PAT 甲级 1054 The Dominant Color (20 分)
1054 The Dominant Color (20 分) Behind the scenes in the computer's memory, color is always talked ab ...
- PAT 甲级 1054 The Dominant Color
https://pintia.cn/problem-sets/994805342720868352/problems/994805422639136768 Behind the scenes in t ...
- 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 ...
- 1054. The Dominant Color (20)
时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Behind the scenes in the compute ...
- 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 ...
- 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 ...
随机推荐
- MDU某产品OMCI模块代码质量现状分析
说明 本文参考MDU系列某产品OMCI模块现有代码,提取若干实例以说明目前的代码质量,亦可作为甄别不良代码的参考. 本文旨在就事论事,而非否定前人(没有前人的努力也难有后人的进步).希望以史为鉴,不破 ...
- java高级---->Thread之单例模式的使用
这里我们介绍一下在多线程中如何安全正确的编写单例模式的代码.不知为何,恰如其分的话总是姗姗来迟,错过最恰当的时机. 多线程中的单例模式 这里面通过代码来体会一下在多线程中如何正确的编写单例模式的代码. ...
- Sencha Touch 实战开发培训 视频教程 第二期 第四节
2014.4.14 晚上8:10分开课. 本节课耗时没有超出一个小时,主要讲解了list的一些扩展用法. 本期培训一共八节,前两节免费,后面的课程需要付费才可以观看. 本节内容: List的高级应用 ...
- nginx 日志文件
默认日志格式 log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status ...
- 23种设计模式之备忘录模式(Memento)
备忘录模式确保在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样可以在以后将对象恢复到原先保存的状态.备忘录模式提供了一种状态恢复的实现机制,使得用户可以方便地回到一个特定 ...
- 兵器簿之github的配置和使用
1.注册一个github 账号,这个大家都懂得了啊 2.配置 (1 检查:进入终端,在用户目录下输入: ls -al ~/.ssh 得到下图代表本地没有配置过github 过. (2 创建一个目录,输 ...
- java 中常见的一些错误
1.NosuchMethodError java 类中找不到该方法! 可能该类所在的同一个包下有一个相同的相同的类,然后那个类中没有我们所要调用的类. 解决方法:若那个类不需要,可以删除class! ...
- 线段树(Segment Tree)总结
0 写在前面 怎么说呢,其实从入坑线段树一来,经历过两个阶段,第一个阶段是初学阶段,那个时候看网上的一些教学博文和模板入门了线段树, 然后挑选了一个线段树模板作为自己的模板,经过了一点自己的修改,然后 ...
- 解决oracle12c安装报“[INS-30131]执行安装程序验证所需的初始设置失败(原因:无法访问临时位置)”方法
安装过很多次oracle,顺顺利利的,今天在新机子上安装oracle12c client过程中竟然神奇的报出一个错误: 很明显的,已经很明确的给出了安装失败的原因:无法访问临时位置!实际上,在安装数据 ...
- Java Native Interface 基于JNI的嵌入式手机软件开发实例
1.通过JNI和c/c++的库组件.其他代码交互 2.java和c不能互通的原因时数据类型问题 Introduction https://docs.oracle.com/javase/8/docs/t ...