Codeforces Round #165 (Div. 1) Greenhouse Effect(DP)
2 seconds
256 megabytes
standard input
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.
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 a single integer — the minimum number of plants to be replanted.
3 2
2 1
1 2.0
1 3.100
1
3 3
1 5.0
2 5.5
3 6.0
0
6 3
1 14.284235
2 17.921382
1 20.328172
3 20.842331
1 25.790145
1 27.204125
2
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)的更多相关文章
- 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 ...
- Codeforces Round #658 (Div. 2) D. Unmerge(dp)
题目链接:https://codeforces.com/contest/1382/problem/D 题意 给出一个大小为 $2n$ 的排列,判断能否找到两个长为 $n$ 的子序列,使得二者归并排序后 ...
- Codeforces Round #471 (Div. 2) F. Heaps(dp)
题意 给定一棵以 \(1\) 号点为根的树.若满足以下条件,则认为节点 \(p\) 处有一个 \(k\) 叉高度为 \(m\) 的堆: 若 \(m = 1\) ,则 \(p\) 本身就是一个 \(k\ ...
- 【Codeforces】Codeforces Round #374 (Div. 2) -- C. Journey (DP)
C. Journey time limit per test3 seconds memory limit per test256 megabytes inputstandard input outpu ...
- Codeforces Round #652 (Div. 2) D. TediousLee(dp)
题目链接:https://codeforces.com/contest/1369/problem/D 题意 最初有一个结点,衍生规则如下: 如果结点 $u$ 没有子结点,添加 $1$ 个子结点 如果结 ...
- Codeforces Round #247 (Div. 2) C. k-Tree (dp)
题目链接 自己的dp, 不是很好,这道dp题是 完全自己做出来的,完全没看题解,还是有点进步,虽然这个dp题比较简单. 题意:一个k叉树, 每一个对应权值1-k, 问最后相加权值为n, 且最大值至少为 ...
- 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 ...
- Codeforces Round #368 (Div. 2) B. Bakery (模拟)
Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...
- Codeforces Round #262 (Div. 2) 460C. Present(二分)
题目链接:http://codeforces.com/problemset/problem/460/C C. Present time limit per test 2 seconds memory ...
随机推荐
- 2015/8/10 Python基本使用(1)
此文为<Python核心编程>的读书笔记记录. Python是一门解释性语言,所有的语句用解释器(interpreter)来直接解释,但它同时是High Level的语言,这样的组成能够在 ...
- [洛谷P3444] [POI2006]ORK-Ploughing
洛谷题目链接[POI2006]ORK-Ploughing 题目描述 Byteasar, the farmer, wants to plough his rectangular field. He ca ...
- mysql 在查询结果中进行二次查询
第一次查询:查询身份证编号和出现次数 select cardid,count(cardid) as total from p_person_info group by cardid 在第一次查询结果进 ...
- 策略模式 C#
策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户而独立变化. 抽象策略角色: 策略类,通常由一个接口或者抽象类实现. 具体策略角色:包装了 ...
- 崩坏3mmd中的渲染技术研究
http://youxiputao.com/articles/11839 主要是参考该篇文章做一个微小的复盘. 漫反射与高光 文章中的漫反射与高光并不是类似于普通的 resultCol = Diffu ...
- Shuffle Cards(牛客第三场+splay)
题目: 题意:将1~n的数进行m次操作,每次操作将第pi位到pi+si-1位的数字移到第一位,求最后的排列. 思路:现在还没不会写splay,在知道这是splay模板题后找了一波别人的模板,虽然过了, ...
- JS对象操作
一.String常用操作 1.截取 substr(start,length) //返回从指定位置开始的指定长度的字符串. substring(start,end) //返回两个指定的位置之间的字符串. ...
- bzoj 1050 并查集
先按边长排序,假设s与t连通,那么我们可以枚举s与t的路径中最短的一条边,通过类似与kruskal的方法找到s与t的路径在当前最小边权情况下尽量小的最大边权,用这个比值更新答案. 特别的,我们对于某一 ...
- device tree property ---- interrupt-names
device tree source 的 interrupt-names property 會對應到 pltform_get_irq_byname() 的第二個參數. .dtsi or .dts in ...
- Redis 主从部署
Redis 主从部署 http://www.xuchanggang.cn/archives/978.html