码迷,mamicode.com
首页 > 其他好文 > 详细

Freight Train(二分+思维)

时间:2020-05-08 12:56:48      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:wait   can   false   factor   sim   send   apt   through   code   

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\n", re);
62     }
63     return 0;
64 }

 

Freight Train(二分+思维)

标签:wait   can   false   factor   sim   send   apt   through   code   

原文地址:https://www.cnblogs.com/0xiaoyu/p/12849760.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!