Submission #647305

#TimeUsernameProblemLanguageResultExecution timeMemory
647305ghostwriterCollecting Stamps 3 (JOI20_ho_t3)C++14
100 / 100
356 ms129356 KiB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
    Tran The Bao - ghostwriter
    Training for VOI23 gold medal
----------------------------------------------------------------
    DIT ME CHUYEN BAO LOC
----------------------------------------------------------------
*/
const int oo = 1e9 + 5;
const int N = 202;
int n, l, x[N], t[N], rs = 0;
pi d[N][N][2][N];
#define dis(x, y) min((x - y + l) % l, (y - x + l) % l)
void minimize(pi &a, pi b) {
	a.nd = min(a.nd, oo);
	b.nd = min(b.nd, oo);
	if (!a.st && !b.st) {
		if (b.nd < a.nd) a = b;
		return;
	}
	if (a.st && !b.st) return;
	if (!a.st && b.st) {
		a = b;
		return;
	}
	if (a.nd > b.nd) a = b;
}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
    cin >> n >> l;
    FOR(i, 1, n) cin >> x[i];
    FOR(i, 1, n) cin >> t[i];
    FOR(i, 0, n + 1)
    FOR(j, 0, n + 1)
    FOR(stat, 0, 1)
    FOR(z, 0, n)
    	d[i][j][stat][z] = {0, oo};
    FOR(i, 0, n + 1)
    FOS(j, n + 1, i + 1)
    FOR(stat, 0, 1) 
    FOR(z, 0, n) {
    	pi &rs = d[i][j][stat][z];
    	if (i == 0 && j == n + 1) {
    		minimize(rs, !z? pi(1, 0) : pi(0, oo));
    		continue;
    	}
    	if (!stat) {
    		if (i) {
    			pi tmp = d[i - 1][j][0][z];
    			minimize(rs, {tmp.st, tmp.nd + dis(x[i], x[i - 1])});
    			tmp = d[i - 1][j][1][z];
    			minimize(rs, {tmp.st, tmp.nd + dis(x[i], x[j])});
    			if (z) {
	    			pi tmp = d[i - 1][j][0][z - 1];
	    			if (tmp.st && tmp.nd + dis(x[i], x[i - 1]) <= t[i]) {
	    				tmp.nd += dis(x[i], x[i - 1]);
	    				minimize(rs, tmp);
	    			}
	    			tmp = d[i - 1][j][1][z - 1];
	    			if (tmp.st && tmp.nd + dis(x[i], x[j]) <= t[i]) {
	    				tmp.nd += dis(x[i], x[j]);
	    				minimize(rs, tmp);
	    			}
    			}
    		}
    	}
    	else {
    		if (j <= n) {
    			pi tmp = d[i][j + 1][1][z];
    			minimize(rs, {tmp.st, tmp.nd + dis(x[j], x[j + 1])});
    			tmp = d[i][j + 1][0][z];
    			minimize(rs, {tmp.st, tmp.nd + dis(x[j], x[i])});
    			if (z) {
	    			pi tmp = d[i][j + 1][1][z - 1];
	    			if (tmp.st && tmp.nd + dis(x[j], x[j + 1]) <= t[j]) {
	    				tmp.nd += dis(x[j], x[j + 1]);
	    				minimize(rs, tmp);
	    			}
	    			tmp = d[i][j + 1][0][z - 1];
	    			if (tmp.st && tmp.nd + dis(x[j], x[i]) <= t[j]) {
	    				tmp.nd += dis(x[j], x[i]);
	    				minimize(rs, tmp);
	    			}
    			}
    		}
    	}
    }
    FOR(i, 0, n)
    FOS(j, n + 1, i + 1)
    FOR(stat, 0, 1)
    FOR(z, 0, n)
    	if (d[i][j][stat][z].st)
    		rs = max(rs, z);
    cout << rs;
    return 0;
}
/*
----------------------------------------------------------------
From Benq:
    stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
        * DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/

Compilation message (stderr)

ho_t3.cpp: In function 'int main()':
ho_t3.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
ho_t3.cpp:75:5: note: in expansion of macro 'FOR'
   75 |     FOR(i, 1, n) cin >> x[i];
      |     ^~~
ho_t3.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
ho_t3.cpp:76:5: note: in expansion of macro 'FOR'
   76 |     FOR(i, 1, n) cin >> t[i];
      |     ^~~
ho_t3.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
ho_t3.cpp:77:5: note: in expansion of macro 'FOR'
   77 |     FOR(i, 0, n + 1)
      |     ^~~
ho_t3.cpp:32:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
ho_t3.cpp:78:5: note: in expansion of macro 'FOR'
   78 |     FOR(j, 0, n + 1)
      |     ^~~
ho_t3.cpp:32:31: warning: unnecessary parentheses in declaration of 'stat' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
ho_t3.cpp:79:5: note: in expansion of macro 'FOR'
   79 |     FOR(stat, 0, 1)
      |     ^~~
ho_t3.cpp:32:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
ho_t3.cpp:80:5: note: in expansion of macro 'FOR'
   80 |     FOR(z, 0, n)
      |     ^~~
ho_t3.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
ho_t3.cpp:82:5: note: in expansion of macro 'FOR'
   82 |     FOR(i, 0, n + 1)
      |     ^~~
ho_t3.cpp:33:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   33 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
      |                               ^
ho_t3.cpp:83:5: note: in expansion of macro 'FOS'
   83 |     FOS(j, n + 1, i + 1)
      |     ^~~
ho_t3.cpp:32:31: warning: unnecessary parentheses in declaration of 'stat' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
ho_t3.cpp:84:5: note: in expansion of macro 'FOR'
   84 |     FOR(stat, 0, 1)
      |     ^~~
ho_t3.cpp:32:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
ho_t3.cpp:85:5: note: in expansion of macro 'FOR'
   85 |     FOR(z, 0, n) {
      |     ^~~
ho_t3.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
ho_t3.cpp:132:5: note: in expansion of macro 'FOR'
  132 |     FOR(i, 0, n)
      |     ^~~
ho_t3.cpp:33:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   33 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
      |                               ^
ho_t3.cpp:133:5: note: in expansion of macro 'FOS'
  133 |     FOS(j, n + 1, i + 1)
      |     ^~~
ho_t3.cpp:32:31: warning: unnecessary parentheses in declaration of 'stat' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
ho_t3.cpp:134:5: note: in expansion of macro 'FOR'
  134 |     FOR(stat, 0, 1)
      |     ^~~
ho_t3.cpp:32:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
ho_t3.cpp:135:5: note: in expansion of macro 'FOR'
  135 |     FOR(z, 0, n)
      |     ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...