zoukankan      html  css  js  c++  java
  • codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)(将一个数组变成严格单增数组的最少步骤)

    E. 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
    output
    input
    output
    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

     而本题的严格如何转化非严格,直接加一行代码,a[i]-=i;

    原理

    推导过程大致如下:a[i] < a[i+1] —> a[i] <= a[i+1]-1 —> a[i]-i <= a[i+1]-(i+1)—> b[i]<=b[i+1]

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #define inf 0x3f3f3f3f
    #define met(a,b) memset(a,b,sizeof a)
    #define pb push_back
    typedef long long ll;
    using namespace std;
    const int N = 3e3+5;
    const int M = 24005;
    int n,a[N],b[N];
    ll dp[N][N];
    
    int  main(){
      scanf("%d",&n);
      for(int i=1; i<=n; i++){
        scanf("%d",&a[i]);
        a[i]-=i;
        b[i]=a[i];
      }
      sort(b+1, b+n+1);
      for(int i=1; i<=n; i++){
        ll minn=dp[i-1][1];
        for(int j=1; j<=n; j++){
          minn=min(minn, dp[i-1][j]);
          dp[i][j]=abs(a[i]-b[j])+minn;
        }
      }
      ll ans=dp[n][1];
      for(int i=2; i<=n; i++){
        ans=min(ans, dp[n][i]);
      }
      printf("%lld
    ",ans);
      return 0;
    }
  • 相关阅读:
    报表中的图形是怎么做出来的?如何在其中实现地图?
    多维分析的本质计算是什么?能做什么又不能做什么?
    报表工具对比选型系列用例——多源分片报表
    报表工具怎样适应移动端?
    报表工具怎样适应移动端?
    互联网技术部门该如何管理
    更好用 更简单的Java缓存框架 jscache
    古人是怎么排行
    Spark ML 机器学习之Word2Vec
    Spark ML之高级数据源
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6493038.html
Copyright © 2011-2022 走看看