poj 2623 快排

简介:

一、题目大意

就是求中间的数。

二、AC code

递归快排ac版:

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <cassert>
#include <time.h>
#include <queue>
#include <map>
#include <stack>
#include <bitset>
#include <string>
#include <sstream>
#include <list>
#define INF 0x3f3f3f3f

using namespace std;

template <class Type>
Type stringToNum(const string& str)
{
    istringstream iss(str);
    Type num;
    iss >> num;
    return num;    
}

//======================================================

#define MAXN 250002

int a[MAXN];

void quickSort(int *arr, int left, int right){
    int i = left, j = right;
    int mid = arr[(i+j)/2];
    while(i <= j){
        while(arr[i] < mid) i ++;
        while(arr[j] > mid) j --;
        if(i <= j){
            int tmp;
            tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp;
            i ++; j --;
        }
    }
    if(i < right) quickSort(arr,i, right);
    if(left < j) quickSort(arr,left, j);
}

int main()
{
    //freopen("input.txt","r",stdin);

    int N;
    cin>>N;

    for (int i = 1; i <= N; ++i) {
        scanf("%d",&a[i]);
    }

    quickSort(a,1,N);

    if(N%2) //odd
        printf("%d.0\n", a[(N+1)/2]);
    else {
        double sum = (double)a[N/2] + (double)a[N/2+1];
        printf("%.1lf\n", sum / 2);
    }

    return 0;
}

可以水过,但是不知道为什么用g++也会WA,用c++ OK
同时因为数的范围过大,用计数排序不靠谱。

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <cassert>
#include <time.h>
#include <queue>
#include <map>
#include <stack>
#include <bitset>
#include <string>
#include <sstream>
#define INF 0x3f3f3f3f

using namespace std;

template <class Type>
Type stringToNum(const string& str)
{
    istringstream iss(str);
    Type num;
    iss >> num;
    return num;    
}

//======================================================

#define MAXN 250002

long long a[MAXN];

int main()
{
    //freopen("input.txt","r",stdin);

    int N;
    cin>>N;

    for (int i = 1; i <= N; ++i) {
        scanf("%lld",&a[i]);
    }

    sort(a+1,a+N+1);

    //odd
    if(N%2)
        printf("%lld.0\n", a[(N+1)/2]);
    else {
        //printf("%.1lf\n", ((double)a[N/2]+(double)a[(N/2)+1])/2); //ok

        //ok
//      double sum = (double)a[N/2]+(double)a[(N/2)+1];
//      printf("%.1lf\n",sum / 2);

        printf("%.1lf\n", (double)(a[N/2]+a[(N/2)+1])/2);
    }

    return 0;
}

stl二分会超时:

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <cassert>
#include <time.h>
#include <queue>
#include <map>
#include <stack>
#include <bitset>
#include <string>
#include <sstream>
#include <list>
#define INF 0x3f3f3f3f

using namespace std;

template <class Type>
Type stringToNum(const string& str)
{
    istringstream iss(str);
    Type num;
    iss >> num;
    return num;    
}

//======================================================

#define MAXN 250002

vector<int > v;

//有序数组递减排列
int  binarySearch(vector<int > array,int len,int value){
    int mid=0;
    int low=0;
    int high=len-1;
    while(low<=high){
        mid=(low+high)/2;
        if(array[mid]>value){       //在右半区
            low=mid+1;
            continue;
        }
        else if(array[mid]<value){  //在左半区
            high=mid-1;
            continue;
        }else
            return mid;             //找到
    }
    return low;  //insert pos
}

int main()
{
    //freopen("input.txt","r",stdin);

    int N;
    cin>>N;

    for (int i = 1; i <= N; ++i) {
        long long tmp;
        scanf("%lld",&tmp);

        int pos = binarySearch(v,i-1,tmp);
        v.insert(v.begin() + pos, tmp);
    }


    if(N%2) //odd
        printf("%lld.0\n", v[(N+1)/2-1]);
    else
        printf("%.1lf\n", (double)(v[N/2-1]+v[N/2])/2);

    return 0;
}
相关文章
poj 1990 MooFest 树状数组
题意就是有N头牛,每头牛都有一个坐标和声调值(x, v),两头牛之间通讯要花费的能量是他们的距离乘以最大的一个音调值,现在要任意两头牛之间都相互通讯一次,求总共需要花费多少能量?
52 0
|
人机交互
POJ-2524,Ubiquitous Religions(并查集模板题)
POJ-2524,Ubiquitous Religions(并查集模板题)
POJ-2253,Frogger(最短路问题)
POJ-2253,Frogger(最短路问题)
|
人工智能
POJ 2370 Democracy in danger(简单贪心)
Democracy in danger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3388   Accepted: 2508 Description In one of the...
957 0
poj supermaket (贪心)
http://poj.org/problem?id=1456 #include #include #include using namespace std; struct nod { int a; int d; }; bool cmp(nod x,nod y) { return x.
699 0
|
文件存储
poj 2229 Sumsets 【动态规划】
点击打开题目 Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 13291   Accepted: 5324 Description Far...
925 0
并查集-poj-1182
poj-1182-食物链 //2014.4.11 HDOJ携程编程大赛预赛第二场第一题 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形。A吃B, B吃C,C吃A。  现有N个动物,以1-N编号。每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种。  有人用两种说法对这N个动物所构成的食物链关系进行描述:  第一种说法是"1
1041 0