• POJ 3666 Making the Grade


                      Making the Grade
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 3436   Accepted: 1608

    Description

    A straight dirt road connects two fields on FJ's farm, but it changes elevation more than FJ would like. His cows do not mind climbing up or down a single slope, but they are not fond of an alternating succession of hills and valleys. FJ would like to add and remove dirt from the road so that it becomes one monotonic slope (either sloping up or down).

    You are given N integers A1, ... , AN (1 ≤ N ≤ 2,000) describing the elevation (0 ≤ Ai ≤ 1,000,000,000) at each of N equally-spaced positions along the road, starting at the first field and ending at the other. FJ would like to adjust these elevations to a new sequence B1, . ... , BN that is either nonincreasing or nondecreasing. Since it costs the same amount of money to add or remove dirt at any position along the road, the total cost of modifying the road is

    |AB1| + |AB2| + ... + |AN - BN |

    Please compute the minimum cost of grading his road so it becomes a continuous slope. FJ happily informs you that signed 32-bit integers can certainly be used to compute the answer.

    Input

    * Line 1: A single integer: N
    * Lines 2..N+1: Line i+1 contains a single integer elevation: Ai

    Output

    * Line 1: A single integer that is the minimum cost for FJ to grade his dirt road so it becomes nonincreasing or nondecreasing in elevation.

    Sample Input

    7 1 3 2 4 5 3 9

    Sample Output

    3

    Source

    USACO 2008 February Gold 


    解法1:n^2的DP      最优解可以不出现新的数字的,可以枚举每一位的高度

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 
     8 const int INF=0x3f3f3f3f;
     9 
    10 int n,a[2013],b[2013],dp[2013],m[2013];
    11 
    12 int abs(int a,int b) {return (a-b)>=0?(a-b):(b-a);}
    13 
    14 bool cmp(int a,int b) {return a>b;}
    15 
    16 int main()
    17 {
    18     while(scanf("%d",&n)!=EOF)
    19     {
    20         for(int i=1;i<=n;i++) { scanf("%d",b+i); a[i]=b[i];}
    21         sort(b+1,b+n+1);
    22         memset(dp,0,sizeof(dp));
    23         memset(m,0,sizeof(m));
    24         for(int i=1;i<=n;i++)
    25         {
    26             for(int j=1;j<=n;j++)
    27             {
    28                 dp[j]=m[j]+abs(a[i]-b[j]);
    29                 if(j>1) m[j]=min(dp[j],m[j-1]);
    30                 else m[j]=dp[j];
    31             }
    32         }
    33         int ans1=INF;
    34         for(int i=1;i<=n;i++) ans1=min(ans1,dp[i]);
    35         memset(dp,0,sizeof(dp));
    36         memset(m,0,sizeof(m));
    37         sort(b+1,b+1+n,cmp);
    38         for(int i=1;i<=n;i++)
    39         {
    40             for(int j=1;j<=n;j++)
    41             {
    42                 dp[j]=m[j]+abs(a[i]-b[j]);
    43                 if(j>1) m[j]=min(m[j-1],dp[j]);
    44                 else m[j]=dp[j];
    45             }
    46         }
    47         int ans2=INF;
    48         for(int i=1;i<=n;i++) ans2=min(ans2,dp[i]);
    49         printf("%d
    ",min(ans1,ans2));
    50     }
    51     return 0;
    52 }

    解法2: 左偏树
    一种贪心的解法,将数组划成很多块,每一块的最优解就是该块的中位数。。。。。 所以就是求中位数连续递增的块。。。。
             栈+左偏树维护中位数  解决。。。。

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 
     5 using namespace std;
     6 
     7 #define cls(x) memset((x),0,sizeof(x))
     8 
     9 typedef long long int LL;
    10 
    11 const int maxn=2013;
    12 
    13 struct leftlistheapnode
    14 {
    15     int v,d,l,r;
    16 }heap[maxn];
    17 
    18 int merge(int x,int y)
    19 {
    20     if(!x) return y; if(!y) return x;
    21     if(heap[x].v<heap[y].v) swap(x,y); ///big root heap
    22     heap[x].r=merge(heap[x].r,y);
    23     int l=heap[x].l,r=heap[x].r;
    24     if(heap[l].d<heap[r].d) swap(heap[x].l,heap[x].r);
    25     if(!heap[x].r) heap[x].d=0;
    26     else heap[x].d=heap[r].d+1;
    27     return x;
    28 }
    29 
    30 int stk[maxn],idx,now[maxn],go[maxn],n;
    31 LL s[maxn],b[maxn],ans[2];
    32 
    33 int main()
    34 {
    35     while(scanf("%d",&n)!=EOF)
    36     {
    37         for(int i=1;i<=n;i++) scanf("%d",&heap[i].v);
    38         ans[0]=ans[1]=0;
    39         for(int t=0;t<2;t++)
    40         {
    41             int st=t?1:n;
    42             int ed=t?n+1:0;
    43             int dd=t?1:-1;
    44 
    45             idx=0;
    46             cls(stk);cls(now);cls(go);cls(s);cls(b);
    47             for(int i=0;i<=n+10;i++)
    48             {
    49                 heap[i].l=heap[i].d=heap[i].r=0;
    50             }
    51 
    52             for(int i=st;i!=ed;i+=dd)
    53             {
    54                 ///对每一个数独立入栈
    55                 idx++;
    56                 stk[idx]=i;
    57                 now[i]=1;go[i]=0;
    58                 s[i]=heap[i].v;b[i]=0;
    59 
    60                 while((idx>=1)&&(heap[stk[idx-1]].v>heap[stk[idx]].v))
    61                 {
    62                     ///merge
    63                     int j=stk[idx-1],k=stk[idx];
    64                     stk[idx-1]=merge(j,k);
    65                     idx--;
    66                     int z=stk[idx];
    67                     ///update
    68                     s[z]=s[j]+s[k];
    69                     b[z]=b[j]+b[k];
    70                     now[z]=now[j]+now[k];
    71                     go[z]=go[j]+go[k];
    72                     if((now[z]+go[z]+1)/2<now[z])
    73                     {
    74                         stk[idx]=merge(heap[z].l,heap[z].r);
    75                         b[stk[idx]]=b[z]+heap[z].v;
    76                         s[stk[idx]]=s[z]-heap[z].v;
    77                         now[stk[idx]]=now[z]-1;
    78                         go[stk[idx]]=go[z]+1;
    79                     }
    80                 }
    81             }
    82             for(int i=1;i<=idx;i++)
    83             {
    84               int j=stk[i];
    85               ans[t]+=(heap[j].v*(now[j]-go[j])-s[j]+b[j]);
    86             }
    87         }
    88         printf("%I64d
    ",min(ans[0],ans[1]));
    89     }
    90     return 0;
    91 }
  • 相关阅读:
    Spring+Mybatis配置
    Python 字符串
    图像处理知识相关
    CUDA编程学习相关
    Pthon学习相关
    【论文阅读】ShuffleNet: An Extremely Efficient Convolutional Neural Network for Mobile Devices
    【pytorch】pytorch学习笔记(一)
    【论文阅读】MEAL: Multi-Model Ensemble via Adversarial Learning
    【论文阅读】HydraPlus-Net: Attentive Deep Features for Pedestrian Analysis
    【论文阅读】Learning Spatial Regularization with Image-level Supervisions for Multi-label Image Classification
  • 原文地址:https://www.cnblogs.com/CKboss/p/3403615.html
Copyright © 2020-2023  润新知