Submission #1101847

#TimeUsernameProblemLanguageResultExecution timeMemory
1101847underwaterkillerwhaleDynamic Diameter (CEOI19_diameter)C++17
0 / 100
2 ms336 KiB
//#include"holiday.h"
#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()

using namespace std;

mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }

const int N = 3e5 + 7;
int Mod = 998244353;///lon
const ll INF = 1e18;
const ll BASE = 137;
const int szBL = 450;

inline void minimize (int &A, int B) {
    if (A > B) A = B;
}

int n, K;
int a[N], b[N], c[N];

pii spt[N][25];
int lg[N];

namespace sub3 {
    const int N3 = 400 + 7;
    int dp[2][N3 * N3], pre[N3 * N3], suf[N3 * N3];

    void solution () {
        int bound = min((K + 1) * n, 400 * 400);
        rep (j, 0, bound) dp[0][j] = j;
        rep (i, 0, n - 1) {
            rep (j, 0, bound) {
                dp[i & 1 ^ 1][j] = 2e9;
            }
            pre[0] = dp[i & 1][0], suf[bound + 1] = 2e9;
            rep (j, 1, bound) pre[j] = min(pre[j - 1], dp[i & 1][j] - j);
            reb (j, bound, 0) suf[j] = min(suf[j + 1], dp[i & 1][j]);
            rep (j, 0, bound) {
                if ((a[i + 1] + j) % (K + 1) != b[i + 1]) continue;
                dp[i & 1 ^ 1][j] = min(pre[j] + j, suf[j + 1]);
//                cout << i + 1 <<" "<<j<<" "<<dp[i & 1 ^ 1][j] <<"\n";
            }
        }
        int res = 2e9;
        rep (j, 0, K) res = min(res, dp[n & 1][j]);
        cout << res <<"\n";
    }
}

namespace sub2 {
    void solution() {
        int res = 0;
        rep (i, 1, n) {
            if (c[i] != c[i - 1] && c[i] == 1) ++res;
        }
        cout << res <<"\n";
    }
}

void solution () {
    cin >> n >> K;
    rep (i, 1, n) {
        cin >> a[i];
    }
    rep (i, 1, n) {
        cin >> b[i];
    }
    rep (i, 1, n) {
        c[i] = ((b[i] - a[i]) % (K + 1) +  K + 1) % (K + 1);
    }
    if (K <= 1)
        sub2 :: solution();
    else if (n <= 400)
        sub3 :: solution();
    else {
        rep (i, 1, n) {
            spt[i][0] = MP(c[i], i);
            lg[i] = log2(i);
        }
        for (int j = 1; (1 << j) <= n; ++j)
            for (int i = 1; i + (1 << j) - 1 <= n; ++i)  {
                spt[i][j] = min(spt[i][j - 1], spt[i + (1 << j - 1)][j - 1]);
            }
        auto get_Min = [] (int L, int R) {
            int K = lg[R - L + 1];
            return min(spt[L][K], spt[R - (1 << K) + 1][K]);
        };
        ll res = 0;
        function<void(int, int, int)> dnc = [&] (int L, int R, int Delta) {
            if (L > R) return 0;
            int Mn, pos;
            tie(Mn, pos) = get_Min(L, R);
            res += Mn - Delta;
            dnc(L, pos - 1, Mn);
            dnc(pos + 1, R, Mn);
        };
        dnc(1, n, 0);
        cout << res <<"\n";
    }
}

#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
    file("CODE");
    ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int num_Test = 1;
//    cin >> num_Test;
    while (num_Test--)
        solution();
}
/*
no bug challenge +10

5 5
01101
10001
00110
10101
00100
*/

Compilation message (stderr)

diameter.cpp: In function 'void sub3::solution()':
diameter.cpp:47:22: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   47 |                 dp[i & 1 ^ 1][j] = 2e9;
      |                    ~~^~~
diameter.cpp:54:22: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   54 |                 dp[i & 1 ^ 1][j] = min(pre[j] + j, suf[j + 1]);
      |                    ~~^~~
diameter.cpp: In function 'void solution()':
diameter.cpp:96:64: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   96 |                 spt[i][j] = min(spt[i][j - 1], spt[i + (1 << j - 1)][j - 1]);
      |                                                              ~~^~~
diameter.cpp: In lambda function:
diameter.cpp:110:9: warning: control reaches end of non-void function [-Wreturn-type]
  110 |         };
      |         ^
diameter.cpp: In function 'int main()':
diameter.cpp:116:27: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 | #define file(name) freopen(name".inp","r",stdin); \
      |                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
diameter.cpp:119:5: note: in expansion of macro 'file'
  119 |     file("CODE");
      |     ^~~~
diameter.cpp:117:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 | freopen(name".out","w",stdout);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
diameter.cpp:119:5: note: in expansion of macro 'file'
  119 |     file("CODE");
      |     ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...