您的位置:首页 > 其它

hdu-5857 Median(水题)

2016-08-18 17:12 316 查看
题目链接:

Median

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 21 Accepted Submission(s): 4


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each query, output one line, the median of the query sequence, the answer should be accurate to one decimal point.

[align=left]Sample Input[/align]

1

4 2
1 2 3 4

1 2
2 4

1 1
2 2

[align=left]Sample Output[/align]

2.0
1.5

题意:

给以一个排好序的序列,m个询问,每个询问给两个区间,问用这两个区间的这些数组成的新序列的中位数是多少;

思路:

把区间分成三部分,其中一个是相交部分,然后找中位数就好了;

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;

template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=(1<<20)+14;
const double eps=1e-12;

int n,m,a
,l1,r1,l2,r2,l3,r3;

int solve(int pos)
{
if(r1>=l1)
{
if(r1-l1+1>=pos)return a[l1+pos-1];
else pos-=(r1-l1+1);
}
if(r3>=l3)
{
if(2*(r3-l3+1)>=pos)
{
if(pos&1)pos=pos/2+1;
else pos=pos/2;
return a[l3+pos-1];
}
else pos-=2*(r3-l3+1);
}
if(r2>=l2)
{
if(r2-l2+1>=pos)return a[l2+pos-1];
else pos-=(r2-l2+1);
}
}

int main()
{
int t;
read(t);
while(t--)
{
read(n);read(m);
For(i,1,n)read(a[i]);
while(m--)
{
read(l1);read(r1);
read(l2);read(r2);
int num=(r1-l1+1)+(r2-l2+1);
if(l1>l2)swap(l1,l2);
if(r1>r2)swap(r1,r2);
if(r1>=l2)
{
l3=l2,r3=r1;
r1=l3-1;
l2=r3+1;
}
else r3=-1,l3=0;
if(num&1)printf("%.1lf\n",solve(num/2+1)*1.0);
else printf("%.1lf\n",solve(num/2)*0.5+solve(num/2+1)*0.5);
}
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: