zoukankan      html  css  js  c++  java
  • Kirill the Gardener 3 URAL

    2072. Kirill the Gardener 3
    Time limit: 2.0 second
    Memory 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 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 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: 183    Printable version    Submit solution    Discussion (5)
    All submissions (2728)    All accepted submissions (905)    Solutions rating (567)




    整个数组有多个数组成,先考虑每一种树,一定是从这个数的一头走到另一头最有,所以:

    (从小到大)按照权值排序,再按照位置排序;
    分成若干区间d[i]:保存区间左右端点;(中间的一定会经过);
    dp[i][0]:表示从区间左端点到右端点;
    dp[i][1]:表示从区间右端点到左端点;
    状态转移方程:
    dp[i][0] = min(dp[i - 1][0] + abs(d[i - 1].rx - d[i].lx), dp[i - 1][1] + abs(d[i - 1].lx - d[i].lx)) + sum;
    dp[i][1] = min(dp[i - 1][0] + abs(d[i - 1].rx - d[i].rx), dp[i - 1][1] + abs(d[i - 1].lx - d[i].rx)) + sum;
    sum = abs(d[i].rx - d[i].lx);




     1 #include<bits/stdc++.h>
     2 #include<algorithm>
     3 #define ll long long
     4 using namespace std;
     5 const int maxn = 1e5+7;
     6 int n, tot;
     7 ll dp[maxn][2];
     8 
     9  
    10 struct node{
    11   int lx, rx;
    12 }id[maxn], d[maxn];
    13  
    14 bool cmp(struct node a, struct node b)
    15 {
    16     if(a.lx != b.lx) return a.lx < b.lx;
    17     else return a.rx < b.rx;
    18 }
    19  
    20  
    21 int main()
    22 {
    23     //freopen("in.txt", "r", stdin);
    24     while(~scanf("%d", &n))
    25     {
    26         tot = 1;
    27         for(int i = 0; i < n ; i++) {
    28             id[i].rx = i;
    29             scanf("%d", &id[i].lx);
    30         }
    31         sort(id, id+n, cmp);
    32         d[tot].lx = d[tot].rx = id[0].rx;
    33         for(int i = 1; i < n; i++)
    34         {
    35             if(id[i].lx == id[i - 1].lx) d[tot].rx = id[i].rx;
    36             else {
    37                 tot++;
    38                 d[tot].lx = d[tot].rx = id[i].rx;
    39             }
    40         }
    41         memset(dp, 0, sizeof(dp));
    42         for(int i = 1; i <= tot; i++)
    43         {
    44             int sum = abs(d[i].rx - d[i].lx);
    45             dp[i][0] = min(dp[i - 1][0] + abs(d[i - 1].rx - d[i].lx), dp[i - 1][1] + abs(d[i - 1].lx - d[i].lx)) + sum;
    46             dp[i][1] = min(dp[i - 1][0] + abs(d[i - 1].rx - d[i].rx), dp[i - 1][1] + abs(d[i - 1].lx - d[i].rx)) + sum;
    47         }
    48         printf("%lld
    ",min(dp[tot][0], dp[tot][1]) + n);
    49     }
    50     return 0;
    51 }
  • 相关阅读:
    mybatis之mapper.xml分析
    mybati之运行过程
    ibatis中的resultClass,parameterClass,resultMap,resultType的使用与区别
    mybatis之parameterType传递多个参数
    mybati之#与$的区别
    【Java面试题】20 运行时异常和一般异常有何区别
    【Java面试题】19 final,finally和finalize的区别
    【Java面试题】18 java中数组有没有length()方法?string没有lenght()方法?下面这条语句一共创建了多少个对象:String s="a"+"b"+"c"+"d";
    【Java面试题】17 如何把一个逗号分隔的字符串转换为数组? 关于String类中split方法的使用,超级详细!!!
    【Java面试题】16 静态代码块,main方法,构造代码块,构造方法
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/10760246.html
Copyright © 2011-2022 走看看