Codeforces Round #165 (Div. 1) Greenhouse Effect(DP)

Greenhouse Effect
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Emuskald is an avid horticulturist and owns the world's longest greenhouse — it is effectively infinite in length.

Over the years Emuskald has cultivated n plants in his greenhouse, of m different plant species numbered from 1 to m. His greenhouse is very narrow and can be viewed as an infinite line, with each plant occupying a single point on that line.

Emuskald has discovered that each species thrives at a different temperature, so he wants to arrange m - 1 borders that would divide the greenhouse into m sections numbered from 1 to m from left to right with each section housing a single species. He is free to place the borders, but in the end all of the i-th species plants must reside in i-th section from the left.

Of course, it is not always possible to place the borders in such way, so Emuskald needs to replant some of his plants. He can remove each plant from its position and place it anywhere in the greenhouse (at any real coordinate) with no plant already in it. Since replanting is a lot of stress for the plants, help Emuskald find the minimum number of plants he has to replant to be able to place the borders.

Input

The first line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 5000, n ≥ m), the number of plants and the number of different species. Each of the following n lines contain two space-separated numbers: one integer number si (1 ≤ si ≤ m), and one real number xi (0 ≤ xi ≤ 109), the species and position of the i-th plant. Each xi will contain no more than 6 digits after the decimal point.

It is guaranteed that all xi are different; there is at least one plant of each species; the plants are given in order "from left to the right", that is in the ascending order of their xi coordinates (xi < xi + 1, 1 ≤ i < n).

Output

Output a single integer — the minimum number of plants to be replanted.

Examples
input
3 2
2 1
1 2.0
1 3.100
output
1
input
3 3
1 5.0
2 5.5
3 6.0
output
0
input
6 3
1 14.284235
2 17.921382
1 20.328172
3 20.842331
1 25.790145
1 27.204125
output
2
Note

In the first test case, Emuskald can replant the first plant to the right of the last plant, so the answer is 1.

In the second test case, the species are already in the correct order, so no replanting is needed.

 【题意】给你n朵花共m种,在实数坐标轴上依次排开。现在要移动最少的花的数量,使得重排后的花相同种类逇相邻,且从左到右种类编号是1~n.

【分析】DP。设dp[i][j]表示前i个排列好以编号为j的物种结尾所移动的最少次数,对于a[i+1],如果a[i+1]>=j,那么a[i+1]可以不动也可以移到后面某一正确位置;如果a[i+1]<j则将a[i+1]移动到前面正确位置。转移方程为:

if  a[i]>=j
dp[i][a[i]] = min(dp[i - 1][j], dp[i][a[i]]); dp[i][j] = min(dp[i - 1][j]+1, dp[i][j]);
else  dp[i][j] = min(dp[i - 1][j]+1, dp[i][j]);

其实可以直接求最长非减子序列,这里麻烦了。

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long LL;
const int N = 5e3+50;
const int mod = 1e9+7;
int n,m;
int dp[N][N],a[N];
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        double x;
        scanf("%d%lf",&a[i],&x);
    }
    met(dp,inf);
    dp[0][0]=0;
    for(int i=1;i<=n;i++){
        for(int j=0;j<=a[i];j++){
            dp[i][a[i]]=min(dp[i][a[i]],dp[i-1][j]);
            dp[i][j]=min(dp[i][j],dp[i-1][j]+1);
        }
        for(int j=a[i]+1;j<=m;j++){
            dp[i][j]=min(dp[i][j],dp[i-1][j]+1);
        }
    }
    int ans=inf;
    for(int i=1;i<=m;i++){
        ans=min(ans,dp[n][i]);
    }
    printf("%d\n",ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/jianrenfang/p/7226245.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值