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 ...
随机推荐
- Large Class--过大的类--要重构的信号
如果想利用单个类做太多事情,其内往往就会出现太多实例变量.一旦如此,Duplicated Code也就接踵而至. 解决方法: 1.将类内彼此相关的变量,将它们放在一起.使用Extrac ...
- 【BZOJ4720】【NOIP2016】换教室 [期望DP]
换教室 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description Input 第一行四个整数n,m,v ...
- Linux系统网络基础知识及配置
一:DNS(domain name system)简介 DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而 ...
- Fetch-新一代Ajax API
AJAX半遮半掩的底层API是饱受诟病的一件事情. XMLHttpRequest 并不是专为Ajax而设计的. 虽然各种框架对 XHR 的封装已经足够好用, 但我们可以做得更好. window.fet ...
- vue_真机调试页面
使用vue开发也有一段时间,是说我太懒了,还是说太懒了.得总结总结的. 之前在开发的时候都是,npm run build把页面打包后再上传到代码库上线用手机看页面效果.样式调整,嗯,很麻烦很傻的. 今 ...
- chrome://settings/content
chrome://settings/content C:\Users\用户名\AppData\Roaming\Microsoft\Internet Explorer chrome://version/
- fork与vfork区别
1. 地址空间各段拷贝: fork: 内核为子进程生成新的地址空间结构,拷贝父进程的代码段,数据空间,堆,栈到自身的地址空间,但注意:子进程的代码段并不会分配物理空间,而是指向父进程的代码段物理空间, ...
- make command explaination 編譯命令解釋
Creating .config file make ARCH=arm CROSS_COMPILE=arm-none-eabi- stm32_defconfig 以上命令是 將變數 ARCH=arm, ...
- API(选项/数据 选项/dom)
选项/数据 data 类型: Object | Function 限制: 组件的定义只接受function var data = { a: 1 } // 直接创建一个实例 var vm = new V ...
- html添加新元素兼容和访问
<!DOCTYPE html> <html> <head> <title>Creating an HTML Element</title> ...