简单DP;朴素即可过。。。虽然900ms
f[i][j][k] 左边过了i辆车,右边过了j辆车,最后一辆过的车是(0,左边,1右边)
枚举i,j,k,然后如果左后过的是左边,则枚举接下来右边过了x辆车的情况,同一个方向上的车,每一个点必须间隔10s(邪恶数据 2 A 0 10 A 10 0)
Mountain Road
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 117 | Accepted: 47 |
Description
In the Franconian Switzerland, there is a narrow mountain road. With only a single lane, this is a bottleneck for two-way traffic. Your job is to schedule incoming cars at both ends so that the last car leaves the road as early as possible.
Each car is specified by three values: the direction in which it is going, the arrival time at the corresponding beginning of the road, and the driving time this car needs to get through, provided it is not slowed down by other cars in front. Cars cannot overtake each other on the mountain road, and reordering cars in the queues at the ends of the road is not allowed.
For safety reasons, two successive cars going in the same direction may not pass any point of the road within less than 10 seconds. This ensures that the second car will not crash into the first car if the latter brakes hard. However, if another car passes in the other direction in between, it will be clear that the road is empty, so in this case, this rule does not apply.
Each car is specified by three values: the direction in which it is going, the arrival time at the corresponding beginning of the road, and the driving time this car needs to get through, provided it is not slowed down by other cars in front. Cars cannot overtake each other on the mountain road, and reordering cars in the queues at the ends of the road is not allowed.
For safety reasons, two successive cars going in the same direction may not pass any point of the road within less than 10 seconds. This ensures that the second car will not crash into the first car if the latter brakes hard. However, if another car passes in the other direction in between, it will be clear that the road is empty, so in this case, this rule does not apply.
Input
The first line of the input consists of a single integer c (1 <= c <= 200), the number of test cases.
Then follow the test cases, each beginning with a single line consisting of an integer n (1 <= n <= 200), the number of cars you are to consider in this test case. The remainder of each test case consists of n lines, one line per car, starting with a single upper case letter ("A" or "B"), giving the direction in which the car is going. Then follow, on the same line, two integers t (0 <= t <= 100 000) and d (1 <= d <= 100 000), giving the arrival time at the beginning of the road and the minimum travel time, respectively, both in seconds.
Within a test case, the cars are given in order of increasing arrival time, and no two cars will arrive at the same time.
Then follow the test cases, each beginning with a single line consisting of an integer n (1 <= n <= 200), the number of cars you are to consider in this test case. The remainder of each test case consists of n lines, one line per car, starting with a single upper case letter ("A" or "B"), giving the direction in which the car is going. Then follow, on the same line, two integers t (0 <= t <= 100 000) and d (1 <= d <= 100 000), giving the arrival time at the beginning of the road and the minimum travel time, respectively, both in seconds.
Within a test case, the cars are given in order of increasing arrival time, and no two cars will arrive at the same time.
Output
For each test case, print a single line consisting of the point in time (in seconds) the last car leaves the road when the cars are scheduled optimally.
Sample Input
2 4 A 0 60 B 19 10 B 80 20 A 85 100 4 A 0 100 B 50 100 A 100 1 A 170 100
Sample Output
200 270
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAXN 300
#define INF 0x3f3f3f3f
int ls[MAXN],le[MAXN],rs[MAXN],re[MAXN];
int f[MAXN][MAXN][2];
int n,nl,nr;
int main()
{
int cs;
scanf("%d",&cs);
while(cs--)
{
scanf("%d",&n);
nl=nr=0;
for(int i=0;i<n;i++)
{
char s[3];
int st,ed;
scanf("%s%d%d",s,&st,&ed);
if(s[0]=='A')
ls[++nl]=st,le[nl]=ed;
else
rs[++nr]=st,re[nr]=ed;
}
for(int i=0;i<=nl;i++)
for(int j=0;j<=nr;j++)
f[i][j][1]=f[i][j][0]=INF;
f[0][0][0]=f[0][0][1]=0;
for(int i=0;i<=nl;i++)
for(int j=0;j<=nr;j++)
for(int k=0;k<2;k++)
{
int cur,pre;
cur=pre=f[i][j][k]-10;
if(k==0)
{
for(int m=j+1;m<=nr;m++)
{
pre=max(pre+10,rs[m]);
cur=max(cur+10,pre+re[m]);
f[i][m][1]=min(f[i][m][1],cur);
}
}
else
{
for(int m=i+1;m<=nl;m++)
{
pre=max(pre+10,ls[m]);
cur=max(cur+10,pre+le[m]);
f[m][j][0]=min(f[m][j][0],cur);
}
}
}
printf("%d\n",min(f[nl][nr][0],f[nl][nr][1]));
}
return 0;
}