Median

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

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.
 
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   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 
题意:给出一个有序的数列.
求由 A[l1]~A[r1] 与 A[l2]~A[r2] 组成的新序列的中位数.
代码:
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define MAX 100005
int l1,l2,r2,r1;
int n,m;
int a[MAX];
int solve(int len)
{
if(r1<=l2)
{
if(r1-l1+>=len) return a[l1+len-];
else{
len-=r1-l1+;
return a[l2+len-];
}
}
else{
if(l2-l1>=len)
{
return a[l1+len-];
}
else if((l2-l1+*(r1-l2+))<len){
len-=(l2-l1+*(r1-l2+));//注意这里的括号没加就变成len-l2+l1-2*(r1-l2+1)
return a[r1+len];
}
else{
len-=l2-l1;
if(len&)
{
return a[l2+len/];
}
else{
return a[l2+len/-];
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
while(m--){
scanf("%d %d %d %d",&l1,&r1,&l2,&r2);
if(l1>l2)swap(l1,l2);
if(r1>r2)swap(r1,r2);
int len=(r2-l2+)+(r1-l1+);
if(len&)
{
printf("%.1f\n",1.0*solve(len/+));
}
else printf("%.1f\n",0.5*solve(len/)+0.5*solve(len/+));
}
}
}
 

hdu5857 Median(模拟)的更多相关文章

  1. HDU5857 Median 模拟

    Median HDU - 5857 There is a sorted sequence A of length n. Give you m queries, each one contains fo ...

  2. hdu-5857 Median(水题)

    题目链接: Median Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  3. POJ 3784 Running Median (模拟水过带翻译)

    Description Moscow is hosting a major international conference, which is attended by n scientists fr ...

  4. Codeforces Round #577 (Div. 2) C. Maximum Median (模拟,中位数)

    题意:给你一个长度为奇数\(n\)的序列.你可以对任意元素加上\(k\)次\(1\),求操作后的中位数最大. 题解:先对序列进行排序,然后对中位数相加,如果中位数和后面的元素相等,就对后面所有和当前中 ...

  5. Codeforces Round #550 (Div. 3) E. Median String (模拟)

    Median String time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  6. 【HDU5857】Median

    题意 给出一个长度为n的有序序列.给出m个询问,每个询问包括四个正整数l1,r1,l2,r2你用l1tor1的和l2tor2的元素来组成一个新的序列,然后找出这个序列的中位数. 分析 这是当时Spri ...

  7. median(NOIP模拟赛Round 3)

    也是神奇的题目.. 原题传送门 首先看到这道题目,很明显我们需要线性算法 1.前缀和+统计 2.DP+统计 对于第一种算法,我们可以对于任何一个a[i]对b进行比较,如果大于b标上1,等于b标上0,小 ...

  8. 10.7 csp-s模拟测试63 Median+Game+Park

    我堕落了 我觉得任牛逼的问题也是我的问题 T1 Median T2 Game T3 Park

  9. [CSP-S模拟测试]:Median(暴力+模拟)

    题目描述 定义两个数列: $$S=\{S(1),S(2),...,S(n)\}\text{和}S_2\{S_2(1),S_2(2),...,S_2(n)\}$$ $$S(k)=(p_k\times k ...

随机推荐

  1. Sass:@error

    @error 和 @warn.@debug 功能是如出一辙. @mixin error($x){ @if $x < 10 { width: $x * 10px; } @else if $x == ...

  2. 覆盖(Override)和重写(Overload)的区别

    Overload Overload是重载的意思. 重载Overload表示同一个类中可以有多个名称相同的方法,但这些方法的参数列表各不相同(即参数个数或类型不同). Overload对我们来说可能比较 ...

  3. 使用rabbitctl添加用户

    使用rabbitctl添加用户 第一.添加mq用户并设置密码 root@live-mq-01:~ # rabbitmqctl add_user mq 123456 1 root@live-mq-01: ...

  4. Python---基础---list(列表)

    2019-05-20 一. # append()  向列表末尾追加新元素   返回值Nonelist1 = [1,2,3,4,5]print(id(list1))list1.append(6)prin ...

  5. 【leetcode】1081. Smallest Subsequence of Distinct Characters

    题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...

  6. @property属性装饰器

    顾名思义,@property就是一个跟属性相关的装饰器, 使用了它之后,取值和赋值操作都变得简洁 from datetime import date, datetime class User: def ...

  7. 解决kubernetes拉取不了镜像仓库的问题

    将镜像仓库地址k8s.gcr.io改成registry.aliyuncs.com/google_containers

  8. Oulipo (poj3461

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29759   Accepted: 11986 Descript ...

  9. Zball in Tina Town

    Zball in Tina Town  Accepts: 356  Submissions: 2463  Time Limit: 3000/1500 MS (Java/Others)  Memory ...

  10. 微信小程序填坑之路其一:wx.request发送与服务端接受

    一.序言 应公司要求要求,要用小程序开发一个信息录入系统.没办法只能听话来填坑. 先介绍一下环境:客户端——小程序:服务端——java:数据库——mysql:服务器——centos7 需求:客户端输入 ...