D Bridge Automation(简单dp)
题意:有一座可上升下降的桥,上升和下降都需要60s,一艘船通过需要20s,一艘船最多可在桥前等30mins,给出每艘船的到达时间,问桥最少需要保持抬起多久(抬起,放下过程中也算)。
思路:简单dp,可分两种情况,一种是这艘桥自己走了,另一种是等其他的船来了一起走。一起走的话,看是等到最后一块走,还是桥保持上升状态一艘一艘的走。
D Bridge Automation
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:
- No boat may be forced to wait for more than 30 minutes.
- 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 ≤ 105
).
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.
10 Problem D: Bridge Automation
Sample Input 1 Sample Output 1
2 160
100
200
Sample Input 2 Sample Output 2
3 250
100
200
2010
Sample Input 3 Sample Output 3
3 300
100
200
2100
代码1:
#include<bits/stdc++.h>
#define maxn 4005
#define inf 0x3f3f3f3f
using namespace std;
int a[maxn];
long long dp[maxn];
int main()
{
ios::sync_with_stdio(0);
int n;
cin >> n;
memset(dp, inf, sizeof(dp));
for(int i = 1; i <= n; i ++) cin >> a[i];
dp[0] = 0;
for(int i = 1; i <= n; i ++)
{
for(int j = 1; j <= i; j ++)
{
dp[i] = min(dp[i], dp[j-1]+max(a[i]-a[j]-1800+140, (i-j+1)*20+120));
}
}
cout << dp[n] << endl;
return 0;
}
代码2:
#include<bits/stdc++.h>
#define maxn 4005
#define inf 0x3f3f3f3f
using namespace std;
int a[maxn];
long long dp[maxn];
int main()
{
ios::sync_with_stdio(0);
int n;
cin >> n;
memset(dp, inf, sizeof(dp));
for(int i = 1; i <= n; i ++) cin >> a[i];
dp[0] = 0;
for(int i = 1; i <= n; i ++)
{
dp[i] = dp[i-1]+140;//让i船先过
for(int j = 1; j <= i; j ++)
{
dp[i] = min(dp[i], dp[j-1]+120+max(a[i]-a[j]-1780, (i-j+1)*20));//max里分别是让j船先等1780秒通过后 保持桥上升状态时 和 将船堆到一起通过。
}
}
cout << dp[n] << endl;
return 0;
}