zoukankan      html  css  js  c++  java
  • 【二分】XMU 1587 中位数

    题目链接:

      http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1587

    题目大意

      求两个长度为n(n<=109)的有序序列合并后的中位数。序列中的数在long long范围内

    题目思路:

      【二分】

      一开始想贪心错了给跪了。

      因为序列长度相等,取中点比较大小,而后更新比较区间。如果x的中点小于y的中点,则中位数一定在x中点右侧和y中点左侧的合并序列中。

     1 //
     2 //by coolxxx
     3 //
     4 #include<iostream>
     5 #include<algorithm>
     6 #include<string>
     7 #include<iomanip>
     8 #include<memory.h>
     9 #include<time.h>
    10 #include<stdio.h>
    11 #include<stdlib.h>
    12 #include<string.h>
    13 #include<stdbool.h>
    14 #include<math.h>
    15 #define min(a,b) ((a)<(b)?(a):(b))
    16 #define max(a,b) ((a)>(b)?(a):(b))
    17 #define abs(a) ((a)>0?(a):(-(a)))
    18 #define lowbit(a) (a&(-a))
    19 #define sqr(a) ((a)*(a))
    20 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
    21 #define eps 1e-8
    22 #define J 10
    23 #define MAX 0x7f7f7f7f
    24 #define PI 3.1415926535897
    25 #define N 104
    26 using namespace std;
    27 int n,m,lll,ans,cas;
    28 long long x,y,xx,yy,z,a1,a2,b1,b2,c1,c2,lx,ly,rx,ry,midx,midy;
    29 long long f(long long a1,long long b1,long long c1,long long i)
    30 {
    31     return a1*sqr(i)+b1*i+c1;
    32 }
    33 void work()
    34 {
    35     lx=ly=1;rx=ry=n;
    36     while(lx<rx-1)
    37     {
    38         midx=(rx+lx)>>1;
    39         midy=(ry+ly)>>1;
    40         if((ry-ly)&1)midy++;
    41         x=f(a1,b1,c1,midx);y=f(a2,b2,c2,midy);
    42         if(x>y)
    43         {
    44             rx=midx;
    45             ly=midy;
    46         }
    47         else
    48         {
    49             lx=midx;
    50             ry=midy;
    51         }
    52     }
    53     x=f(a1,b1,c1,lx);
    54     y=f(a2,b2,c2,ly);
    55     xx=f(a1,b1,c1,rx);
    56     yy=f(a2,b2,c2,ry);
    57 }
    58 int main()
    59 {
    60     #ifndef ONLINE_JUDGE
    61 //    freopen("1.txt","r",stdin);
    62 //    freopen("2.txt","w",stdout);
    63     #endif
    64     int i,j,k;
    65 //    while(~scanf("%s",s1))
    66     while(~scanf("%d",&n))
    67     {
    68         cin>>a1>>b1>>c1>>a2>>b2>>c2;
    69         work();
    70         z=min(xx,yy)+max(x,y);
    71         cout<<z/2;
    72         if(z%2)printf(".5");
    73         puts("");
    74     }
    75     return 0;
    76 }
    77 
    78 
    79 /*
    80 //
    81 
    82 //
    83 */
    View Code
  • 相关阅读:
    阿里笔试题
    springboot-security-jwt
    java 面试架构篇
    java 面试题 mybatis 篇
    Java 多线程并发工具类
    java 面试题 高阶版
    给你的右键菜单添加“VScode”
    HTML重点知识点汇总
    HTML5知识点小结
    给博客园添加百度统计方法
  • 原文地址:https://www.cnblogs.com/Coolxxx/p/5428001.html
Copyright © 2011-2022 走看看