SOJ 3711 Mountain Road

8 篇文章 0 订阅

SOJ3711
题意:这件题意的有点坑。有一条单向道,依次有车到达路的两边,同一方向的车经过同一个地点的时间间隔不能少于10s,不同方向的车,必须等到道路没车才能通过。并且同一个方向的车不能改变车的顺序。之所以说题意有点坑是因为题目中有这样一句话”provided it is not slowed down by other cars in front“,如果把这句话理解为每辆车只能匀速通过会出现问题。见下面分析。
分析:见第二个样例,
>

输入:
4
A 0 100
B 50 100
A 100 1
A 170 100
输出:
270

如果每辆车都只能匀速通过的话,可以发现,270s无论什么方案都是不能得到的,这里的270s应该是这样得到的,先过第二辆车,这个时候,第二辆车走完的时刻是150s,再通过第一辆,此时出发时间是150s,到达时间是250s,再通过第三辆,可以发现,如果必须是匀速,因为连续的两辆车通过同一个地点的间隔不能小于10s,所以他必须在前面一辆到达的250s之后的10s 260s到达,如果按照匀速这个假设,它只能在时刻259s的时候出发,所以下一辆只能在269s的时刻出发了。如果没有匀速的限制条件,第三辆车载160s的时刻出发,可以在260s的时候到达,第四俩车在170s的时刻出发,刚好就在270s的时刻到达了,也就是样例所示。

现在可以考虑定理dp[i][j][A/B]为当通过前i辆A方向的车,和j辆B方向的车,最后一辆车的方向是A/B的时候最短时间。如果这道题的题意是每辆车只能是匀速,那么dp[i][j][A/B]的时间可以从dp[i-1][j][A/B]转移而来,但是这里因为不是匀速直接这样转移会出问题。所以可以这样来跟新,对于每一个i,j都当成是一个间断点,也就是刚好车辆”反向”的点,然后去更新后面都是顺序通过的点,这样就能保证最后一定是最优的结果。如果还是不能理解,参考代码吧。

#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 200 + 5;
struct Car {
    int s, d;
    Car() {}
    Car(int s, int d):s(s), d(d) {}
};
Car A[maxn], B[maxn];
int dp[maxn][maxn][2];

void DP(int p, int q)
{
    for(int i = 0; i <= p; i++) {
        for(int j = 0; j <= q; j++) {
            for(int k = 0; k < 2; k++) {
                dp[i][j][k] = inf;
            }
        }
    }
    dp[0][0][0] = dp[0][0][1] = 0;

    for(int i = 0; i <= p; i++) {
        for(int j = 0; j <= q; j++) {
            int s = dp[i][j][0], t = s;
            for(int k = j + 1; k <= q; k++) {
                s = max(s, B[k-1].s);
                t = max(t, s + B[k-1].d);
                dp[i][k][1] = min(dp[i][k][1], t);
                s += 10; t += 10;
            }
            s = dp[i][j][1];
            t = s;
            for(int k = i + 1; k <= p; k++) {
                s = max(s, A[k-1].s);
                t = max(t, s + A[k-1].d);
                dp[k][j][0] = min(dp[k][j][0], t);
                s += 10; t += 10;
            }
        }
    }
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--) {
        int n;
        scanf("%d", &n);
        int p = 0, q = 0;
        for(int i = 0; i < n; i++) {
            char dir;
            int t, d;
            cin >> dir >> t >> d;
            if(dir == 'A') {
                A[p++] = Car(t, d);
            } else {
                B[q++] = Car(t, d);
            }
        }
        DP(p, q);
        printf("%d\n", min(dp[p][q][0], dp[p][q][1]));
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Description Mountain Watching [Jeffrey Wang, 2009] One day, Bessie was gazing off into the distance at the beautiful Wisconsin mountains when she wondered to herself: which mountain is the widest one? She decided to take N (1 <= N <= 100,000) equally-spaced height measurements H_i (1 <= H_i <= 1,000,000,000) sequentially along the horizon using her new Acme Long Distance Geoaltimeter. A mountain is defined to be a consecutive sequence of H_i values which increases (or stays the same) and then decreases (or stays the same), e.g., 2, 3, 3, 5, 4, 4, 1. It is possible for a mountain on the edge of her field of vision only to increase or only to decrease in height, as well. The width of a mountain is the number of measurements it encompasses. Help Bessie identify the widest mountain. Here's a simple example of a typical horizon: ******* * ********* *** ********** ***** *********** ********* * * ***************** *********** *** * ** ******************* ************* * * ******* * ********************************************************************** 3211112333677777776543332111112344456765432111212111112343232111111211 aaaaaa ccccccccccccccccccccc eeeeeee ggggggggg bbbbbbbbbbbbbbbbbbbbbbbbbbbb ddddd ffffffffff hhhhhhhhh The mountains are marked 'a', 'b', etc. Obviously, mountain b is widest with width 28. The mountain on the left has width 6 for the purposes of this task. Input * There are multiple test cases. * For each case: ** Line 1: A single integer: N ** Lines 2..N+1: Line i+1 contains a single integer: H_i Output * For each case: ** Line 1: A single line with a single integer that is the width of the widest mountain. Sample Input 7 3 2 3 5 4 1 6 INPUT DETAILS: The height measurements are 3, 2, 3, 5, 4, 1, 6. Sample Output 5 OUTPUT DETAILS: The widest mountain consists of the measurements 2, 3, 5, 4, 1. Other mountains include 3, 2 and 1, 6

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值