计蒜客 Bridge Automation(ACM ICPC 2017 Warmup Contest 9)

来源:互联网 发布:服务器安装mysql数据库 编辑:程序博客网 时间:2024/06/05 18:56

In Delft there are a number of bridges that are still being operated by a human, known as the bridge operator. One such bridge operator will soon retire, hence there is the need for a replacement.The Bridge And Poker Committee has decided to use a computer program to automatically open and close the bridge, eliminating the need for human interaction.However, the computer program still needs to be written. The requirements for this project are as follows:

1. No boat may be forced to wait for more than 30 minutes.

2. The amount of time during which the bridge is unavailable to road traffic must be as small as possible while still satisfying requirement 1.

It takes 60 seconds to raise or lower the bridge. During this time the bridge is not available to either road traffic or water traffic.

Boats arrive at the bridge at predictable times. It takes 20 seconds for a boat to sail through the bridge, assuming the bridge is already fully raised.

If the bridge is not fully raised when a boat arrives, the boat must wait. If there are boats waiting when the bridge becomes fully raised, these boats pass through the bridge one-by-one,which takes 20 seconds per boat. The bridge must remain fully raised as long as there are still boats sailing through! As soon as all boats have passed, the bridge may be lowered. But it might be more efficient to keep the bridge raised for a little while longer if the next boat is soon to arrive.

Given the arrival times of all boats, operate the bridge such that all boats can pass through without any boat waiting longer than 30 minutes. What is the total amount of time during which the bridge is unavailable to road traffic?

Input

The first line contains an integer N, the number of boats that must pass the bridge(1 ≤ N ≤ 4 000).

Then follow N lines, each containing an integer Ti, the time at which boat i will arrive at the bridge in seconds (60 ≤ Ti ≤ 10^510
​5
​​ ).

Boats are sorted by increasing time of arrival, and never arrive within 20 seconds of each other (i < j implies Ti + 20 ≤ Tj ).

Output

Write one line with an integer, the total number of seconds during which the bridge must be unavailable for road traffic in order for all boats to pass the bridge.

样例输入1

2
100
200
样例输出1

160
样例输入2

3
100
200
2010
样例输出2

250
样例输入3

3
100
200
2100
样例输出3

300

题意:n条船,一条桥,船要过桥的话要等桥升起才行, 桥升起时或者升起状态或者下降时 船都不能通行。一条船的最大等待时间为 30分钟,一条船过桥时间为20秒,船依次过桥,不能同时。现在给出每条船的到达时间,求道路最少的不能使用时间。

分析:DP:d[i]表示 1~i 的船通过后 道路最少的不能使用时间, 则 d[i] = d[j] + w ( 0<=j<=i, w 分情况考虑 看代码). 相当于 第i条船和前面的那些船一起通过,既 桥给这些船上升下降。

#include<iostream>#include<stdio.h>#include<algorithm>#include<string.h>#include<vector>const int maxn = 5000;const int inf = 1e9;typedef long long ll;using namespace std;int n,a[maxn+5];int d[maxn+5];int main(){    while(~scanf("%d",&n))    {        for(int i=1; i<=n; i++) scanf("%d",&a[i]);        memset(d,0,sizeof(d));        for(int i=1; i<=n; i++)        {            d[i] = inf;            for(int j=1; j<=i; j++)            {                int tmp;                if(a[j]+1800<a[i]-(i-j)*20)//(a[i]-(i-j)*20为船开始通行的最优打开时间,                {                    tmp = 140+(a[i]-(a[j]+1800)) + d[j-1];//当第一条船的最大等待时间小于 船开始通行的最优时间时。/140 为 桥上升与下降 和 船j的通过时间和。                }                else                {                    tmp = 140 + (i-j)*20 + d[j-1];                }                d[i] = min(d[i],tmp);            }        }        printf("%d\n",d[n]);    }    return 0;}




阅读全文
0 0
原创粉丝点击