Median
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1563 Accepted Submission(s): 417
Problem Description
There is a sorted sequence A of length n. Give you m queries, each one contains four integers, l1, r1, l2, r2. You should use the elements A[l1], A[l1+1] ... A[r1-1], A[r1] and A[l2], A[l2+1] ... A[r2-1], A[r2] to form a new sequence, and you need to find the median of the new sequence.
Input
First line contains a integer T, means the number of test cases. Each case begin with two integers n, m, means the length of the sequence and the number of queries. Each query contains two lines, first two integers l1, r1, next line two integers l2, r2, l1<=r1 and l2<=r2.
T is about 200.
For 90% of the data, n, m <= 100
For 10% of the data, n, m <= 100000
A[i] fits signed 32-bits int.
T is about 200.
For 90% of the data, n, m <= 100
For 10% of the data, n, m <= 100000
A[i] fits signed 32-bits int.
Output
For each query, output one line, the median of the query sequence, the answer should be accurate to one decimal point.
Sample Input
1 4 2 1 2 3 4 1 2 2 4 1 1 2 2
Sample Output
2.0 1.5
Author
BUPT
Source
Recommend
wange2014
题意:给你一个有序序列,有q次询问,每次给出l1,r1和l2,r2,序列l1~r1和序列l2~r2形成一个新序列,求出新序列的中间值
解题思路:暴力判断
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int n,m,l1,l2,r1,r2;
LL a[100009];
void get(int p,int &x)
{
if(l2>r1)
{
if(p+l1-1<=r1) x=p+l1-1;
else x=p-(r1-l1+1)+l2-1;
}
else if(r1<=r2)
{
if(p<=l2-l1) x=p+l1-1;
else if(p>r1-l1+1+r1-l2+1) x=p-(r1-l1+1+r1-l2+1)+r1;
else x=(p-(l2-l1)+1)/2+l2-1;
}
else
{
if(p<=l2-l1) x=p+l1-1;
else if(p>r2-l1+1+r2-l2+1) x=p-(r2-l1+1+r2-l2+1)+r2;
else x=(p-(l2-l1)+1)/2+l2-1;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++) scanf("%lld",&a[i]);
while(m--)
{
scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
if(l1>l2)
{
swap(l1,l2);
swap(r1,r2);
}
int k=(r1-l1+1),kk=(r2-l2+1),x,xx;
if((k+kk)%2==1)
{
int p=(k+kk+1)/2;
get(p,x);
printf("%.1lf\n",(double)a[x]);
}
else
{
int p=(k+kk)/2;
int pp=p+1;
get(p,x),get(pp,xx);
printf("%.1lf\n",(double)(a[x]+a[xx])/2.0);
}
}
}
return 0;
}