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+;
const int mod = 1e9+;
int n,m;
int dp[N][N],a[N];
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
double x;
scanf("%d%lf",&a[i],&x);
}
met(dp,inf);
dp[][]=;
for(int i=;i<=n;i++){
for(int j=;j<=a[i];j++){
dp[i][a[i]]=min(dp[i][a[i]],dp[i-][j]);
dp[i][j]=min(dp[i][j],dp[i-][j]+);
}
for(int j=a[i]+;j<=m;j++){
dp[i][j]=min(dp[i][j],dp[i-][j]+);
}
}
int ans=inf;
for(int i=;i<=m;i++){
ans=min(ans,dp[n][i]);
}
printf("%d\n",ans);
return ;
}

Codeforces Round #165 (Div. 1) Greenhouse Effect(DP)的更多相关文章

  1. Codeforces Round #260 (Div. 2)C. Boredom(dp)

    C. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  2. Codeforces Round #658 (Div. 2) D. Unmerge(dp)

    题目链接:https://codeforces.com/contest/1382/problem/D 题意 给出一个大小为 $2n$ 的排列,判断能否找到两个长为 $n$ 的子序列,使得二者归并排序后 ...

  3. Codeforces Round #471 (Div. 2) F. Heaps(dp)

    题意 给定一棵以 \(1\) 号点为根的树.若满足以下条件,则认为节点 \(p\) 处有一个 \(k\) 叉高度为 \(m\) 的堆: 若 \(m = 1\) ,则 \(p\) 本身就是一个 \(k\ ...

  4. 【Codeforces】Codeforces Round #374 (Div. 2) -- C. Journey (DP)

    C. Journey time limit per test3 seconds memory limit per test256 megabytes inputstandard input outpu ...

  5. Codeforces Round #652 (Div. 2) D. TediousLee(dp)

    题目链接:https://codeforces.com/contest/1369/problem/D 题意 最初有一个结点,衍生规则如下: 如果结点 $u$ 没有子结点,添加 $1$ 个子结点 如果结 ...

  6. Codeforces Round #247 (Div. 2) C. k-Tree (dp)

    题目链接 自己的dp, 不是很好,这道dp题是 完全自己做出来的,完全没看题解,还是有点进步,虽然这个dp题比较简单. 题意:一个k叉树, 每一个对应权值1-k, 问最后相加权值为n, 且最大值至少为 ...

  7. Codeforces Round #119 (Div. 2) Cut Ribbon(DP)

    Cut Ribbon time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  8. Codeforces Round #368 (Div. 2) B. Bakery (模拟)

    Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...

  9. Codeforces Round #262 (Div. 2) 460C. Present(二分)

    题目链接:http://codeforces.com/problemset/problem/460/C C. Present time limit per test 2 seconds memory ...

随机推荐

  1. Java包名命名规则

    1.  sun公司建议java包的命名规则为域名的倒写. 比如:sun公司www.sun.com 就用该是com.sun.www 2. indi : 个体项目,指个人发起,但非自己独自完成的项目,可公 ...

  2. C11简洁之道:函数绑定

    1.  可调用对象 在C++中,有“可调用对象”这么个概念,那么什么是调用对象呢?有哪些情况?我们来看看: 函数指针: 具有operator()成员函数的类对象(仿函数): 可以被转换为函数指针的类对 ...

  3. Spring理论基础-控制反转和依赖注入

    第一次了解到控制反转(Inversion of Control)这个概念,是在学习Spring框架的时候.IOC和AOP作为Spring的两大特征,自然是要去好好学学的.而依赖注入(Dependenc ...

  4. 14、char和varchar的区别?

    就长度来说: ♣ char的长度是不可变的; ♣ 而varchar的长度是可变的,也就是说,定义一个char[10]和varchar[10],如果存进去的是‘csdn’,那么char所占的长度依然为1 ...

  5. TensorFlow中get_variable共享变量调用

    import tensorflow as tf with tf.variable_scope('v_scope',reuse=True) as scope1: Weights1 = tf.get_va ...

  6. Java的Integer常量池和String常量池

    1.Integer的常量池 看下面一段代码: package cn.qlq.test; public class ArrayTest { public static void main(String[ ...

  7. c语言中网络字节序和主机字节序的转换

    函数说明   相关函数:htonl, htons, ntohl 头文件:#include <netinet/in.h> 定义函数:unsigned short int ntohs(unsi ...

  8. arm---先搞清楚各种版本号【转】

    转自:http://blog.csdn.net/linnoo/article/details/53214689 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] ARM的几种版本 ...

  9. Qualcomm platform, the commonly used parameters of charger and battery in device tree file

    Platform MSM8917 PM8937 PMI8940 Parameters 1 battery charging voltage : qcom,float-voltage-mv = < ...

  10. 大原則 研讀 spec 與 code 的 心得

    最近在研究 stm32f429i-disc0 的 device tree source code, 並且 參造 Devicetree Specification Release 0.1, 在 dts ...