Gym

题面

Time limit: 3 seconds

The local council is recording traffic flow using a pressure pad laid across the road. The pressure pad tracks whenever the wheels on an axle of a vehicle cross the pressure pad. The only vehicles using the road are cars with two axles. Each vehicle may or may not have a single-axle trailer attached to it. When a car crosses the pressure pad, two times are recorded: one when the front wheels cross and another when the rear wheels cross. If the car is towing a trailer an additional time is recorded when the trailer wheels cross. Given a sequence of times from the recorder, the council wishes to know how many cars without trailers crossed the pad and how many cars with trailers crossed it.
Obviously, there is some ambiguity. For example, a sequence of 6 recordings could be three cars without trailers or two cars with trailers. To reduce such ambiguity, we will make the following two assumptions:

  1. Any two successive times with a difference less than or equal to 1000 ms must belong to the same vehicle.
  2. Any two successive times with a difference greater than or equal to 2000 ms must be from different vehicles.
    Given a sequence of times, determine the number of cars with and without a trailer.

Input
The first line of the input contains a single integer n (1 ≤ n ≤ 300 000), which is the number of times the pressure pad was triggered. The second line contains n distinct integers t1, . . . , tn (0 ≤ ti < $ 2^{30} $) in increasing order, the times that the pressure pad was triggered. The times are in milliseconds.

Output
Display the number of cars with and without trailers. If the number of cars of each type can be uniquely determined, then display two lines of the form

Cars without trailers: X
Cars with trailers: Y

If there is no interpretation of the times that is consistent with the assumptions, then display Impossible. If there are multiple interpretations of the times that give different numbers of cars with and without trailers, then display Ambiguous.

Sample Input 1

7
10 200 5000 6100 7200 8300 9400

Sample Output 1

Cars without trailers: 2
Cars with trailers: 1

Sample Input 2

6
0 1100 2200 3300 4400 5500

Sample Output 2

Ambiguous

Sample Input 3

4
0 1000 2000 3001

Sample Output 3

Impossible

代码

//此代码来自:http://morecoder.com/article/1281774.html

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
/*
一道dp题,看来自己还是需要继续努力呀
dp[i]表示的是1-i个时间,所能够完美表示的2轮的车有多少个
状态转移
满足条件
dp[i]=dp[i-2]+1
dp[i]=dp[i-3]
并且如果dp[i-2]+1!=dp[3],说明有多种情况,虽然能够分好

*/
int a[350000];
int dp[350000];
int visit[350000];
int main()
{
    int n, i;
    scanf("%d", &n);
    memset(dp, -1, sizeof(dp));
    memset(visit, 0, sizeof(visit));
    for (i = 1; i <= n; i++) scanf("%d", &a[i]);
    a[0] = -inf;
    dp[0] = 0;
    a[n + 1] = 2 * inf;
    for (i = 1; i <= n; i++)
    {
        if (i >= 2 && dp[i - 2] >=0
             && (a[i] - a[i - 1] < 2000)
            && (a[i - 1] - a[i - 2] > 1000)
             && a[i + 1] - a[i] > 1000)
        {
            visit[i] = visit[i - 2];
            dp[i] = dp[i - 2] + 1;
        }
        if (i >= 3
            && dp[i - 3] >=0
            && (a[i] - a[i - 1] < 2000
                && a[i - 1] - a[i - 2] < 2000)
             && (a[i - 2] - a[i - 3] > 1000)
            && a[i + 1] - a[i] > 1000)
        {

            if (dp[i] > 0)
            {
                if (dp[i] != dp[i - 3]) visit[i] = 1;
                else
                visit[i]=max(visit[i],visit[i-3]);
                //为什么要选择最大的,是因为这两种情况都可以
                //虽然结果一样,但是,如果出现一个有问题,那么你在分的时候就出现
                //多种情况,你却不知道如何弄
            }
            else
            {
                dp[i] = dp[i - 3];
                visit[i] = visit[i - 3];
            }
        }
    }
    if (dp[n] == -1)
    {
        printf("Impossible
");
        return 0;
    }
    if (visit[n] == 1)
    {
        printf("Ambiguous
");
        return 0;
    }
    printf("Cars without trailers: %d
", dp[n]);
    printf("Cars with trailers: %d
", (n - 2 * dp[n]) / 3);
}

原文地址:https://www.cnblogs.com/YY666/p/11626821.html