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 24Sample 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 ... 
随机推荐
- Qt编写密钥生成器+使用demo(开源)
			在很多商业软件中,需要提供一些可以试运行的版本,这样就需要配套密钥机制来控制,纵观大部分的试用版软件,基本上采用以下几种机制来控制.1:远程联网激活,每次启动都联网查看使用时间等,这种方法最完美,缺点 ... 
- python中字符串(str)的常用处理方法
			str='python String function' 生成字符串变量str='python String function' 字符串长度获取:len(str)例:print '%s length= ... 
- Why is IMAP better than POP?
			https://www.fastmail.com/help/technical/imapvspop.html POP is a very simple protocol that only allow ... 
- openstack-networking-neutron(二)---tun/tap
			本博客已经添加"打赏"功能,"打赏"位置位于右边栏红色框中,感谢您赞助的咖啡. 简介 虚拟网卡Tun/tap驱动是一个开源项目,支持很多的类UNIX平台,Ope ... 
- 用ELK打造可视化集中式日志
			原文链接:https://yq.aliyun.com/articles/57420 摘要: Elk是Elastic search, Logstash和Kibana三者的简称. Elastic sear ... 
- 解决远程登陆Linux误按ctrl+s锁屏
			很多刚从windows转移到linux上来工作的朋友,在用vi/vim编辑文件时,常常会习惯性的按下Ctrl+s来保存文件内容.殊不知这样按下去后面会造成整个终端不响应了,ssh连接还好说,直接关 ... 
- 服务器群秒级别文件同步(ssh+SHELL)
			1.介绍 \ 2.业务服务器远程更新浏览服务器文件的脚本 #!/bin/bash operate=$ ip=$ conf_file="/var/www/html/test/ip_list&q ... 
- Java实现验证码的产生和验证
			大家都知道为了防止我们的网站被有些人和黑客恶意攻击,比如我们网站的注册页面,如果我们在用户注册的时候不加上一个验证码框的话,别人就可以写一个脚本对你的网站进行恶意的注册,比如每分钟对你的网站进行n次的 ... 
- POJ 2240 - Arbitrage - [bellman-ford求最短路]
			Time Limit: 1000MS Memory Limit: 65536K Description Arbitrage is the use of discrepancies in currenc ... 
- Python函数-logging.basicConfig
			在我们写程序的时候需要记录日志信息,可以用到logging.basicConfig函数 import logging logging.basicConfig(level=logging.DEBUG, ... 
