URAL - 2072 Kirill the Gardener 3 dp
2017-04-01 21:21
183 查看
2072. Kirill the Gardener 3
Time limit: 2.0 secondMemory limit: 64 MB
Kirill the gardener has got a new task. He has to water the flowers growing on the huge flowerbed! It should be mentioned that the bed is very long and narrow at the same time. So from bird’s-eye view
(and Kirill growth too) it looks like a straight line, where n points-flowers are located at regular intervals. For this job Kirill has a watering pot of infinite volume and smartwatch that shows moisture content of each flower before watering. The watering
takes too much time, so the most dried flowers can die, which is unacceptable. So Kirill decided to water the flowers in order of their non-decreasing dryness. On the other hand, he wants to finish the watering as soon as possible, because there are a lot
of other interesting things.
Assume that watering of one flower and walking between two neighbor flowers takes Kirill one minute. Can you figure out the time in which the young gardener will complete his job if he acts optimally?
Initially Kirill stands near the leftmost flower.
Input
The first line contains an integer n (1 ≤ n ≤ 105) — it’s amount of flowers in the flowerbed. The secondline contains n integers separated by spaces — it‘s moisture content of flowers given in order of their positions in the flowerbed from left to right. Moisture content is an integer from 1 up to 109 (including
both).
Output
In the only line print an integer — the minimal time in which Kirill would complete watering.Sample
input | output |
---|---|
6 3 2 5 6 2 5 | 21 |
Notes
There is one of the possible ways to finish up in 21 minutes in the example:Go from the 1st to the 5th flower (4 minutes)
Water the 5th flower (1 minute)
Go from the 5th to the 2nd flower (3 minutes)
Water the 2nd flower (1 minute)
Go from the 2nd to the 1st flower (1 minute)
Water the 1st flower (1 minute)
Go from the 1st flower to the 3rd flower (2 minutes)
Water the 3rd flower (1 minute)
Go from the 3rd flower to the 6th flower (3 minutes)
Water the 6th flower (1 minute)
Go from the 6th flower to the 4th flower (2 minutes)
Water the 4th flower (1 minute)
Problem Author: Ilya Kuchumov
Problem Source: Ural Regional School Programming Contest 2015
Tags: dynamic programming (
hide tags for unsolved problems
)
Difficulty: 186 Printable version Submit
solution Discussion (3)
All submissions (2095) All
accepted submissions (669) Solutions rating (419)
可以把一样的数看为一层,必须走完这一层才可以走下一层,然后就是选择每一层从左先走还是从右先走
#include<bits/stdc++.h>
using namespace std;
int l[100005],r[100005],a[100005],b[100005];
long long int dp[2][2];
map<int,int>mp;
int cmp(int u,int v)
{
return u<v;
}
int main()
{
int i,index,n,a[100005],t=1;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+1,b+n+1,cmp);
for(i=1;i<=n;i++)
{
if(!mp.count(b[i]))
{
mp[b[i]]=t++;
}
}
// printf("%d\n",t);
for(i=1;i<=n;i++)
{
index=mp[a[i]];
if(l[index]==0)
{
l[index]=r[index]=i;
}
else
{
r[index]=i;
}
}
long long int ans=0;
for(i=1;i<t;i++)
{
ans+=r[i]-l[i];
// printf("%d %d\n",l[i],r[i]);
}
for(i=1;i<t;i++)
{
if(i==1)
{
dp[0][0]=abs(l[i]-1);
dp[0][1]=abs(r[i]-1);
}
else
{
dp[1][0]=min(dp[0][0]+abs(l[i]-r[i-1]),dp[0][1]+abs(l[i]-l[i-1]));
dp[1][1]=min(dp[0][0]+abs(r[i]-r[i-1]),dp[0][1]+abs(r[i]-l[i-1]));
dp[0][1]=dp[1][1];
dp[0][0]=dp[1][0];
}
// printf("%d %d %d %d\n",dp[0][0],dp[0][1],dp[1][0],dp[1][1]);
}
printf("%lld\n",min(dp[1][0],dp[1][1])+n+ans);
return 0;
}
相关文章推荐
- URAL 1143 Electric Path(带方向的区间DP + 记忆化搜索)
- URAL 1519 Formula 1 【插头DP模板题】
- URAL 1353 Milliard Vasya's Function(数位DP)
- URAL 1036 Lucky Tickets(基础dp)
- Ural_1017. Staircases(DP)
- 数位DP-URAL-1057-Amount of Degrees
- URAL1009 - K-based Numbers - 数位dp
- URAL 1142. Relations(dp啊)
- Binary Apple Tree_ural1018_树状dp
- 树形dp ural1018苹果二叉树
- DP+路径回溯 timus 1078. Segments URAL 解题报告
- Ural 1018 Binary Apple Tree (树形DP)
- ural 1057(数位dp)
- 数位DP入门Ural1057
- [BZOJ]|[Ural] Formula 1-----插头DP入门
- Ural 1119 Metro(DP)
- URAL - 1036 Lucky Tickets(DP+大数)
- URAL 1036(数位dp)
- Ural 1152 False Mirrors(状压DP)
- URAL 1114 Boxes (dp)