kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)
Ignatius and the Princess IV
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K (Java/Others)
Total Submission(s): 43629 Accepted Submission(s): 19213
"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.
"But what is the characteristic of the special integer?" Ignatius asks.
"The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.
Can you find the special integer for Ignatius?
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1
5
1
被专题的第一题吓到了,以为还是一道难题,就复杂考虑了。排序,然后
dp[i] = (a[i] == a[i-1]) ? dp[i-1] + 1 : 1
后来才发现必定有解,直接输出就可以了
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = ;
int a[maxn];
int dp[maxn]; int main() {
freopen("in.txt", "r", stdin);
int n;
while(~scanf("%d", &n)) {
memset(dp, , sizeof(dp));
for(int i = ; i < n; i++) {
scanf("%d", &a[i]);
}
if(n == ) {//特判了一下
printf("%d\n", a[]);
continue;
}
sort(a, a+n);
dp[] = ;
int maxdp = dp[], pos = -;
for(int i = ; i < n; i++) {
dp[i] = (a[i] == a[i-]) ? dp[i-] + : ;
if(dp[i] > maxdp) {
maxdp = dp[i];//最大值的dp,对应出现最多的数
pos = i;
}
}
printf("%d\n", a[pos]);
}
}
kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)的更多相关文章
- HDU1029 Ignatius and the Princess IV (水题)
<题目链接> 题目大意:给你一段序列,问你在这个序列中出现次数至少为 (n+1)/2 的数是哪个. 解题分析: 本题是一道水题,如果用map来做的话,就非常简单,但是另一个做法还比较巧妙. ...
- HDU 1029 Ignatius and the Princess IV --- 水题
HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...
- kuangbin专题十二 POJ3186 Treats for the Cows (区间dp)
Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7949 Accepted: 42 ...
- kuangbin专题十二 POJ1661 Help Jimmy (dp)
Help Jimmy Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14214 Accepted: 4729 Descr ...
- kuangbin专题十二 HDU1176 免费馅饼 (dp)
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- kuangbin专题十二 HDU1074 Doing Homework (状压dp)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)
FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- kuangbin专题十二 HDU1260 Tickets (dp)
Tickets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- kuangbin专题十二 HDU1114 Piggy-Bank (完全背包)
Piggy-Bank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
随机推荐
- Struts2 - ModelDriven 拦截器、Preparable 拦截器
开篇:拦截器在Struts中的作用 在我们的web.xml中,我们配置了一个过滤器,实现将所有请求交付StrutsPrepareAndExecuteFilter类.一旦接受到任意action的请求,该 ...
- [原]NYOJ-组合数-32
大学生程序代写 http://acm.nyist.net/JudgeOnline/problem.php?pid=32 /*组合数 时间限制:3000 ms | 内存限制:65535 KB 难度: ...
- ONVIF测试方法及工具
设备是否支持ONVIF验证1 ONVIF Test Tool安装1.1PC安装环境要求:装有Microsoft .Net Framework 3.5或以上版本.1.2安装源文件请见:ONVIF Con ...
- Agc011_C Squared Graph
传送门 题目大意 给定$n$个点$m$条边的简单图(无重边无自环),将有序点对$\{a,b\}$作为新的点,新产生的$n^2$个点中对于两个点,$\{a,b\},\{x,y\}$,当且仅当原图中存在边 ...
- bzoj3595 方伯伯的oj
有$n$个数,一开始是$1~n$,有$m$次操作 1.把编号为$x$的人编号改为$y$,保证$y$没出现过 2.把编号为$x$的人提到第一名 3.把编号为$x$的人怼到最后一名 4.查询排名为$x$的 ...
- WPF中Xaml编译正常而Designer Time时出错的解决办法
开发wpf时我们经常遇到一个xaml文件在设计时显示解析错误(比如在:VS或者Blend)而编译正常运行正常. 原因是:xaml的在Debug版本下必须为anyCPU. 解决办法: 1.打开工程文件x ...
- HDOJ1150(最小点集覆盖)
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> ...
- druid数据源的加密解密工具
数据库得加密 先来一个网上大多数的教程吧,一个比较好的教程,如下. jar包版本:druid-1.0.13.jar 1. 加密,用以下命令将用户名和密码加密 cmd命令行执行 java -cp D:/ ...
- Python:easygui的安装、导入、使用、设置
转于:https://blog.csdn.net/sinat_37390744/article/details/55211652 博主:钏的博客 一.下载安装 1)下载0.96的easygui.htt ...
- Android audioManager
Android audioManager AudioManager provides access to volume and ringer mode control. 获取对象 Use Contex ...