HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)
此题无法用JavaAC,不相信的可以去HD1029题试下!
Problem Description
“OK, you are not too bad, em… But you can never pass the next test.” feng5166 says.
“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?
Input
The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.
Output
For each test case, you have to output only one line which contains the special number you have found.
Sample Input
5
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
Sample Output
3
5
1
题意:就是在一行数中找出那个出现次数大于等于(n+1)/2的那个数,题目保证那个数只有一个!
此题有个坑,用Java无法AC,无论用桶排序,快排,还是DP都无法AC。会超时!
简单题,就不分析了。此处把Java代码也写上了。
AC的c语言代码:(后面有Java的(3种方法都用了))
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
while(~scanf("%d",&n)){
int i;
int con=0;
int m;
int t;
for(i=0;i<n;i++){
scanf("%d",&t);
if(con==0){
m=t;
con++;
}else{
if(t==m){
con++;
}else{
con--;
}
}
}
printf("%d\n",m);
}
return 0;
}
Java的超时代码:3种方法!
package cn.hncu.acm;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author 陈浩翔
* @version 1.0 2016-6-18
*/
//总结:此题无法用Java在2000ms内AC。 测试数据过多,输入流需要耗费太多时间。
public class P1029 {
/*
//第一种方法 排序后判断
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n =sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
Arrays.sort(a);
System.out.println(a[(n+1)/2]);
}
}
//超时
*/
/*
//第二种方法 DP
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n=sc.nextInt();
int con=0;
int result=0;
for(int i=0;i<n;i++){
int m = sc.nextInt();
if(con==0){
result=m;
con++;
}else{
if(m==result){
con++;
}else{
con--;
}
}
}
System.out.println(result);
}
}
超时
*/
/*
//桶排序
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n=sc.nextInt();
int a[] = new int[500000];
int m=0;
int t=0;
for(int i=0;i<n;i++){
t=sc.nextInt();
a[t]++;
if(a[t]>=(n+1)/2){
m=t;
}
}
System.out.println(m);
}
}
超时
*/
}
HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)的更多相关文章
- Ignatius and the Princess IV (简单DP,排序)
方法一: 直接进行排序,输出第(n+1)/2位置上的数即可. (容易超时,关闭同步后勉强卡过) #include<iostream> #include<cstdio> # ...
- HDU 1029 Ignatius and the Princess IV --- 水题
HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...
- HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)
HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...
- HDU 1029 Ignatius and the Princess IV (map的使用)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1029 Ignatius and the Princess IV Time Limit: 2000/10 ...
- hdu 1029 Ignatius ans the Princess IV
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- [ACM] hdu 1029 Ignatius and the Princess IV (动归或hash)
Ignatius and the Princess IV Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32767K (Ja ...
- HDU 1029 Ignatius and the Princess IV (动态规划、思维)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- hdu 1028 Ignatius and the Princess III 简单dp
题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...
- HDU 1029 Ignatius and the Princess IV
解题报告: 题目大意:就是要求输入的N个数里面出现的次数最多的数是哪一个,水题.暴力可过,定义一个一位数组,先用memset函数初始化,然后每次输入一个数就将下标对应的上标对应的那个数加一,最后将整个 ...
随机推荐
- weblogic 12c 配置jvm的内存大小
每个weblogic server 都是运行在一个java虚拟机上 ,对weblogic的内存设置也就是对java虚拟机的内存设置. MEM_ARGS=-Xms512m -Xmx1024m -XX:M ...
- 网站开发常用jQuery插件总结(12)固定元素插件scrolltofixed
这个插件在前段时间用过一次,当时是改一个网站.要求顶部的菜单栏随着滚动条的滚动而固定.也大体写了一下,不过在文章中也只是提了一下,文章地址:jQuery插件固定元素位置. 在这篇文章中,再进行总结一下 ...
- Json 数组排序
/*********************************************Json 数组排序 ******************************************** ...
- HTML实体
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 新开窗口不被拦截的方法-window.open和表单提交form
$("#btn").click(function() { var w = window.open(); setTimeout(function() { w.location = & ...
- js与uri中location关系
//获取域名host = window.location.host;host2=document.domain; //获取页面完整地址url = window.location.href; docum ...
- HTML5 改良的 input 元素的种类
html5中增加改良的input 元素 . 在过去我们制作网页输入框,会用到不少JS验证,如今有了HTML5写这种效果已经没有那么麻烦了,下面我来给大家介绍两种HTML5的input的新增加的类型应用 ...
- 怎样清除td和input之间空隙
<style> input {background:red;border:none;height:30px;margin:0px} td {background-color:blue;pa ...
- 要将表的限制条件写到与该表同级别的where中
测试目的:将朱查询的限制条件放到子查询的where中,查看性能影响. 测试数据:create table t1 as select object_id,object_name from dba_obj ...
- 在ubuntu12.0.4上搭建samba服务器以实现文件共享
在安装之前samba服务器之前,先进行以下配置和测试. <壹> 准备工作 一.NAT联网方式 (1)硬件连接 无需网线,无需路由器 (2)虚拟机选择NAT连接方式 (3)测试网络通不通 在 ...