杭电ACM OJ 1029 Ignatius and the Princess IV 快速排序 挖坑填埋法

来源:互联网 发布:外汇行情数据接口 编辑:程序博客网 时间:2024/05/29 02:17

Ignatius and the Princess IV

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K (Java/Others)
Total Submission(s): 36359    Accepted Submission(s): 15863


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
51 3 2 3 3111 1 1 1 1 5 5 5 5 5 571 1 1 1 1 1 1
 

Sample Output
351
翻译:这一题就是先给你一个质数,比如5,再给你一个序列,比如1 3 2 3 3.
你的任务是找到出现次数大于等于 5+1/2 = 3的数,自然就是3了。
做法:因为出现次数大于N+1/2,那么自然排好序后,数组的中间的数肯定是我们要求的那个数。这是为什么呢?因为他的出现次数是大于N+1/2的。假设我们的中间不是我们要求的那个数,那么这一串数在左边?在右边?那肯定不满足长度大于n+1/2了。
我们这里选择的是快速排序。
快速排序用java的List做很方便,用数组做也不麻烦。可以看一下http://blog.csdn.net/qq_36523667/article/details/78699581。讲的非常非常好。干脆利落地讲明白了快排。
全部代码(包含了两种方法):
public class IgnatiusAndThePrincessFour1029 {    IgnatiusAndThePrincessFour1029(List<Integer> list) {        int size = list.size();//        calculate(list, 0, size - 1);////        for (int i = 0; i < size; i++) {//            System.out.print(list.get(i) + " ");//        }        int[] a = new int[size];        for (int i = 0; i < size; i++) {            a[i] = list.get(i);        }        calculate(a, 0, a.length - 1);    }    // 136284008List实现太轻松了    void calculate(List<Integer> list, int startIndex, int endIndex) {// origin index 0 size-1        if (startIndex == endIndex || startIndex + 1 == endIndex) {            return;        }        int firstNum = list.get(startIndex);        int index = startIndex;        for (int i = startIndex + 1; i <= endIndex; i++) {            int num = list.get(i);            if (num < firstNum) {                list.add(startIndex, num);                list.remove(i + 1);                index++;            }        }        calculate(list, startIndex, index - 1);        calculate(list, index + 1, endIndex);    }    //数组实现    void calculate(int a[], int startIndex, int endIndex) {        if (startIndex == endIndex || startIndex + 1 == endIndex || startIndex > endIndex) {            return;        }        int x = a[startIndex];        int ken = startIndex;        int s = startIndex;        int e = endIndex;        startIndex ++;        boolean flag = true;        while (startIndex <= endIndex) {            if (flag) {                if (x > a[endIndex]) {                    a[ken] = a[endIndex];                    ken = endIndex;                    flag = false;                }                endIndex --;//0            } else {                if (x < a[startIndex]) {                    a[ken] = a[startIndex];                    ken = startIndex;                    flag = true;                }                startIndex ++;            }        }        a[ken] = x;        calculate(a, s, ken - 1);        calculate(a, ken + 1, e);    }    static List<Integer> initList(Integer... integers) {        List<Integer> list = new ArrayList<>();        Collections.addAll(list, integers);        return list;    }    public static void main(String[] args) throws Exception {        List<Integer> list = initList(1, 3, 6, 2, 8, 4, 0, 0, 8);        IgnatiusAndThePrincessFour1029 i = new IgnatiusAndThePrincessFour1029(list);    }}

原创粉丝点击