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. “没有找到mfc100u.dll”的解决方法

    现在需要安装 MindManager 2016 思维导图软件时,打开软件提示找不到 mfc100u.dll,无法执行程序.之前一直好好的,现在换电脑了安装提示这个问题,然后百度找的解决方案: 需要去微 ...

  2. 五、Ubuntu 16.04 安装PyCharm

    方法一,在终端用指令通过第三方源安装pycharm. 本文通过第三方源安装PyCharm,好处是升级方便. 添加源:    $ sudo add-apt-repository ppa:mystic-m ...

  3. Redis分布式锁【实战】

    概述 目前几乎很多大型网站及应用都是分布式部署的,分布式场景中的数据一致性问题一直是一个比较重要的话题.分布式的CAP理论告诉我们“任何一个分布式系统都无法同时满足一致性(Consistency).可 ...

  4. 20.Nodejs基础知识(上)——2019年12月16日

    2019年12月16日18:58:55 2019年10月04日12:20:59 1. nodejs简介 Node.js是一个让JavaScript运行在服务器端的开发平台,它让JavaScript的触 ...

  5. Zabbix通过邮件发送Screen图形报表实现

    在使用Zabbix的过程中,我们通常会建立一些需要的Screen图形报表来汇总需要监控的Graph. 而下面的两个脚本,则是通过从Zabbix数据库中获取所有的Screen图形参数,提供Zabbix的 ...

  6. root登录

    ,编辑/etc/lightdm/lightdm.conf: gedit /etc/lightdm/lightdm.conf [Seat:*] autologin-guest=false autolog ...

  7. Ubuntu 18.04 安装 Octave 5.1

    最新版目前只能通过编译安装.折腾了半天终于搞定: 需要使用apt-get install先把各种 dependencies 安装好. 编译JIT需要安装sudo apt-get install llv ...

  8. 数据库智能管理助手-CloudDBA

    摘要:阿里云CloudDBA主要分为离线分析和在线分析两种功能.帮助用户节省成本,定位问题,分析原因并推荐解决方法.CloudDBA可以做到实时诊断,离线诊断和SQL优化.并且通过MySQL的参数调优 ...

  9. TextView控件常用属性

    常用属性 android:id——控件ID android:layout_width——控件宽度 android:layout_height——控件高度 android:text——文本内容 andr ...

  10. python 全栈开发,Day53(jQuery的介绍,jQuery的选择器,jQuery动画效果)

    01-jQuery的介绍 1.为什么要使用jQuery 在用js写代码时,会遇到一些问题: window.onload 事件有事件覆盖的问题,因此只能写一个事件. 代码容错性差. 浏览器兼容性问题. ...