码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces1221D. Make The Fence Great Again(dp)

时间:2019-10-04 22:37:21      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:ons   fine   mask   col   har   style   链接   题目   git   

题目链接:传送门

思路:

仔细想一下可以发现:每个位置最多就增加2个高度。

所以就可以有状态:

f[i][j]表示保证前i个篱笆都是great时,第i个篱笆增加j的高度所需要的最小花费(1 <= i <= n, 0 <= j <= 2)。总共有3n个状态。

如果i = 1,f[i][j] = a[1] * j;

如果i > 1, f[i][j] = min{f[i-1][k] | 0 <= k <= 2 && a[i]+j != a[i-1]+k};这里的“a[i]+j != a[i-1]+k”保证了篱笆都是great的。

答案ans = min{f[n][j] | 0 <= j <= 2}

时间复杂度是O(n)的。


 

代码:O(n)

技术图片
#include <bits/stdc++.h>
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define N 300005
#define M 100005
#define INF 0x3f3f3f3f
#define mk(x) (1<<x) // be conscious if mask x exceeds int
#define sz(x) ((int)x.size())
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mp(a,b) make_pair(a, b)
#define endl ‘\n‘
#define lowbit(x) (x&-x)

using namespace std;
typedef long long ll;
typedef double db;

/** fast read **/
template <typename T>
inline void read(T &x) {
    x = 0; T fg = 1; char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == -) fg = -1;
        ch = getchar();
    }
    while (isdigit(ch)) x = x*10+ch-0, ch = getchar();
    x = fg * x;
}
template <typename T, typename... Args>
inline void read(T &x, Args &... args) { read(x), read(args...); }

ll a[N], b[N];
ll f[N][3];

int main()
{
    int q;
    cin >> q;
    while (q--) {
        int n; cin >> n;
        for (int i = 1; i <= n; i++) {
            read(a[i], b[i]);
            memset(f[i], 0x3f, sizeof(f[i]));
        }
        memset(f[0], 0, sizeof(f[0]));
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < 3; j++) {
                for (int k = 0; k < 3; k++) if (a[i-1]+j != a[i]+k) {
                    f[i][k] = min(f[i][k], f[i-1][j] + k*b[i]);
                }
            }
        }
        ll ans = 2e18;
        for (int i = 0; i < 3; i++) {
            ans = min(ans, f[n][i]);
        }
        cout << ans << endl;
    }
    return 0;
}
View Code

 

Codeforces1221D. Make The Fence Great Again(dp)

标签:ons   fine   mask   col   har   style   链接   题目   git   

原文地址:https://www.cnblogs.com/Lubixiaosi-Zhaocao/p/11623449.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!