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 ...
随机推荐
- WP8.1学习系列(第五章)——中心控件Hub或透视控件Pivot交互UX
具有主页菜单(中心或透视控件)的中心应用中心 你可能要设计包含许多功能的应用.当你看着这些功能时,可能会决定将它们整理到独立的区域中.这些区域最终会成为用户要访问的应用的独立部分.你需要设计一个简便的 ...
- 【laravel5.6】 Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes
在进行数据迁移时候报错: 特殊字段太长报错, php artisan migrate 现在utf8mb4包括存储emojis支持.如果你运行MySQL v5.7.7或者更高版本,则不需要做任何事情. ...
- Esper学习之十三:EPL语法(九)
距离上一篇博客已经有很多个月的时间了,由于工作的原因,没怎么腾出手来写博客.再加上自己已计划算法学习为第一要务,更新博客的事情暂时就放缓了脚步.所以各位童鞋大可不必等我的博客,先把文档看起来,有什么不 ...
- iPhone X的缺口和CSS
苹果公司(Apple)的发布会也开完了,新产品也将登陆了.估计很多开发人员看到iPhone X的设备是要崩溃了,特别对于前端开发人员更是如此. iPhone X的屏幕覆盖了整个手机的屏幕,为相机和其他 ...
- Simple Mail Transfer Protocol --- SMTP协议
https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol Simple Mail Transfer Protocol
- [原]linux下将网卡设置为混杂模式
设置为混杂模式ifconfig eth2 promisc取消设置ifconfig eth2 -promisc ------------------------------------------ 下面 ...
- 【咸鱼教程】TextureMerger1.6.6 三:Bitmap Font的制作和使用
BitmapFont主要用于特殊字体在游戏中的使用 目录 一 方法1:添加字符 适合一张一张的零碎图片来制作位图字体 二 方法2:系统字体 适合使用已安装的系统字体来制作位图字 ...
- iOS - 原生的CIFilter生成二维码和条形码
使用CIFilter可以不引入任何第三方库,就可以生成想要的二维码和条形码,今天简单的介绍一下使用CIFilter生成二维码和条形码.系统CIFilter生成的二维码和条形码的大小有时并不能满足需求, ...
- JavaEE Cookie HttpSession 学习笔记
1. 会话管理概述 1.1 什么是会话 好比一次通话.打开浏览器,点击多次链接(发出多次请求和收到多次的响应),关闭浏览器,这个过程就是一次会话. 有功能 可以 文件 新建会话 1.2 解决的问题是 ...
- div的最小宽高和最大宽高
div的最小宽高和最大宽高很少使用但是很实用,今天敲代码,就遇到了,要在div里设置滚动条,众所周知,一般是设overflow-y:auto,但需要一个高度,只有div里的内容超过这个高度时,才会有滚 ...