URAL 1026 Questions and answers

 [成绩]
ID Date Author Problem Language Judgement result Test # Execution time Memory used
2974561 14:02:37
29 Mar 2010
bake 1026 C++ Accepted
0.078 609 KB
2974560 14:02:13
29 Mar 2010
bake 1026 C++ Compilation error


[解题报告]
    首先,请忽略下面的那个CE……现在才发现DEV-CPP原来那么不爽……
    这道题的算法其实很简单……就是把输入的数先排好序,然后问一个答一个(这是O(NLOGN)的),当然那个在线算法是O(KN)的,但在这道题上,前者期望速度绝对比后者快(因为LOGN<=5<=100<=K).
[程序]
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <string>
#define N 100000
using namespace std;
long a[N+1];long n;
static inline void quick(long b,long e)
{
    long i=b,j=e;long x=a[rand()%(e-b+1)+b];
    while (i<=j)
    {
        while (a[i]<x) i++;
        while (a[j]>x) j--;
        if (i<=j)
        {
            long t=a[i];a[i]=a[j];a[j]=t;
            i++;j--;
        }
    }
    if (b<j) quick(b,j);
    if (i<e) quick(i,e);
}
int main(int argc, char *argv[])
{
     cin >> n;
    for (long i=1;i<=n;i++)
        cin >> a[i];
    quick(1,n);
    string xx;
    cin >> xx;
    long k;
    cin >> k;
    for (long i=1,b;i<=k;i++)
    {
         cin >> b;
        cout << a[b] << endl;
    }
//    system("PAUSE");
    return EXIT_SUCCESS;
}

posted @ 2010-03-29 16:00  为美好世界献上珂学  阅读(70)  评论(0)    收藏  举报