제출 #1059031

#제출 시각아이디문제언어결과실행 시간메모리
1059031underwaterkillerwhale메기 농장 (IOI22_fish)C++17
53 / 100
303 ms142416 KiB
#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) (int)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 = 1e5 + 7;
const int Mod = 1e9 + 7;
const ll INF = 2e9 + 7;
const ll BASE = 137;
const int szBL = 320;

void inline maximize (ll &A, ll B) { if (A < B) A = B; }

struct Fish {
    int fs, se;
    ll W;
};

int n, m;
vector<ll> pcur[N], dp[N], fsuf[N], f[N], fpre[N], dppre[N], Left[N], Right[N];

vector<int> high[N];
Fish a[(int)3e5 + 7];
vector<pll> vec[N];

ll premx[N], sufmx[N];

void init (int j, int m) {
    pcur[j].resize(m + 2, 0);
    Left[j].resize(m + 2, 0);
    Right[j].resize(m + 2, 0);
    fpre[j].resize(m + 2, 0);
    dppre[j].resize(m + 2, 0);
    f[j].resize(m + 2, 0);
    fsuf[j].resize(m + 2, 0);
    dp[j].resize(m + 2, 0); ///sửa ik
}

//void solution () {
ll max_weights(int _n, int _m, vector<int> X, vector<int> Y, vector<int> W) {
//    cin >> n >> m;
//    n = 1e5;
//    m = Rand(1, (n - 1) / 2);
//    m = min(1LL * n * n - Rand(1, n * n), (ll)3e5);
    n = _n;
    m = _m;
//    vector<ll> val;
//    rep (i, 0, (n - 1) / 2) val.pb(i * 2);
//    random_shuffle(ALL(val));
    rep (j, 0, n) high[j] = {0};
    rep (i, 1, m) {
//        cin >> a[i].fs >> a[i].se >> a[i].W;
//        a[i].fs = val[i - 1].fs;
//        a[i].se = val[i - 1].se;
//        a[i].W = Rand(1, 100000);
//            a[i].fs = val[i];
//            a[i].se = Rand(0, n - 1);
//            a[i].W = 1e9;
        a[i].fs = X[i - 1];
        a[i].se = Y[i - 1];
        a[i].W = W[i - 1];
        ++a[i].fs;
        ++a[i].se;
//        cout << a[i].fs<<","<<a[i].se<<"\n";
        vec[a[i].fs].pb(MP(a[i].se, a[i].W));
        high[a[i].fs].pb(a[i].se);
    }
    init(0, 5);
    rep (j, 1, n) {
        iter (&id, vec[j + 1])
            high[j].pb(id.fs);
        iter (&id, vec[j - 1])
            high[j].pb(id.fs);
        sort (ALL(high[j]));
        high[j].resize(unique(ALL(high[j])) - high[j].begin());
        int m = SZ(high[j]);
        init(j, m);
        iter (&id, vec[j]) pcur[j][lower_bound(ALL(high[j]), id.fs) - high[j].begin()] = id.se;
        rep (i, 1, m){
            pcur[j][i] += pcur[j][i - 1];
        }
        rep (i, 1, m - 1) {
            int pos = lower_bound(ALL(high[j - 1]), high[j][i]) - high[j - 1].begin();
            Left[j][i] = pcur[j - 1][pos];
//            if (j == 2) cout << i<<" "<<<pcur[j][i] <<".\n";
        }
    }
    rep (j, 1, n - 1) {
        int m = SZ(high[j]);
        rep (i, 1, m - 1) {
            int pos = lower_bound(ALL(high[j + 1]), high[j][i]) - high[j + 1].begin();
            Right[j][i] = pcur[j + 1][pos];
        }
    }
//    return;
    rep (j, 1, n) {
        int m = SZ(high[j]);
        vector<int> &ch = high[j];
        if (j >= 2) {
            ll prem = *max_element(ALL(dp[j - 2]));
            rep (i, 0, m - 1) {
                int pos = upper_bound(ALL(high[j - 1]), ch[i]) - high[j - 1].begin();
//                if (j == 3 && high[j][i] == 4) {
//                    cout << dp[j][i] <<" "<<Left[j][i]<<"hihih\n";
//                }
                maximize(f[j][i], fpre[j - 1][pos - 1] + Left[j][i]);
                maximize(dp[j][i], prem + Left[j][i]);
                maximize(dp[j][i], fsuf[j - 1][pos] - pcur[j][i]);
                maximize(dp[j][i], f[j][i]);
            }
            rep (i, 0, m - 1) {
                int pos = upper_bound(ALL(high[j - 2]), ch[i]) - high[j - 2].begin();
//                if (j == 3 && high[j][i] == 4) {
//                    cout << dp[j][i] <<" "<<Left[j][i]<<"hihih\n";
//                }
                maximize(f[j][i], dppre[j - 2][pos - 1] + Left[j][i]);
                maximize(f[j][i], fsuf[j - 2][pos]);
                maximize(dp[j][i], f[j][i]);
            }
        }
        if (j > 3) {
            ll prem = 0;
            rep (i, 0, SZ(high[j - 3]) - 1) {
                prem = max(prem, dp[j - 3][i] + Right[j - 3][i]);
            }
            rep (i, 0, m - 1) {
                maximize(dp[j][i], Left[j][i] + prem);
                maximize(f[j][i], Left[j][i] + prem);
            }
        }
        fpre[j][0] = f[j][0];
        dppre[j][0] = dp[j][0];
        rep (i, 1, m - 1) {
            fpre[j][i] = max(fpre[j][i - 1], f[j][i] - pcur[j][i]);
            dppre[j][i] = max(dppre[j][i - 1], dp[j][i]);
        }
        reb (i, m - 1, 0) {
            fsuf[j][i] = max(fsuf[j][i + 1], dp[j][i] + Right[j][i]);
        }
        rep (i, 0, m - 1) {
//            cout << j <<  " "<<high[j][i] <<" "<< dp[j][i] <<"\n";
        }
    }
//    cout << *max_element(ALL(dp[n])) <<"\n";
    return *max_element(ALL(dp[n]));
}
//
//#define file(name) freopen(name".inp","r",stdin); \
//freopen(name".out","w",stdout);
//int main () {
////    file("c");
////    ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
//    int num_Test = 1;
////    cin >> num_Test;
//    while (num_Test--)
////        solution();
//        cout << max_weights(5, 4, {0, 1, 4, 3}, {2, 1, 4, 3}, {5, 2, 1, 3});
//}
/*
no bug challenge +2
2 + 8 * 2 - 9
7 7
0 2 5
0 1 1
2 1 2
2 2 1
4 4 1
4 6 2
6 3 3

*/

컴파일 시 표준 에러 (stderr) 메시지

fish.cpp:163:1: warning: multi-line comment [-Wcomment]
  163 | //#define file(name) freopen(name".inp","r",stdin); \
      | ^
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...