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 ...
随机推荐
- URL域名获取
http://"是协议名 "www.test.com"是域名 "是端口号 "aaa"是站点名 "bbb.aspx"是页面 ...
- GDI+ 和GDI
GDI:Graphics Device Interface,即图形设备接口,是Windows API的一个重要组成部分.它是Windows图形显示程序与实际物理设备之间的桥梁,GDI使得用户无需关心具 ...
- redhat vi 命令
转载:http://www.cnblogs.com/zhanglong0426/archive/2010/10/07/1845268.html http://blog.sina.com.cn/s/bl ...
- sencha touch Ext.app.Application
Ext.app.Application一般用于app.js中 用来初始化整个应用 可以预先加载controllers(控制器),models(模型),stores(数据源),views(视图) 例如: ...
- rabbitmq日志记录进出的每条消息
参考: https://blog.csdn.net/u013256816/article/details/76039201 https://blog.csdn.net/aosica321/articl ...
- JavaScript怎样学
嘿,我最近接到一个 Web 项目,不过老实说,我这两年没怎么接触 Web 编程,听说 Web 技术已经发生了一些变化.听说你是这里对新技术最了解的 Web 开发工程师? 准确地说,我是一名「前端工程师 ...
- Kafka usecase
h1, h2, h3, h4, h5, h6, p, blockquote { margin: 5px; padding: 5; } body { font-family: "Helveti ...
- Amazon ec2 改成密码登录方式
sudo passwd rootsu rootvi /etc/ssh/sshd_config"# PasswordAuthentication yes" uncommentsbin ...
- Unity3D笔记二十 多媒体与网络
1 游戏音频 1.游戏音乐:如游戏背景音乐 2.游戏音效:如开枪或打怪物时“砰砰”的游戏音效 Unity 3D游戏引擎共支持4种音乐格式的文件,具体如下. aiff:适用于较短的音乐文件,可用作游戏音 ...
- nginx 二级域名跳转
server { listen ; server_name m.aaoo.cn; #charset koi8-r; #access_log logs/host.access.log main; rew ...