【二分】XMU 1587 中位数
2016-04-24 20:05
232 查看
[b]题目链接:[/b]
http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1587
[b]题目大意:[/b]
求两个长度为n(n<=109)的有序序列合并后的中位数。序列中的数在long long范围内
[b]题目思路:[/b]
【二分】
一开始想贪心错了给跪了。
因为序列长度相等,取中点比较大小,而后更新比较区间。如果x的中点小于y的中点,则中位数一定在x中点右侧和y中点左侧的合并序列中。
View Code
http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1587
[b]题目大意:[/b]
求两个长度为n(n<=109)的有序序列合并后的中位数。序列中的数在long long范围内
[b]题目思路:[/b]
【二分】
一开始想贪心错了给跪了。
因为序列长度相等,取中点比较大小,而后更新比较区间。如果x的中点小于y的中点,则中位数一定在x中点右侧和y中点左侧的合并序列中。
// //by coolxxx // #include<iostream> #include<algorithm> #include<string> #include<iomanip> #include<memory.h> #include<time.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<stdbool.h> #include<math.h> #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) #define abs(a) ((a)>0?(a):(-(a))) #define lowbit(a) (a&(-a)) #define sqr(a) ((a)*(a)) #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b)) #define eps 1e-8 #define J 10 #define MAX 0x7f7f7f7f #define PI 3.1415926535897 #define N 104 using namespace std; int n,m,lll,ans,cas; long long x,y,xx,yy,z,a1,a2,b1,b2,c1,c2,lx,ly,rx,ry,midx,midy; long long f(long long a1,long long b1,long long c1,long long i) { return a1*sqr(i)+b1*i+c1; } void work() { lx=ly=1;rx=ry=n; while(lx<rx-1) { midx=(rx+lx)>>1; midy=(ry+ly)>>1; if((ry-ly)&1)midy++; x=f(a1,b1,c1,midx);y=f(a2,b2,c2,midy); if(x>y) { rx=midx; ly=midy; } else { lx=midx; ry=midy; } } x=f(a1,b1,c1,lx); y=f(a2,b2,c2,ly); xx=f(a1,b1,c1,rx); yy=f(a2,b2,c2,ry); } int main() { #ifndef ONLINE_JUDGE // freopen("1.txt","r",stdin); // freopen("2.txt","w",stdout); #endif int i,j,k; // while(~scanf("%s",s1)) while(~scanf("%d",&n)) { cin>>a1>>b1>>c1>>a2>>b2>>c2; work(); z=min(xx,yy)+max(x,y); cout<<z/2; if(z%2)printf(".5"); puts(""); } return 0; } /* // // */
View Code
相关文章推荐
- 剑指offer:二叉搜索树的后序遍历序列
- 配合LLDB调试器进行iOS代码调试
- 实验一Java开发环境的熟悉
- vs无法打开模块文件
- java中遍历MAP的几种方法
- HDFS的读写限流方案
- 《CSS3实战》笔记--弹性盒模型(三)
- css3的box-sizing
- Unity常用脚本类的继承关系
- [剑指offer]数值的整数次方
- frontpage 2010.2003绿色版
- Dynamic Programming: Weighted Independent Sets in Path Graphs
- 个人工作总结06
- JavaScript实现DDoS攻击原理,以及保护措施。
- 手把手实现AdaBoost算法
- JBox的简单使用Demo
- ListView嵌套EditText导致软键盘闪退的问题
- 团队作业---软件制作4
- Java打印对象的内存地址
- WebService的初步探究与应用-02(SOAP协议)