Submission #1167855

#TimeUsernameProblemLanguageResultExecution timeMemory
1167855vibeduckSolar Storm (NOI20_solarstorm)C++20
0 / 100
2092 ms55996 KiB
#include <bits/stdc++.h> 
using namespace std;

#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define int long long
#define all(x) (x).begin(), (x).end()

typedef long double ld;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef map<int, int> mii;
typedef unordered_map<int, int> umii;

const int mod = 1e9 + 7;
const int inf = INTMAX_MAX;
const bool tc = false;

const int mxn = 1e6 + 5;
int d[mxn], p[mxn], v[mxn], vp[mxn];
pii bounds[mxn];

int sv(int l, int r) {
    return vp[r + 1] - vp[l];
}

inline void solve() {
    // input
    int n, s, k;
    cin >> n >> s >> k;
    for (int i = 0; i < n - 1; i++) cin >> d[i];
    for (int i = 1; i < n; i++) p[i] = p[i - 1] + d[i - 1];
    for (int i = 0; i < n; i++) cin >> v[i];
    for (int i = 1; i <= n; i++) vp[i] = vp[i - 1] + v[i - 1];
    
    // calculate bounds
    for (int t = 0; t < n; t++) {
        int lo = t;
        int hi = n;
        while (hi > lo + 1) {
            int mid = (lo + hi) / 2;
            if (p[mid] - p[t] > k) hi = mid;
            else lo = mid;
        }
        int r = lo;
        int l;
        if (p[t] <= k) l = 0;
        else {
            int lo = 0;
            int hi = t;
            while (hi > lo + 1) {
                int mid = (lo + hi) / 2;
                if (p[t] - p[t-mid] > k) hi = mid;
                else lo = mid;
            }
            l = t-lo;
        }
        bounds[t] = {l, r};
        //cout << t << " bounds " << l << " -> " << r << '\n';
    }

    //cout << '\n';

    pair<int, vector<int>> ans = {-1, {0, 0}};
    for (int st = 0; st < n; st++) {
        // starting seg
        pair<int, vector<int>> cur = {0, {}};
        int crp = st;

        //cout << "start " << st+1 << '\n';

        for (int _ = 0; _ < s; _++) {
            // segment j such that s[j].fi <= s[i].se + 1 and s[j].se should be maximized
            cur.se.pb(crp);

            if (crp == n - 1) {
                cur.fi += sv(bounds[crp].fi, bounds[crp].se);
                break;
            }
            if (bounds[crp + 1].fi > bounds[crp].se + 1) {
                cur.fi += sv(bounds[crp].fi, bounds[crp].se);
                break;
            }

            int lo = crp;
            int hi = n;
            while (hi > lo + 1) {
                int mid = (lo + hi) / 2;
                if (bounds[mid].fi <= bounds[crp].se + 1) lo = mid;
                else hi = mid;
            }
            // lo is max satisfying
            int nxt = lo;

            // this guy - next
            cur.fi += sv(bounds[crp].fi, bounds[crp].se);

            if (bounds[crp].se == n - 1) break;

            if (bounds[nxt].fi != bounds[crp].se + 1) cur.fi -= sv(bounds[nxt].fi, bounds[crp].se);

            crp = nxt;
        }

        //cout << "total value = " << cur.fi << '\n';
        //cout << '\n';
        ans = max(ans, cur);
    }

    cout << ans.se.size() << '\n';
    for (auto &x : ans.se) cout << x + 1 << " ";
    cout << '\n';
}

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

signed main() {
    ios::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    //setIO();

    int t = 1;
    if (tc) {
        cin >> t;
    }

    while (t--) {
        solve();
    }
}

Compilation message (stderr)

SolarStorm.cpp: In function 'void setIO(std::string)':
SolarStorm.cpp:130:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  130 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SolarStorm.cpp:131:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...