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 ...
随机推荐
- Object之wait
一.源码. 1.公共本地,且可继承但不可重写. 2.公共,且可继承但不可重写. 3.公共,且可继承但不可重写. 二.解释. 1.因为这些方法是Object类中的非静态的public方法,而任何其他类都 ...
- Python - matplotlib 数据可视化
在许多实际问题中,经常要对给出的数据进行可视化,便于观察. 今天专门针对Python中的数据可视化模块--matplotlib这块内容系统的整理,方便查找使用. 本文来自于对<利用python进 ...
- only_full_group_by问题而引发的对group by的深入思考
问题背景 最近在项目中使用mysql的group by进行分组查询的场景比较多,其中一次遇到了一个问题,即在开发环境执行一个如下sql时是正确且可执行的, select a,b,max(c) from ...
- UVA10066
/* 最长公共子序列 */ #include <cstdio> #include <string.h> #include <iostream> const int ...
- Filter过滤器原理和登录实现
Filter过滤器API Servlet过滤器API包含了3个接口,它们都在javax.servlet包中,分别是Filter接口.FilterChain接口和FilterConfig接口. ...
- Python入门学习之路,怎么 “开心,高效,踏实” 地把Python学好?兴趣,兴趣,兴趣!
Python入门学习之路,怎么 “开心,高效,踏实” 地把Python学好?兴趣,兴趣,兴趣!找到你自己感兴趣的点进行切入,并找到兴趣点进行自我驱动是最好的学习方式! 推荐两本书,一本作为 ...
- python之路----面向对象进阶一
一.isinstance和issubclass isinstance(obj,cls)检查是否obj是否是类 cls 的对象 class Foo(object): pass obj = Foo() i ...
- python进程编程
多进程multiprocess模块 multiprocessing is a package that supports spawning processes using an API similar ...
- 第一个c++泛型函数(即模板)
先定义如下: ns.h template <typename T> // 这个关键字typename, 明显多此一举 inline void PRINT_ELEMENTS(const T& ...
- 通过一个例子了解Ajax
Ajax指的Asyncronous JavaScript and XML Ajax并不是什么新的编程语言, 它是现有一些东西的应用.从它的名称中就可以看出来 假如我们设想, 浏览器展示了一个页面,但需 ...