PAT甲题题解-1054. The Dominant Color (20)-排序/map
原本用map,发现超时了,后来便先用数组存储排个序,最后for一遍统计每种颜色出现的次数(每种颜色的首位索引相减+1),找出最多的即可。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <map>
using namespace std;
const int maxn=;
long long color[maxn]; int main()
{
int m,n;
scanf("%d %d",&m,&n);
long long a,ans;
int cnt=;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
scanf("%lld",&a);
color[cnt++]=a;
}
}
sort(color,color+cnt);
int tmp;
int last=;//每种颜色的第一个索引
int maxcnt=;
ans=color[];
color[cnt]=-;
for(int i=;i<=cnt;i++){
if(color[i]!=color[i-]){
tmp=i-last;
if(tmp>maxcnt){
maxcnt=tmp;
ans=color[i-];
}
last=i;
}
}
printf("%lld\n",ans);
return ;
}
然而没注意到题目中,说到出现最多的颜色肯定是大于一半区域的,其实用map映射来统计,只要有出现大于一半的直接输出然后break,是不会超时。
PAT甲题题解-1054. The Dominant Color (20)-排序/map的更多相关文章
- 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 ...
- PAT甲题题解-1011. World Cup Betting (20)-误导人的水题。。。
题目不严谨啊啊啊啊式子算出来结果是37.975样例输出的是37.98我以为是四舍五入的啊啊啊,所以最后输出的是sum+0.005结果告诉我全部错误啊结果直接保留两位小数就可以了啊啊啊啊 水题也不要这么 ...
- PAT甲题题解-1012. The Best Rank (25)-排序水题
排序,水题因为最后如果一个学生最好的排名有一样的,输出的课程有个优先级A>C>M>E那么按这个优先级顺序进行排序每次排序前先求当前课程的排名然后再与目前最好的排名比较.更新 至于查询 ...
- PAT甲题题解-1019. General Palindromic Number (20)-又是水题一枚
n转化为b进制的格式,问你该格式是否为回文数字(即正着写和倒着写一样)输出Yes或者No并且输出该格式又是水题... #include <iostream> #include <cs ...
- PAT甲题题解-1027. Colors in Mars (20)-水。。。
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
- PAT甲题题解-1062. Talent and Virtue (25)-排序水题
水题,分组排序即可. #include <iostream> #include <cstdio> #include <algorithm> #include < ...
- PAT甲题题解-1095. Cars on Campus(30)-(map+树状数组,或者模拟)
题意:给出n个车辆进出校园的记录,以及k个时间点,让你回答每个时间点校园内的车辆数,最后输出在校园内停留的总时间最长的车牌号和停留时间,如果不止一个,车牌号按字典序输出. 几个注意点: 1.如果一个车 ...
- PAT甲题题解-1128. N Queens Puzzle (20)-做了一个假的n皇后问题
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789810.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲题题解-1001. A+B Format (20)-字符串处理,水
计算A+B的和,并且按标准格式处理,每3个就要有个逗号 #include <iostream> #include <cstdio> #include <algorithm ...
随机推荐
- 开源作业调度框架 - Quartz.NET - ASP.NET部署
经过这次使用实践,感觉Quartz.NET使用起来方便快捷 但是在发布部署时我们会遇到一个问题 那就是当Web应用程序经常没有按照预计的时间去执行. 那问题出在哪里了呢? 根据以往的经验很容易就可以找 ...
- November 15th, 2017 Week 46th Wednesday
Of all the tribulations in this world, boredom is the one most hard to bear. 所有的苦难中,无聊是最难以忍受的. When ...
- tcp尽快尽快了
看的撒积分卡拉斯的放假啊的顺口溜
- ubuntu 视频播放问题
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/chang_xing/article/details/30976659 ...
- linux 的常用命令---------第九阶段
Centos 7 系统启动及相关配置文件(面试题) 1. BIOS 初始化,开始post开机自检(主要检查磁盘.cpu.内存) 2. 加载 MBR 到内存 3. GRUB 阶段(可不说) 4. 加载内 ...
- C#中使用WeiFenLuo.WinFormsUI.Docking.dll实现窗口停靠效果
很酷的效果,很值得好好去学习的哈. 重置工具箱: 新建一个WinForm程序,项目名称为TestDockPanelControl.选中Form1窗体后选择工具箱--->>新建个添加选项卡命 ...
- Error at offset之反序列化
关于PHP 序列化(serialize)和反序列化(unserialize)出现错误(Error at offset)的解决办法. 首先我们分析一下为什么会出现这个错误: 编码问题 UTF-8: AN ...
- jsp运算符
一.执行运算: 支持四则运算.关系(>;<....),逻辑运算(&&.||) 注意: +:只能进行加法运算,字符串形式的数字可以进行加法运算,这里的+号不能进行字符串的拼接 ...
- Base64Util 工具类
package com.org.utils; import java.io.ByteArrayOutputStream; public class Base64Util { private stati ...
- 【H5】dropload (移动端下拉刷新,上拉加载)
插件概要地址:http://ximan.github.io/dropload/ 一般下载其中的demo2对照修改即可使用. 小吐槽.我在项目中用的时候,有个后端说ajax麻烦但是还是要做体现他很热爱工 ...