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 ...
随机推荐
- centos安装samba
yum -y install samba samba-client samba-swat /etc/init.d/smb start chkconfig --level 35 smb on cp -p ...
- lnmp之mysql5.5.17安装
先执行命令yum install cmake mysql5.5采用的是cmake安装(更先进的configure) wget下载目录(到清华大学的镜像站下载) [root@localhost loca ...
- VS2010的快捷键;VS2012变化的快捷键
注释::VS2010是(Ctrl+E,C),VS2012是(Ctrl+K, Ctrl+C),实际操作,按住Ctrl键不放,先按K键,再按C键.相当于Ctrl+K加 Ctrl+C的组合键 反註解:VS2 ...
- Android (Android Studio)无法启动adb 解决方案
打开cmd 输入: netstat -aon|findstr "5037" 回车 taskkill /pid xxxx /f ps:xxxx为占用端口 ...
- Python2 和Python3 的差异总结
一.基本语法差异 1.1 核心类差异 Python3对Unicode字符的原生支持 Python2中使用 ASCII 码作为默认编码方式导致string有两种类型str和unicode,Python3 ...
- tomcat和jetty区别
参见:https://www.cnblogs.com/fengli9998/p/7247559.html 1. Jetty更轻量级.这是相对Tomcat而言的. 由于Tomcat除了遵循Java Se ...
- 发布QT exe
https://blog.csdn.net/u014453443/article/details/85837138
- 远程登录 dos命令
1.桌面连接命令 mstsc /v: 192.168.1.250 /console 2.若需要远程启动所有Internet服务,可以使用iisreset命令来实现. 进入“命令提示符”窗口.在提示符后 ...
- (八)git更改提交操作
1.git reset --hard + hash值 2.git reflog 查看当前仓库的操作日志 3.git commit --amend 修改提交信息(上一条) 4.git rebase -i ...
- nginx服务器的rewrite功能
nginx编译的时候默认是把rewrite功能编辑进去的,但是因为rewrite需要使用正则表达式,因此需要安装pcre依赖包. yum install -y pcre pcre-install re ...