Codeforces 1076F Summer Practice Report dp

Summer Practice Report

dp[ i ][ 0 ]表示放完前 i 页, 第 i 页最后一段是 0, 0个数的最小值。

dp[ i ][ 1 ]表示放完前 i 页, 第 i 页最后一段是 1, 1个数的最小值。

这个转移细节有点多。。。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long

using namespace std;

const int N = 3e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T> bool chkmax(T& a, T b) {
    return a < b ? a = b, true : false;
}
template<class T> bool chkmin(T& a, T b) {
    return a > b ? a = b, true : false;
}

int n, x[N], y[N];
LL k;
int dp[N][2];


int calc(int prea, int a, int b, int op) {
    if(!op) {
        if(k - prea + b * k < a) return inf;
        if(a * k < b) return inf;
        if(k - prea + (b - 1) * k < a - 1) return a - (k - prea + (b - 1) * k);
        return 1;
    } else {
        if(k - prea + (b - 1) * k < a) return inf;
        if((a + 1) * k < b) return inf;
        if(a * k < b - 1) return b - a * k;
        return 1;
    }
}

int main() {
    scanf("%d%lld", &n, &k);
    for(int i = 1; i <= n; i++) scanf("%d", &x[i]);
    for(int i = 1; i <= n; i++) scanf("%d", &y[i]);
    memset(dp, inf, sizeof(dp));
    dp[0][0] = dp[0][1] = 0;
    for(int i = 0; i < n; i++) {
        if(dp[i][0] <= k) {
            chkmin(dp[i + 1][0], calc(dp[i][0], x[i + 1], y[i + 1], 0));
            chkmin(dp[i + 1][1], calc(dp[i][0], x[i + 1], y[i + 1], 1));
        }
        if(dp[i][1] <= k) {
            chkmin(dp[i + 1][1], calc(dp[i][1], y[i + 1], x[i + 1], 0));
            chkmin(dp[i + 1][0], calc(dp[i][1], y[i + 1], x[i + 1], 1));
        }
    }
    if(dp[n][0] > k && dp[n][1] > k) {
        puts("NO");
    } else puts("YES");
    return 0;
}

/*
*/

 

上一篇:PAT (Basic Level) Practice (中文)1006 换个格式输出整数 (15 分)


下一篇:UCF Local Programming Contest 2013(Practice)赛后总结