Median

题目链接:

http://acm.split.hdu.edu.cn/showproblem.php?pid=5857

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

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

Source


2016 Multi-University Training Contest 10


##题意:

给出一个有序的数列.
求由 A[l1]~A[r1] 与 A[l2]~A[r2] 组成的新序列的中位数.


##题解:

中位数:排序后中间位置的数,偶数个时为中间两个的平均值.
由于序列是有序的,可以分情况找到新序列的中位数的下标.
注意细节的处理.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 101000
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

int n, m;

LL num[maxn];

LL query1(int l1,int r1,int l2,int r2, int aim) {

if(l1+aim-1 < l2) return num[l1+aim-1];

else if(aim > (l2-l1)+2(r1-l2+1)) {

int pos = aim - ((l2-l1)+2
(r1-l2+1));

return num[r1+pos];

} else {

aim -= (l2-l1);

int pos = aim / 2;

if(aim % 2) return num[l2+pos+1-1];

else return num[l2+pos-1];

}

}

LL query2(int l1,int r1,int l2,int r2, int aim) {

if(l1+aim-1 <= r1) return num[l1+aim-1];

aim -= (r1-l1+1);

return num[l2+aim-1];

}

int main(int argc, char const *argv[])

{

//IN;

int t; cin >> t;
while(t--)
{
scanf("%d %d", &n, &m);
for(int i=1; i<=n; i++) {
scanf("%lld", &num[i]);
} while(m--) {
int L1,L2,R1,R2;
int l1,r1; scanf("%d %d", &L1, &R1);
int l2,r2; scanf("%d %d", &L2, &R2);
l1 = min(L1,L2); l2 = max(L1,L2);
r1 = min(R1,R2); r2 = max(R1,R2); int tol = (r1-l1+1) + (r2-l2+1); if(r1 < l2) {
if(tol & 1) {
printf("%lld.0\n", query2(l1,r1,l2,r2, (tol+1)/2));
} else {
LL ans = query2(l1,r1,l2,r2, (tol+1)/2) + query2(l1,r1,l2,r2, (tol+1)/2+1);
printf("%lld", ans/2);
if(ans % 2) printf(".5\n");
else printf(".0\n");
}
}
else {
if(tol & 1) {
printf("%lld.0\n", query1(l1,r1,l2,r2, (tol+1)/2));
} else {
LL ans = query1(l1,r1,l2,r2, (tol+1)/2) + query1(l1,r1,l2,r2, (tol+1)/2+1);
printf("%lld", ans/2);
if(ans % 2) printf(".5\n");
else printf(".0\n");
}
}
}
} return 0;

}

HDU 5857 Median (推导)的更多相关文章

  1. HDU 5857 Median

    因为原序列是排列好了的,那么只要看一下给出的两个区间相交的情况,然后分类讨论一下,O(1)输出. #pragma comment(linker, "/STACK:1024000000,102 ...

  2. HDU 2685 GCD推导

    求$(a^n-1,a^m-1) \mod k$,自己手推,或者直接引用结论$(a^n-1,a^m-1) \equiv a^{(n,m)}-1 \mod k$ /** @Date : 2017-09-2 ...

  3. [ An Ac a Day ^_^ ] hdu 4565 数学推导+矩阵快速幂

    从今天开始就有各站网络赛了 今天是ccpc全国赛的网络赛 希望一切顺利 可以去一次吉大 希望还能去一次大连 题意: 很明确是让你求Sn=[a+sqrt(b)^n]%m 思路: 一开始以为是水题 暴力了 ...

  4. hdu 3648 Median Filter (树状数组)

    Median Filter Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. [hdu 2298] 物理推导+二分答案

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2298 #include<bits/stdc++.h> using namespace st ...

  6. HDU 5312(数学推导+技巧)

    首先说一下.N*(N-1)/2为三角形数,随意一个自然数都最多可由三个三角形数表示. 对于,对于给定的要求值 V, 那么其一组解可表示为 V = 6*(K个三角形数的和)+K: 即随意由k个数组成的解 ...

  7. HDU5857 Median 模拟

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

  8. hdu 3282 Running Median

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3282 Running Median Description For this problem, you ...

  9. HDU 5734 Acperience (推导)

    Acperience 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5734 Description Deep neural networks (DN ...

随机推荐

  1. Android Camera 使用小结

    Android手机关于Camera的使用,一是拍照,二是摄像,由于Android提供了强大的组件功能,为此对于在Android手机系统上进行Camera的开发,我们可以使用两类方法:一是借助Inten ...

  2. 随便谈谈alphago与人机大战

    3月16日历时8天的人机大战终于落下帷幕,alphago以4:1的比分击败了当年如日中天的李世石.这个结果让我这个围棋爱好者+计算机爱好者百感交集…… ——一个时代落幕了,一个新的时代开启了. 这次人 ...

  3. 【转载】Mysql binlog relaylog 日志迁移

    背景:   默认情况下,mysql的数据.binlog.relaylog都是保存在同一个磁盘上,路径根据每个人的设置不一. 当mysql数据库中数据或日志增长很快时,磁盘可能面临空间不够或者IO性能跟 ...

  4. 对于fmri的设计矩阵构造的一个很直观的解释-by 西南大学xulei教授

    本程序意在解释这样几个问题:完整版代码在本文的最后. 1.实验的设计如何转换成设计矩阵? 2.设计矩阵的每列表示一个刺激条件,如何确定它们? 3.如何根据设计矩阵和每个体素的信号求得该体素对刺激的敏感 ...

  5. 用 Xcode 开发 Cydia Substrate 插件(二)

    上次介绍了一个如何用 Xcode 来构建 Substrate 插件,但是开发的具体过程还没有涉及,而这往往又正是初学者最难下手的地方,所以有了本文的后续. 不过在开始之前你要先做好思想准备,相比较开发 ...

  6. 【解题报告】zju-1145 Dreisam Equations

    原题地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=145 题目大意:在给定的等式右边数字之间加上加.减.乘运算符,使等式成 ...

  7. Android 使Volley完美支持自定义证书的Https

    其实在最早的版本里,Volley甚至是不支持https协议的,只能跑http,当然你也可以自己修改他的源码让他支持,如今volley的代码经过一些改进以后, 已经可以完美支持https协议了,无论是在 ...

  8. RequireJS进阶(三) 转

    进阶的前面两篇讲述了r.js如何通过命令行把所有的模块压缩为一个js文件或把所有的css压缩为一个css文件.其中包括一些压缩配置参数的使用. 但以上两种方式有几个问题 1.通过命令手动配置压缩选项显 ...

  9. 通过反射执行get、set方法

    Class clazz = sourceObj.getClass(); 1.获取所有属性 BeanInfo beanInfo = Introspector.getBeanInfo(clazz); Pr ...

  10. 一种Javascript解释ajax返回的json的好方法

    通常ajax请求返回的格式为json或者xml,如果返回的是json,则可以通过转换成javascript对象进行操作,如下: 1.ajax请求的controller实现 @RequestMappin ...