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. 树莓派 msmtp和mutt 的安装和配置

    1,安装mutt sudo apt-get install mutt 2,安装msmtp sudo apt-get install msmtp 3,设置mutt /etc/Muttrc # 系统全局设 ...

  2. 2018-09-20-weekly

    Algorithm 最长有效括号 What 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. How 这里可以用栈来求解,需要定义个start变量来记录合法括号串的起 ...

  3. [python 学习] 使用 xml.etree.ElementTree 模块处理 XML

    ---恢复内容开始--- 导入数据(读文件和读字符串) 本地文件 country_data.xml <?xml version="1.0"?> <data> ...

  4. git & gerrit & shell

    g公司使用Gerrit改善评审流程. 比较麻烦.gerrit提交后会触发vertifyCI, 实施代码扫描. 这一堆过程, 打印出一堆信息, 都在log中, 所以处理log就需要自己写shell了. ...

  5. pytorch 指定GPU训练

    # 1: torch.cuda.set_device(1) # 2: device = torch.device("cuda:1") # 3:(官方推荐)import os os. ...

  6. Java技术中如何使用keepalived实现双机热备

    Keepalived简介 Keepalived是Linux下一个轻量级别的高可用解决方案.高可用(High Avalilability,HA),其实两种不同的含义:广义来讲,是指整个系统的高可用行,狭 ...

  7. VUE的双向绑定及局部组件的使用

    vue的双向绑定,使用v-model,v-model只能使用在input  textare    select中 <!DOCTYPE html> <html lang="z ...

  8. kafka消费组、消费者

    consumer group consumer instance 一个消费组可能有一个或者多个消费者.同一个消费组可以订阅一个或者多个主题.主题的某一个分区只能被消费组的某一个消费者消费.那么分区和消 ...

  9. CF613D Kingdom and its Cities 虚树 + 树形DP

    Code: #include<bits/stdc++.h> #define ll long long #define maxn 300003 #define RG register usi ...

  10. xiugai-去除js注释

    <div class="myLoading"> <div class="svg-wrap"> <svg width="8 ...