Racing Car Computer uva

这道题就是给你一个车,前边有几辆车,后边有几辆车,可能有几辆车是并行的,那么他问的是,最少有几辆车是不符合实际的。(也就是求n-最多有几辆车符合实际)

这道题看似很复杂,但是仔细想,如果两辆车是并行的,那么他们前边后边车的个数是相同的,(前提是前边后边的车的个数要合法),知道前边后边车的个数,假设是a,b,那么这辆车所在的区间就有可能是[a+1,n-b].所以用一个区间代表有无,相同的时候,区间完全一样才说明可以。容易遗漏的一点是,在这个区间内,最多满足条件的个数是(n-b-(a+1)+1)辆车。用dp【i】表示,到第i辆车为止,最多有dp【i】个是满足的。状态转移方程是dp[i]=max(dp[i],dp[j]+cnt[j+1][i])

题目:

The racing cars of today are equipped with so many sophisticated equipment. Introduction of a new
visual transducer which is interfaced with the on-board computer can tell you on the  y how many
cars are ahead of you while how many are trailing. There are
N
cars on a racing track. Each has an
on-board computer with the new feature. During the race, every single car's computer keeps displaying
two integers,
a
(The number of cars in front of him) &
b
(The number of cars behind him) for a
particular moment. It is possible that at some time, some of the cars are racing side by side i.e. they
are exactly at the same location. A car will not consider any other car at the same location to be a
leading or trailing car.
Now, it is suspected that some of the new transducers are not working properly inside such high
speed vehicles. The report with all computers' data generated at a particular timestamp is reported to
you. You are to determine the minimum number of cars that have faulty data.
Input
Each test case begins with an integer
N
(1

N

1000), the number of cars on a track. The next
N
lines each has two integers |
a
&
b
(0

a;b

1500) for a particular car.
The last test case is followed by a line with a single `
0
' indicating the end of input.
Output
For each test case, print a line in the format, `
Case
X
:
Y
', where
X
is the case number &
Y
is the
minimum number of cars that must have faulty data according to the report.
Sample Input
4
2 2
0 0
0 2
3 1
1
1 1
0
Sample Output
Case 1: 3
Case 2: 1


代码:

#include<bits/stdc++.h>
using namespace std;
int cnt[2000][2000];
int dp[1100];
int main()
{
    int n;
    int a,b;
    int cas=0;
    while(scanf("%d",&n))
    {
        cas++;
        if(n==0)
            break;
        memset(cnt,0,sizeof(cnt));
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a,&b);
            if(n-b>=(a+1))
            cnt[a+1][n-b]++;
            if(cnt[a+1][n-b]>=(n-b-a-1+1))
                cnt[a+1][n-b]=n-b-a;
           // cout<<"a+1:"<<a+1<<"  n-b:"<<n-b<<"   "<<cnt[a+1][n-b]<<"$$$"<<endl;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<i;j++){
                dp[i]=max(dp[i],dp[j]+cnt[j+1][i]);
            }
           // cout<<dp[i]<<"***"<<endl;
        }
        cout<<"Case "<<cas<<":"<<" "<<n-dp[n]<<endl;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/xianpingping/article/details/80564163
car
今日推荐