Bzoj 1592 Usaco Making the Grade pavement trimming DP

Source: Internet
Author: User

The main idea: to give an untidy road surface, you can increase or decrease a road surface, all need the cost of |x-x ' |, the road to the correction to a monotonous or monotonous non-rising minimum cost is how much.


Idea: The height span of the road is a little big ah, first discretization. After F[i][j], the road surface is guaranteed to be monotonically non-descending and the maximum height for the minimum cost of J is how much, using a prefix and optimization. Monotonous does not rise also the same, simple DP water over.


CODE:

#include <map> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm > #define MAX 2010#define INF 0x3f3f3f3fusing namespace std; int Cnt,src[max];p air<int,int *> xx[max];map<int,int> G;    int f[max][max],_f[max][max];//monotone not descending int g[max][max],_g[max][max];//monotone not ascending int main () {CIN >> cnt;        for (int i = 1; I <= cnt; ++i) {scanf ("%d", &xx[i].first);    Xx[i].second = &src[i];    } sort (xx + 1,xx + cnt + 1);    int t = 0;        for (int i = 1; I <= cnt; ++i) {if (i = = 1 | | Xx[i].first! = xx[i-1].first) ++t;        *xx[i].second = t;    G[t] = Xx[i].first;    } memset (F,0x3f,sizeof (f));    memset (G,0x3f,sizeof (g));    for (int i = 1; I <= t; ++i) f[1][i] = G[1][i] = ABS (G[src[1]]-g[i]);    for (int i = 1; I <= cnt; ++i) _f[i][0] = _g[i][t + 1] = INF;    for (int i = 1; I <= t; ++i) _f[1][i] = min (_f[1][i-1],f[1][i]);       for (int i = t; i; i.) _g[1][i] = min (_g[1][i + 1],g[1][i]); for (int i = 2; I <= cnt, ++i) {for (int j = 1; j <= cnt; ++j) {f[i][j] = _f[i-1][j] + ABS (G[J]            -G[src[i]]);        G[I][J] = _g[i-1][j] + ABS (G[J]-g[src[i]);        } for (int j = 1; j <= T; ++j) _f[i][j] = min (_f[i][j-1],f[i][j]);    for (int j = t; j;--j) _g[i][j] = min (_g[i][j + 1],g[i][j]);    } int ans = INF;    for (int i = 1; I <= t; ++i) ans = min (ans,min (f[cnt][i],g[cnt][i]));    cout << ans << endl; return 0;}


Bzoj 1592 Usaco Making the Grade pavement trimming DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.