Kirill the Gardener 3 URAL 2072

本文介绍了一个关于浇水花园中花朵的算法问题。园丁Kirill需要根据花朵的干湿程度为它们浇水,并尽量减少总的行动时间。文章通过动态规划的方法解决这一问题,详细展示了如何计算最优路径及所需时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 ≤ 10 5) — it’s amount of flowers in the flowerbed. The second line 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 10 9 (including both).
Output
In the only line print an integer — the minimal time in which Kirill would complete watering.
Example
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)

一开始觉得数组太大不能dp其实可以,因为无论怎么走 在同一个序号阶段 走最少的路程必定是从最左到最右或者从最右到最左,最终停在一边。那么就可以dp了

#include <bits/stdc++.h>
using namespace std;
struct node
{
    long long val,x;
}s[101010];
long long dp[101010][2];
int cmp(node a,node b)
{
    if(a.val==b.val)
    {
        return a.x<b.x;
    }
    else 
        return a.val<b.val;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
            scanf("%lld",&s[i].val);
            s[i].x=i;
    }
    sort(s+1,s+1+n,cmp);
    int num=0;
    for(int i=1;i<=n;i++)
    {
        if(s[i].val!=s[i-1].val)
            num++;
    }
    long long prel=0;
    long long prer=0;
    int tot=1;
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i++)
    {
        int j=i+1;
        while(s[j].val==s[i].val&&j<=n) j++;
        j--;
        long long t=abs(s[j].x-s[i].x);
        // printf("%d %d %d %d\n",prel,prer,s[i].x,s[j].x );
        // dp[tot][0]=dp[tot-1][0]+abs(prel-s[j].x)+t;
        // dp[tot][0]=min(dp[tot][0],dp[tot-1][1]+abs(prer-s[j].x)+t);
        // dp[tot][1]=dp[tot-1][1]+abs(prer-s[i].x)+t;
        // dp[tot][1]=min(dp[tot][1],dp[tot-1][0]+abs(prel-s[i].x)+t);  
        dp[tot][0]=t+min(abs(s[j].x-prel)+dp[tot-1][0],abs(s[j].x-prer)+dp[tot-1][1]);
        dp[tot][1]=t+min(abs(s[i].x-prel)+dp[tot-1][0],abs(s[i].x-prer)+dp[tot-1][1]);
        // printf("%d %d\n",dp[tot][0],dp[tot][1] );
        tot=tot+1;
        prel=s[i].x,prer=s[j].x;
        i=j;
    }
    printf("%lld",min(dp[num][0],dp[num][1])+n-1);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值