HDU 1157 Who's in the Middle (快速排序 or 任意排序)

Who‘s in the Middle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 9903    Accepted Submission(s): 4736

Problem Description

FJ is surveying his herd to find the most average cow. He wants to know how much milk this ‘median‘ cow gives: half of the cows give as much or more than the median; half give as much or less.

Given an odd number of cows N (1 <= N < 10,000) and their milk output (1..1,000,000), find the median amount of milk given such that at least half the cows give the same amount of milk or more and at least half give the same or less.

Input

* Line 1: A single integer N

* Lines 2..N+1: Each line contains a single integer that is the milk output of one cow.

Output

* Line 1: A single integer that is the median milk output.

Sample Input

5
2
4
1
3
5

Sample Output

3

Hint

INPUT DETAILS: 

Five cows with milk outputs of 1..5 

OUTPUT DETAILS: 

1 and 2 are below 3; 4 and 5 are above 3.

就是奇数个数字,求中位数,排序下,即可

#include<iostream>
#include<stdio.h>
#include<string.h>
int a[10010];
using namespace std;
void Swap(int *a,int *b){
    int t=*a;
    *a=*b;
    *b=t;
}
int partition(int a[],int l,int h){
    int v = a[l];
    int i=l;
    int j=h+1;
    while(true){
        while(a[++i]<v)if(i==h)break;
        while(v<a[--j])if(j==l)break;
        if(i>=j)break;
        Swap(&a[i],&a[j]);
    }
    Swap(&a[l],&a[j]);
    return j;
}
void quick_sort(int a[],int l,int h){
    if(h<=l)return ;
    int j=partition(a,l,h);
    quick_sort(a,l,j-1);//左边
    quick_sort(a,j+1,h);//右边
}
int main(int argc, char *argv[])
{
    //freopen("1157.in","r",stdin);
    int N;
    int i;
    while(scanf("%d",&N)!=EOF)
    {
        i=0;
        for(i=0;i<N;++i)
            scanf("%d",&a[i]);
        quick_sort(a, 0, N-1);
        printf("%d\n",a[N/2]);
    }
    return 0;
}

注意事项:partition一定要有返回值!!!别糊涂了,此外,partition确定后,排前面的,和后面的即可,即j-1,j+1

HDU 1157 Who's in the Middle (快速排序 or 任意排序)

时间: 2024-08-10 11:57:01

HDU 1157 Who's in the Middle (快速排序 or 任意排序)的相关文章

JavaScript 数据结构与算法之美 - 归并排序、快速排序、希尔排序、堆排序

1. 前言 算法为王. 想学好前端,先练好内功,只有内功深厚者,前端之路才会走得更远. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 JavaScript ,旨在入门数据结构与算法和方便以后复习. 之所以把归并排序.快速排序.希尔排序.堆排序放在一起比较,是因为它们的平均时间复杂度都为 O(nlogn). 请大家带着问题:快排和归并用的都是分治思想,递推公式和递归代码也非常相似,那它们的区别在哪里呢 ? 来阅读下文. 2. 归并排序(Merge Sort) 思想 排序一个数

POJ 2388 Who&#39;s in the Middle(水~奇数个数排序求中位数)

题目链接:http://poj.org/problem?id=2388 题目大意: 奇数个数排序求中位数 解题思路:看代码吧! AC Code: 1 #include<stdio.h> 2 #include<algorithm> 3 using namespace std; 4 int main() 5 { 6 int n; 7 while(scanf("%d",&n)!=EOF) 8 { 9 int na[n+1]; 10 for(int i=0; i

啊哈!算法之快速排序与桶排序

啊哈!算法之快速排序与桶排序 1.快速排序算法 快速排序由 C. A. R. Hoare(东尼·霍尔,Charles Antony Richard Hoare)在1960 年提出,之后又有许多人做了进一步的优化.在数列种随机找出一个基准数,因为数列是杂乱的,所以取首项为基准数.从后往前找到比基准数大的位置,再从前往后找到比基准数小的位置,交换元素:右游标向前移动与左游标向后移动,它们相遇时用基准数的位置与相遇的位置交换.此时原来数列以相遇的位置被划分为了两个需要排序的数列,再次执行上述过程:当左

排序:快速排序与选择排序

       在最近的学习中,对于排序算法进行了一定的学习,在这里对快速排序和选择排序的部分内容进行说明,其余内容在后期会进行补充,感谢大家提出宝贵意见. 宏定义如下: <strong><span style="font-size:18px;">#include <iostream> using namespace std; #define M 21 typedef int SqList[M];</span></strong>

算法有插入排序,堆排序,合并排序,快速排序和stooge排序

比较的算法有插入排序,堆排序,合并排序,快速排序和stooge排序, 先说一下比较结果 1,比较插入和stooge排序,stooge的表现如此之差,数组大小在2000时 InsertSort VS StoogeSort 's Running Time:     16ms:47672ms; Terribly! Isn't It? 所以在后面的比较中,没有带stooge这个垃圾算法 2,插入排序,堆排序,合并排序,快速排序运行时间对比 (网易博客的表格功能太差了,不爽,只好以文本对齐展现给大家了):

快速排序和计数排序API

听说有十大排序算法,先学习一下快速排序和计数排序算法. 一:快速排序 快速排序主要就是每一个找到当前取出来的标尺的数的位置,然后把小于它的数放在左边(从小到大排),把大于它的数放在右边.然后利用递归分左右继续找位置放数字,这个过程有点类似之前的根据前序和中序找后序的题目.递归的出口就是当只有一个数的时候就不需要排了.取出来的位置就是他的位置. 代码: 找每一次的位置: int findpos(int l,int r){ int temp = data[l]; int i = l, j = r;

C++线性表通过结构体实现操作和结构体字符串快速排序和shell排序结合

#include<iostream> #include<string> #define ml 10 using namespace std; typedef struct{//定义Data数据项 std::string name; long num; }Data; struct Link{//定义结构体 Data data[ml+1]; int length; }L; void initLink(Link *p){//初始化,即便有数据可以覆盖写入增加效率 p->length

HDU 1157

我竟然在这么水的题目上WA了一次,可是它没说要多组数据啊.我的英语啊!!!上题目: Problem Description FJ is surveying his herd to find the most average cow. He wants to know how much milk this 'median' cow gives: half of the cows give as much or more than the median; half give as much or l

快速排序和分治排序

快速排序让我看了很久,也折磨了我很长时间,因为大体上的思路我是有了,但是写代码时总是出现各种问题,要想把它调试出来对我个人来说还是有一定难度的,而且因为工作和偷懒的原因,导致之前调试不出来的错误放了很久,今天终于出来啦,还是有些小激动的哦,下面来分享一下我的代码并做一点点说明. 要学会快速排序,就必须先要学会分治法,分治的思想是给一串乱序的数字(数字是假设,也可以是其他的对象,当然方法的参数可以自己定义哦,我在这里假设有一个整型的数组吧)然后给他一个中间数,分治法会把这些数字以给他的那个是中间数