题解G

Posted on 2014-07-16 14:37  alexanderone  阅读(116)  评论(0编辑  收藏  举报

For each test case, the first line contains a single integer n (1 <= n <= 100,000) indicating the total number of problems. The second line contains n integers xi (0 <= xi <= 1,000,000,000), separated by single space, denoting the difficulties of each problem, already sorted by publish time. The next line is a single integer m (1 <= m <= 100,000), specifying number of queries. Then m lines follow, each line contains a pair of integers, A and B (1 <= A <= B <= n), denoting that Mr. B needed to choose a problem between positions A and B(inclusively, positions are counted from 1). It is guaranteed that the number of items between A and B is odd.

Output

For each query, output a single line containing an integer which denotes the difficulty of the problem that Mr. B should choose.

Example

Input: 5
5 3 2 4 1
3
1 3
2 4
3 5
5
10 6 4 8 2
3
1 3
2 4
3 5 Output: Case 1:
3
3
2
Case 2:
6
6
4
废话连篇的题目,就是排序问题。可能快排的方法不够优化,超时三次没过。

#include<stdio.h>
#include<iostream>
using namespace std;
void qsort(int left ,int right, long long a[])
{
int i , j, x;
if(right<left+2){
if(a[left]>a[right])
swap(a[left], a[right]);
return ;
}
i = left, j = right, x = a[right];
while(i<j){
while(a[i]<x && i<j) i++;
while(a[j]>=x && i<j) j--;
swap(a[i], a[j]);
}
swap(a[i], a[right]);
if(left<i-1) qsort(left, i-1, a);
if(i+1<right) qsort(i+1, right,a);
}

int main()
{
int n,i,m,a,b,c,y,p;
long long x[100005];
long long z[100005];
p=1;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
{
scanf("%lld",&x[i]);
}
for(i=1;i<=n;i++)
{
z[i]=x[i];
}
scanf("%d",&m);
printf("Case %d:\n",p);
for(a=1;a<=m;a++)
{
scanf("%d %d",&b,&c);
for(i=b;i<=c;i++)
{
z[i]=x[i];
}
qsort(b,c,z);
y=(b+c)/2;
printf("%d\n",z[y]);
}
p++;
}
}

出来的结果没问题,需要优化。。。。

Copyright © 2024 alexanderone
Powered by .NET 8.0 on Kubernetes