hdu-5857 Median(水题)
2016-08-18 17:12
316 查看
题目链接:
Total Submission(s): 21 Accepted Submission(s): 4
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each query, output one line, the median of the query sequence, the answer should be accurate to one decimal point.
[align=left]Sample Input[/align]
1
4 2
1 2 3 4
1 2
2 4
1 1
2 2
[align=left]Sample Output[/align]
2.0
1.5
题意:
给以一个排好序的序列,m个询问,每个询问给两个区间,问用这两个区间的这些数组成的新序列的中位数是多少;
思路:
把区间分成三部分,其中一个是相交部分,然后找中位数就好了;
AC代码:
Median
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 21 Accepted Submission(s): 4
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each query, output one line, the median of the query sequence, the answer should be accurate to one decimal point.
[align=left]Sample Input[/align]
1
4 2
1 2 3 4
1 2
2 4
1 1
2 2
[align=left]Sample Output[/align]
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 ,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; }
相关文章推荐
- 数据结构——12 删除两个双向链表中相同的节点
- Rails 加载 错误
- Android - WebView 设置url的Cookie 免登陆
- R语言与Python对比
- 彻底学会使用epoll(三)——ET的读操作实例分析
- Unity3d ugui(2) scrollBar的使用
- 数据库中INFORMATION_SCHEMA的说明及使用
- 安卓仿微信录音功能,带有声音更改
- 置入滤镜——文字印在布纹上
- 2013年人气最高的JavaScript框架排名
- 让你以为在做网页的ArcGIS Runtime WPF
- 设置myeclipse的workspance编码为utf-8
- 两年多工作心得和体会
- 提高可靠性最笨的办法,也是最有效的方法
- 如何让IIS 能响应HTTP PUT和DELETE请求
- javaIo流的层次结构以及字节流与字符流的区别
- Android UI自动化测试--Espresso
- 查找文件夹下所有文件方法c++
- django模板 实现奇偶分行
- iOS 常用第三方类库、完整APP示例