zoukankan      html  css  js  c++  java
  • Freight Train(二分+思维)

    The chemical company NS (Nasty Substances) has three factories: one in the Netherlands, one in Belgium and one in Luxembourg. Chemicals are moved between the factories on a regular basis, which is always done by freight train. Last night, the the weekly shipment of chemicals was again sent from the factory in the Netherlands to the factory in Belgium. However, something has gone wrong: some of the chemicals that arrived in Belgium were supposed to go to the factory in Luxembourg. What’s more, the Luxumbourg chapter is eagerly awaiting the arrival of its chemicals, as any delay in delivery causes major issues in the production pipeline.

    In order to make sure that the error can be corrected as quickly as possible, an additional L1L−1 locomotives are sent towards the freight train, which is currently stationed at the Belgian factory. (Thus, in total there are now LL locomotives available.) Each locomotive can pick up some initial segment of the train (that is, the first KK wagons) and transport it either back to the Netherlands or on towards Luxembourg. There is no time to do more elaborate rearrangements of the freight train. Since time is of the essence, you want the trains going to Luxembourg to be as short as possible: shorter trains can move faster. Trains heading back to Holland can be as long as you want. You are given a list of the wagons that need to go to Luxembourg. All the other wagons are empty and can either be carried along to the Luxembourg plant as well, or be driven back to the Netherlands. No wagon can be left behind. Your task is to split the freight train into (at most) LL consecutive trains so that the largest train heading for Luxembourg is as short as possible.

    Input

    The input starts with a line containing an integer TT (1T100)1≤T≤100), the number of test cases. Then for each test case:

    • One line with three space-separated integers NN, WW and LL, satisfying 1N1091≤N≤109 and 1W,L100001≤W,L≤10000 and WNW≤N. These denote the number of wagons of the freight train stationed in Belgium, the number of wagons still containing freight and the number of locomotives.

    • One line with WW space-separated integers, denoting the numbers of the wagons that still contain freight in ascending order. Wagons are numbered 11 through NN.

    Output

    For each test case, output one line with a single integer, denoting the number of wagons of the longest train heading for Luxembourg.

    In the first test case, you take the first two wagons and send them towards Luxembourg. The remaining wagons can go back to the Netherlands.

    In the second test case, your best option is to simply split the train in three parts, each of which heads for Luxembourg.

    In the third test case, split the train into three parts of two wagons each. Two of those are sent towards Luxembourg. Note that one of the locomotives remains unused.

    Sample Input 1Sample Output 1
    3
    6 2 2
    1 2
    8 3 3
    1 4 7
    6 4 4
    1 2 5 6
    
    2
    3
    2

    题意:现有n节车厢,其中w节非空车厢,l个车头,要把n节车厢全部运走,空车运往A点无限制条件,非空车运往B急用(最慢的也尽可能快,使得最大车厢数最小),车速取决于车厢数量,问非空车最大车厢的最小值。

    思路:二分找“非空车最大车厢数的最小值”,判断这个数是否能运走所有车厢

     1 #include <bits/stdc++.h>
     2 ///错误原因:开头空车厢很多的时候,应该num++
     3 #define inf 1e9
     4 
     5 using namespace std;
     6 
     7 int a[10005], w, k, n;
     8 
     9 bool judge(int x)///一个车头长为x
    10 {
    11     int num, cur;
    12     num = 0;
    13     cur = 0;
    14     for(int i=0;i<w;i++)
    15     {
    16         if(a[i] <= cur) continue;///与前面车厢公用一车头
    17         else
    18         {
    19             if(a[i] <= cur + x)///启用一个新车头
    20             {
    21                 num++;
    22                 cur += x;
    23             }
    24             else ///前一车头与当前车头之间有太多空车厢,空车厢自己单独用一个车头
    25             {
    26                 num += 2;
    27                 cur = a[i]+x-1;
    28             }
    29         }
    30     }
    31     if(n > cur) num++;///判断最后一个车头是否过长
    32     if(num <= k) return true;
    33     else return false;
    34 }
    35 
    36 int main()
    37 {
    38     int t;
    39     scanf("%d", &t);
    40     while(t--)
    41     {
    42         scanf("%d %d %d", &n, &w, &k);
    43         for(int i=0;i<w;i++)
    44         {
    45             scanf("%d", &a[i]);
    46         }
    47         int l, r, re;
    48         l = 1;
    49         r = n;
    50         while(l<=r) ///二分
    51         {
    52             int mid = l + (r-l)/2;
    53             if(judge(mid))
    54             {
    55                 re = mid;
    56                 r = mid - 1;
    57             }
    58             else
    59                 l = mid + 1;
    60         }
    61         printf("%d
    ", re);
    62     }
    63     return 0;
    64 }
  • 相关阅读:
    python实现布隆过滤器及原理解析
    gin框架源码解析
    阿里云docker操作问题记录
    Qt编写数据可视化大屏界面电子看板系统
    CSS3-3D制作案例分析实战
    前端可视化项目流程,涉及three.js(webGL),3DMax技术,持续更新
    前端可视化项目流程,涉及three.js(webGL),3DMax技术,持续更新
    jquery拖拽排序,针对后台列表table进行拖拽排序(Echart不刷新页面,多语言切换下的地图数据重新加载,api请求数据加载
    Java 设置Excel条件格式(高亮条件值、应用单元格值/公式/数据条等类型)C# 创建Excel气泡图
    Java 如何在PPT中设置形状组合、取消组合、编辑组合形状
  • 原文地址:https://www.cnblogs.com/0xiaoyu/p/12849760.html
Copyright © 2011-2022 走看看