zoukankan      html  css  js  c++  java
  • C++-蓝桥杯-区间移位[二分答案][multiset多重集的使用]

    有一说一,check不好想

    思路解析,from 这里

     1 #include <set> 
     2 #include <cstdio>
     3 #include <iostream>
     4 using namespace std;
     5 multiset<pair<int,int> >A;
     6 const int N=10000*2;
     7 int check(int mid){
     8     multiset<pair<int,int> > B(A);
     9     int pos=0;
    10     while(true){
    11         int flag=0;
    12         for(multiset<pair<int,int> >::iterator it=B.begin();it!=B.end();it++){
    13             int L=(*it).second,R=(*it).first;
    14             if(L-mid<=pos&&pos<=R+mid){//可更新出新区间,pos更新要分类讨论 
    15                 if(pos<=L+mid)pos+=(R-L);
    16                 else pos=R+mid;
    17                 flag=1,B.erase(it);
    18                 break;
    19             }
    20         }
    21         if(!flag||pos>=N)break;    
    22     }
    23     return pos;
    24     
    25 }
    26 
    27 double binarySearch(int l,int r){
    28     int mid;
    29     while(l<=r){
    30         mid=(l+r)>>1;
    31         if(check(mid)<N)l=mid+1;
    32         else r=mid-1;
    33     }
    34     return l*0.5;
    35 }
    36 
    37 int main(){
    38     int L,R,n;
    39     for(cin>>n;n--;){
    40         cin>>L>>R;
    41         L*=2,R*=2;
    42         A.insert(make_pair(R,L));//按右端点为第一关键字升序排序         
    43     }
    44     cout<<binarySearch(0,N)<<endl; 
    45     return 0;
    46 } 
  • 相关阅读:
    GDI 设备环境句柄(2)
    GDI 像素(5)
    Api+Mvc增删查改
    sql语句全
    Mvc 导出
    触发器、事务
    计算时间戳的差
    SQL行转列经典例子(转载)
    Socket (套接字)通信
    MVC上传图片
  • 原文地址:https://www.cnblogs.com/JasonCow/p/13743065.html
Copyright © 2011-2022 走看看