URAL 2072 Kirill the Gardener 3
思路:
dp+离散化
由于湿度的范围很大,所以将湿度离散化
可以证明,先到一种湿度的最左端或者最右端,然后结束于最右端或最左端最优,因为如果结束于中间,肯定有重复走的路
状态:dp[i][0]表示湿度为i结束于左端最优的步数
dp[i][1]表示湿度为i结束于右端最优的步数
初始状态:dp[0][0]=dp[0][1]=0
状态转移:
dp[i][0]=min(dp[i][0],dp[i-1][0]+abs(prel-nowr)+abs(nowl-nowr));
dp[i][0]=min(dp[i][0],dp[i-1][1]+abs(prer-nowr)+abs(nowl-nowr));
dp[i][1]=min(dp[i][1],dp[i-1][0]+abs(prel-nowl)+abs(nowl-nowr));
dp[i][1]=min(dp[i][1],dp[i-1][1]+abs(prer-nowl)+abs(nowl-nowr));
prel和prer表示上一种湿度的最左端和最右端
nowl和nowr表示当前湿度的最左端和最右端
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)) const int N=1e5+;
const ll INF=0x3f3f3f3f3f3f3f3f;
int a[N];
ll dp[N][];
vector<int>pos[N];
vector<int>sz;
int main(){
ios::sync_with_stdio(false);
cin.tie();
int n;
cin>>n;
for(int i=;i<=n;i++)cin>>a[i],sz.pb(a[i]);
sort(sz.begin(),sz.end());
sz.erase(unique(sz.begin(),sz.end()),sz.end());
for(int i=;i<=n;i++){
int t=lower_bound(sz.begin(),sz.end(),a[i])-sz.begin()+;
pos[t].pb(i);
}
mem(dp,INF);
dp[][]=dp[][]=;
int prel=,prer=;
for(int i=;i<=sz.size();i++){
int nowl=pos[i][],nowr=pos[i][pos[i].size()-];
dp[i][]=min(dp[i][],dp[i-][]+abs(prel-nowr)+abs(nowl-nowr));
dp[i][]=min(dp[i][],dp[i-][]+abs(prer-nowr)+abs(nowl-nowr));
dp[i][]=min(dp[i][],dp[i-][]+abs(prel-nowl)+abs(nowl-nowr));
dp[i][]=min(dp[i][],dp[i-][]+abs(prer-nowl)+abs(nowl-nowr));
prel=nowl,prer=nowr;
}
cout<<min(dp[sz.size()][],dp[sz.size()][])+n<<endl;
return ;
}
URAL 2072 Kirill the Gardener 3的更多相关文章
- URAL 2072 Kirill the Gardener 3 (单调DP)
[题目链接] http://acm.timus.ru/problem.aspx?space=1&num=2072 [题目大意] 一个园丁要给一排花浇水,每个花都有一个标号,必须要先浇标号小的, ...
- 2072. Kirill the Gardener 3
http://acm.timus.ru/problem.aspx?space=1&num=2072 回忆一下 #include <iostream> #include <st ...
- Ural 2072:Kirill the Gardener 3(DP)
http://acm.timus.ru/problem.aspx?space=1&num=2072 题意:有n朵花,每朵花有一个饥渴值.现在浇花,优先浇饥渴值小的(即从小到大浇),浇花需要耗费 ...
- ural 2064. Caterpillars
2064. Caterpillars Time limit: 3.0 secondMemory limit: 64 MB Young gardener didn’t visit his garden ...
- ural 2073. Log Files
2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...
- ural 2066. Simple Expression
2066. Simple Expression Time limit: 1.0 secondMemory limit: 64 MB You probably know that Alex is a v ...
- URAL 2040 Palindromes and Super Abilities 2(回文树)
Palindromes and Super Abilities 2 Time Limit: 1MS Memory Limit: 102400KB 64bit IO Format: %I64d ...
- 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome
题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...
- ural 2071. Juice Cocktails
2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...
随机推荐
- try except else
try except 语句还有一个可选的else子句,如果使用这个子句,那么必须放在所有的except子句之后.这个子句将在try子句没有发生任何异常的时候执行.例如: for arg in sys. ...
- MFC CFile类读写文件详解
CFile类提供了对文件进行打开,关闭,读,写,删除,重命名以及获取文件信息等文件操作的基本功能,足以处理任意类型的文件操作. 一个读写文件的例子: 文件I/O 虽然使用CArchive类内建的序列化 ...
- POJ 1182 并查集
Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到 ...
- 错误源:.net SqlClient data provider
下午在做毕业设计的时候,想删除数据库的一条数据,结果发现删除的时候老是出现 ======错误源:.net SqlClient data provider==== 这样的错误:本来以为是我还在运行着项目 ...
- SpringMVC+Spring+Mybatis+Maven+mysql整合
一.准备工作1.工具:jdk1.7.0_80(64)+tomcat7.0.68+myeclipse10.6+mysql-5.5.48-win322. 开发环境安装配置.Maven项目创建(参考:htt ...
- Linux服务器配置---tftpserver
安装tftp-server 1.安装tftp-server [root@localhost weijie]# yum install -y tftp-server Loaded plugins: fa ...
- 有趣的js匿名函数写法(function嵌套)
例子没有什么实际意义,只能做为思路参考 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&quo ...
- 史上最全的HTML和CSS标签常用命名规则
文件夹主要建立以下文件夹: 1.Images 存放一些网站常用的图片: 2.Css 存放一些CSS文件: 3.Flash 存放一些Flash文件: 4.PSD 存放一些PSD源文件: 5.Temp 存 ...
- 计算概论(A)/基础编程练习1(8题)/8:与7无关的数
#include<stdio.h> int main() { ; // n < 100 scanf("%d", &n); // 循环遍历判断 再进行平方和 ...
- python集合set{ }、集合函数及集合的交、差、并
通过大括号括起来,用逗号分隔元素,特点 1.由不同元素组成,如果定义时存在相同元素,处理时会自动去重 2.无序 3.元素只能是不可变类型,即数字.字符串.布尔和元组,但集合本身可变 4.可直接定义集合 ...