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);
        }
    }
    超时
    */
}
时间: 2024-09-21 12:33:52

HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)的相关文章

hdu 1028 Ignatius and the Princess III (母函数)

点击打开链接 Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16394    Accepted Submission(s): 11552 Problem Description "Well, it seems the first problem is too easy. I

HDOJ/HDU 2537 8球胜负(水题.简单的判断)

Problem Description 8球是一种台球竞赛的规则.台面上有7个红球.7个黄球以及一个黑球,当然还有一个白球.对于本题,我们使用如下的简化规则:红.黄两名选手轮流用白球击打各自颜色的球,如果将该颜色的7个球全部打进,则这名选手可以打黑球,如果打进则算他胜.如果在打进自己颜色的所有球之前就把黑球打进,则算输.如果选手不慎打进了对手的球,入球依然有效. 现在给出打进的球(白球除外)的顺序,以及黑球由哪方打进,你的任务是判定哪方是胜者. 假设不会有一杆同时打进一颗黑球和其他彩球. Inp

HDOJ 1028 Ignatius and the Princess III(递推)

Problem Description "Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says. "The second problem is, given an positive integer N, we define an equation like this: N=a[1]+a[2]+a[3]+-+a[m];

HDOJ/HDU 1161 Eddy&amp;#39;s mistakes(大写字母转换成小写字母)

Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for example "computer science" is written frequently "coMpUtEr scIeNce" by him, this mistakes lets Eddy's English teacher be extremely disco

HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)

Problem Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more t

HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)

Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 注意:本题只有一组测试数据,处理到文件结束. Output 对于每个提

HDOJ/HDU 2352 Verdis Quo(罗马数字与10进制数的转换)

Problem Description The Romans used letters from their Latin alphabet to represent each of the seven numerals in their number system. The list below shows which letters they used and what numeric value each of those letters represents: I = 1 V = 5 X

HDOJ/HDU 1022 Train Problem I(模拟栈)

Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes

HDOJ(HDU) 2061 Treasure the new start, freshmen!(水题、)

Problem Description background: A new semester comes , and the HDU also meets its 50th birthday. No matter what's your major, the only thing I want to tell you is:"Treasure the college life and seize the time." Most people thought that the colle