杭电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?
"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); }}
阅读全文
0 0
- 杭电ACM OJ 1029 Ignatius and the Princess IV 快速排序 挖坑填埋法
- 杭电OJ 1029:Ignatius and the Princess IV
- 杭电OJ(HDOJ)1029题:Ignatius and the Princess IV(数论)
- 杭电 1029 Ignatius and the Princess IV
- 杭电 HDU 1029 Ignatius and the Princess IV
- c语言杭电1029Ignatius and the Princess IV
- 杭电1029 Ignatius and the Princess IV
- 杭电HDOJ 1029 Ignatius and the Princess IV
- 杭电1029 之 Ignatius and the Princess IV
- hdu-oj 1029 Ignatius and the Princess IV
- Hdu oj 1029 Ignatius and the Princess IV
- HDU ACM 1029-Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV 水题排序
- 杭电ACM OJ 1026 Ignatius and the Princess I DFS+BFS
- 杭电ACM OJ 1028 Ignatius and the Princess III 母函数+优化
- 杭电 1029 Ignatius and the Princess IV (技巧数学题)
- 杭电OJ 1028:Ignatius and the Princess III
- 杭电OJ 1027:Ignatius and the Princess II
- XGBoost解析系列--源码主流程
- 库记录
- kubernetes中 demo: tomcat + mysql
- 基于jQuery的三级联动控件
- jqGrid
- 杭电ACM OJ 1029 Ignatius and the Princess IV 快速排序 挖坑填埋法
- JS事件流,事件绑定
- PTA_6-6 Deque(25 分)手动实现双端队列
- 【离散数学】2.1什么是命题
- getWriter() has already been called for this response错误
- eclipse安装php报错
- 分布式事物解决方案含视频教程
- Linux下多网卡绑定bonding bond6
- 转移表