zoukankan      html  css  js  c++  java
  • [二分]Kayaking Trip

    题目描述

    You are leading a kayaking trip with a mixed group of participants in the Stockholm archipelago, but as you are about to begin your final stretch back to the mainland you notice a storm on the horizon. You had better paddle as fast as you can to make sure you do not get trapped on one of the islands. Of course, you cannot leave anyone behind, so your speed will be determined by the slowest kayak. 
    Time to start thinking; How should you distribute the participants among the kayaks to maximize your chance of reaching the mainland safely?
    The kayaks are of different types and have different amounts of packing, so some are more easily paddled than others. This is captured by a speed factor c that you have already figured out for each kayak. The final speed v of a kayak, however, is also determined by the strengths s1 and s2 of the two people in the kayak, by the relation v = c(s1 + s2 ). In your group you have some beginners with a kayaking strength of sb , a number of normal participants with strength s n and some quite experienced strong kayakers with strength se .

    输入

    The first line of input contains three non-negative integers b, n, and e, denoting the number of beginners, normal  participants, and experienced kayakers, respectively. The total number of participants, b + n + e, will be even, at least 2, and no more than 100 000. This is followed by a line with three integers sb , sn , and se , giving the strengths of the corresponding participants (1 ≤ sb < sn < se ≤ 1 000). The third and final line contains m = (b+n+e)/2 integers c1 , . . . , cm(1 ≤ ci ≤ 100 000 for each i), each giving the speed factor of one kayak.

    输出

    Output a single integer, the maximum speed that the slowest kayak can get by distributing the participants two in each kayak.

    样例输入

    3 1 0
    40 60 90
    18 20
    

    样例输出

    1600
    

     

    思路:

      最大化最小值就是典型的二分特征嘛,二分喽

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 const int maxn = 1e5+50;
      4 const int INF = 0x7f7f7f7f;
      5 const double eps = 1e-8;
      6 struct node
      7 {
      8     int num_a;
      9     int num_b;
     10     int num_c;
     11     int need;
     12 };
     13 node pp[maxn];
     14 int x,y,z,stra,strb,strc,tot,all;
     15 int tx,ty,tz;
     16 int cap[maxn];
     17 inline int read()                                //读入优化
     18 {
     19     int x=0,f=1;
     20     char c=getchar();
     21     while (!isdigit(c))
     22         f=c=='-'?-1:1,c=getchar();
     23     while (isdigit(c))
     24         x=(x<<1)+(x<<3)+(c^48),c=getchar();
     25     return x*f;
     26 }
     27 void comb()
     28 {
     29     all=0;
     30     if(tx>=2)
     31     {
     32         pp[all].num_a=2;
     33         pp[all].num_b=0;
     34         pp[all].num_c=0;
     35         pp[all++].need=stra*2;
     36     }
     37     if(ty>=2)
     38     {
     39         pp[all].num_a=0;
     40         pp[all].num_b=2;
     41         pp[all].num_c=0;
     42         pp[all++].need=strb*2;
     43     }
     44     if(tz>=2)
     45     {
     46         pp[all].num_a=0;
     47         pp[all].num_b=0;
     48         pp[all].num_c=2;
     49         pp[all++].need=strc*2;
     50     }
     51     if(tx&&ty)
     52     {
     53         pp[all].num_a=1;
     54         pp[all].num_b=1;
     55         pp[all].num_c=0;
     56         pp[all++].need=stra+strb;
     57     }
     58     if(tz&&ty)
     59     {
     60         pp[all].num_a=0;
     61         pp[all].num_b=1;
     62         pp[all].num_c=1;
     63         pp[all++].need=strc+strb;
     64     }
     65     if(tz&&tx)
     66     {
     67         pp[all].num_a=1;
     68         pp[all].num_b=0;
     69         pp[all].num_c=1;
     70         pp[all++].need=strc+stra;
     71     }
     72 }
     73 bool cmp2(node a,node b)
     74 {
     75     return a.need<b.need;
     76 }
     77 bool judge(int v)
     78 {
     79     tx=x,ty=y,tz=z;
     80     for(int i=1; i<=tot; i++)
     81     {
     82         double nedd=v*1.0/cap[i];
     83         comb();
     84         sort(pp,pp+all,cmp2);
     85         bool flag=false;
     86         for(int i=0; i<all; i++)
     87         {
     88             if(nedd-1.0*pp[i].need<=eps)
     89             {
     90                 tx-=pp[i].num_a;
     91                 ty-=pp[i].num_b;
     92                 tz-=pp[i].num_c;
     93                 flag=true;
     94                 break;
     95             }
     96         }
     97         if(flag==false)
     98             return false;
     99     }
    100     return true;
    101 }
    102 void div()
    103 {
    104     int l=1,r=INF;
    105     int mid,ans;
    106     while(r-l>1)
    107     {
    108         mid=(l+r)/2;
    109         if(judge(mid))
    110         {
    111             l=mid;
    112         }
    113         else
    114         {
    115             r=mid;
    116         }
    117     }
    118     cout << l<< endl;
    119 }
    120 int main()
    121 {
    122     x=read(),y=read(),z=read();
    123     stra=read(),strb=read(),strc=read();
    124     tot=(x+y+z)/2;
    125     for(int i=1; i<=tot; i++)
    126     {
    127         cap[i]=read();
    128     }
    129     div();
    130     return 0;
    131 }
    View Code
  • 相关阅读:
    【华为云技术分享】使用keil5打开GD32F450i的MDK项目出现的问题以及J-Link无法烧录程序对应的解决方案
    【华为云技术分享】不为人知的稠密特征加入CTR预估模型的方法
    205. 判断两个字符串的模式是否相同 Isomorphic Strings
    541. 反转字符串2 Reverse String II
    插入排序,二分查找插入排序,使用二叉树的插入排序
    二分查找,求mid值的类型溢出问题
    二叉搜索树类的C#实现
    二叉搜索树,删除节点
    二叉搜索树的前驱节点和后继节点
    438. 匹配字符串(顺序不同但个数相同的字符串) Find All Anagrams in a String
  • 原文地址:https://www.cnblogs.com/SoulSecret/p/9562360.html
Copyright © 2011-2022 走看看