题目链接:

Median

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

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
 
题意:
 
给以一个排好序的序列,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[N],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;
}

  

hdu-5857 Median(水题)的更多相关文章

  1. hdu 5210 delete 水题

    Delete Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5210 D ...

  2. hdu 1251 (Trie水题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  3. HDU 5857 Median (推导)

    Median 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5857 Description There is a sorted sequ ...

  4. HDU 5703 Desert 水题 找规律

    已知有n个单位的水,问有几种方式把这些水喝完,每天至少喝1个单位的水,而且每天喝的水的单位为整数.看上去挺复杂要跑循环,但其实上,列举几种情况之后就会发现是找规律的题了= =都是2的n-1次方,而且这 ...

  5. HDU 4493 Tutor 水题的收获。。

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=4493 题意我都不好意思说,就是求12个数的平均数... 但是之所以发博客,显然有值得发的... 这个题最 ...

  6. hdu 4802 GPA 水题

    GPA Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4802 Des ...

  7. hdu 4493 Tutor 水题

    Tutor Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4493 D ...

  8. hdu 5495 LCS 水题

    LCS Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5495 Descr ...

  9. hdu 4891 模拟水题

    http://acm.hdu.edu.cn/showproblem.php?pid=4891 给出一个文本,问说有多少种理解方式. 1. $$中间的,(s1+1) * (s2+1) * ...*(sn ...

  10. hdu 5734 Acperience 水题

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

随机推荐

  1. 解析iscroll-小demo

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  2. Sahi ---实现 Web 自动化测试

    参考网址:http://sahipro.com/docs/sahi-apis/index.html Sahi 是 Tyto Software 旗下的一个基于业务的开源 Web 应用自动化测试工具.Sa ...

  3. hdu 2602 - Bone Collector(01背包)解题报告

    Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  4. openssl生成https证书

    openssl生成https证书 分类: 其它2009-09-03 16:20 452人阅读 评论(0) 收藏 举报 includemoduleaccessapachessl服务器 openssl生成 ...

  5. asp.net core 系列之Reponse caching之cache in-memory (2)

    这篇文章(主要翻译于官网,水平有限,见谅)讲解asp.net core 中的 Cache in-memory (内存缓存). Cache in-memory in ASP.NET Core Cachi ...

  6. 黑名单机制来临,你的应用还好么?Android P DP2最新兼容性报告出炉

    5月9日,谷歌面向全球开发者发布了 Android P Beta,即 Android P DP2. 华为终端开放实验室第一时间对TOP1000主流应用兼容性进行测试,那么在版本兼容性方面各主流应用有何 ...

  7. bash学习记录

    bash: 管理员:  提示符# 普通用户:提示符$ 环境变量 A=3(变量是指内存空间,A指的是内存空间的名称-变量标示符) PS1  \u@\h:\w\$  \u用户名 \h主机名 \w工作目录的 ...

  8. 限制UITextView的字数和字数监控,表情异常的情况和禁用表情

    限制UITextView的字数和字数监控,表情异常的情况和禁用表情   3523FD80CC4350DE0AE7F89A8532B9A8.png 因为字数占一个字符,表情占两个字符.你要是限制15个字 ...

  9. smarty模板里实现缓存。

    smarty模板里实现缓存.分页缓存在任何里都可以用 我用了三个类 include("../init.inc.php");//模板入口类 include("../DBDA ...

  10. N皇后问题算法

    N皇后问题的两种主要算法是试探回溯法和位运算法.前一种是经典算法,后一种是目前公认的最高效算法,后者比前者效率提高了至少一个数量级.很多问题可以借鉴位运算的思想. 以下是转载的我认为写的比较好的一篇N ...