zoukankan      html  css  js  c++  java
  • Codeforces 713C Sonya and Problem Wihtout a Legend DP

    C. Sonya and Problem Wihtout a Legend
    time limit per test
    5 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Sonya was unable to think of a story for this problem, so here comes the formal description.

    You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0.

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ 3000) — the length of the array.

    Next line contains n integer ai (1 ≤ ai ≤ 109).

    Output

    Print the minimum number of operation required to make the array strictly increasing.

    Examples
    input
    7
    2 1 5 11 5 9 11
    output
    9
    input
    5
    5 4 3 2 1
    output
    12
    Note

    In the first sample, the array is going to look as follows:

    11

    |2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9

    And for the second sample:

    5

    |5 - 1| + |4 - 2| + |3 - 3| + |2 - 4| + |1 - 5| = 12

     

    题意:求一个序列变为严格递增序列的最小花费。

    思路:这题和POJ3666本质相似,POJ3666求的是非严格递增,在这题的条件下,我们只要将a[i]-i预处理,再按照相同的做法就可以保证严格递增了。QD某校赛比赛时候看到这题就觉得好像,结果一直没有想出来怎么改,POJ那题还是最近做过的呐。/.另外这题还可以用优先队列做...

    /** @Date    : 2016-11-26-17.16
      * @Author  : Lweleth (SoungEarlf@gmail.com)
      * @Link    : https://github.com/
      * @Version :
      */
    
    #include<bits/stdc++.h>
    #define LL long long
    #define MMF(x) memset((x),0,sizeof(x))
    #define MMI(x) memset((x), INF, sizeof(x))
    using namespace std;
    
    const int INF = 0x3f3f3f3f;
    const int N = 1e5+2000;
    
    int a[3030];
    int b[3030];
    LL dp[3030][3030];
    int main()
    {
        int n;
        while(~scanf("%d", &n))
        {
            MMF(dp);
            for(int i = 1; i <= n; i++)
            {
                scanf("%d", a + i);
                a[i] -= i;
                b[i] = a[i];
            }
            sort(b + 1, b + 1 + n);
            for(int i = 1; i <= n; i++)
            {
                dp[i][1] =  dp[i - 1][1] + abs(a[i] - b[1]);
                for(int j = 2; j <= n; j++)
                {
                    dp[i][j] = min(dp[i - 1][j] + abs(a[i] - b[j]), dp[i][j - 1]);
                }
            }
            printf("%lld
    ", dp[n][n]);
        }
        return 0;
    }
    
    
  • 相关阅读:
    PHP基础函数、自定义函数以及数组
    php 中 isset 和empty 的区别
    mysql中索引的使用
    mysql 中的增改查删(CRUD)
    mysql语句应该注意的问题
    《zw版·Halcon-delphi系列原创教程》 水果自动分类脚本(机器学习、人工智能)
    《zw版·Halcon-delphi系列原创教程》 2d照片-3d逆向建模脚本
    《zw版·Halcon-delphi系列原创教程》简单的令人发指,只有10行代码的车牌识别脚本
    《zw版·Halcon-delphi系列原创教程》航母舰载机·视觉定位标志的识别代码
    zw版_zw中文增强版Halcon官方Delphi例程
  • 原文地址:https://www.cnblogs.com/Yumesenya/p/6109128.html
Copyright © 2011-2022 走看看