Submission #1130678

#TimeUsernameProblemLanguageResultExecution timeMemory
1130678CrabCNHCandies (JOI18_candies)C++20
100 / 100
97 ms31816 KiB
#include <bits/stdc++.h>

#define task   "BriantheCrab"

#define int    long long
#define pii    pair <int, int>
#define fi     first
#define se     second
#define szf    sizeof
#define sz(s)  (int)((s).size())

using namespace std;

template <class T> void mini (T &t, T f) {if (t > f) t = f;}
template <class T> void maxi (T &t, T f) {if (t < f) t = f;}

const int maxN = 2e5 + 5;
const int inf = 1e18 + 7;
const int mod = 1e9 + 7;

int n;
int a[maxN];

struct range {
    int l, r, val;
};

struct cmp {
    bool operator () (range A, range B) {
        return A.val < B.val;
    }
};

namespace sub1 {
    int dp[2005][2005]; // max val at the i-th place and choose j 
    
    void sol () {
        for (int i = 0; i <= n; i ++) {
            for (int j = 1; j <= n; j ++) {
                if (i == 0) {
                    dp[i][j] = -inf;
                }
                else if (i == 1) {
                    dp[i][j] = ((j == 1) ? a[i] : -inf);
                }
                else {
                    dp[i][j] = max (dp[i - 1][j], dp[i - 2][j - 1] + a[i]);
                }
            }
        }    
        for (int i = 1; i <= (n + 1) / 2; i ++) {
            cout << dp[n][i] << '\n';
        }
    }
}

namespace full {
    int last[maxN], nxt[maxN];
    bool visited[maxN];
    
    void sol () {
        priority_queue <range, vector <range>, cmp> pq;
        for (int i = 1; i <= n; i ++) {
            pq.push ({i, i + 1, a[i]});
        }
        for (int i = 1; i <= n + 1; i ++) {
            last[i] = i - 1;
            nxt[i] = i + 1;
        }
        int take = 0;
        int res = 0;
        while (!pq.empty ()) {
            auto [l, r, val] = pq.top ();
            pq.pop ();
            if (visited[l] || visited[r]) {
                continue;
            }
            res += val;
            visited[l] = visited[r] = true;
            take ++;
            cout << res << '\n';
            if (take == (n + 1) / 2) {
                return;
            }
            int u = last[l];
            int v = nxt[r];
            if (1 <= u && v <= n + 1) {
                nxt[u] = v;
                last[v] = u;
                a[u] += a[r] - a[l];
                pq.push ({u, v, a[u]});
            }
        }
        return;
    }  
};

void Solve () {    
    cin >> n;
    for (int i = 1; i <= n; i ++) {
        cin >> a[i];
    }
    if (n <= 2000) {
        sub1 :: sol ();
        return;
    }
    full :: sol ();
}

signed main () {
    cin.tie (nullptr) -> sync_with_stdio (false);
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    int t = 1;
    //cin >> t;
    while (t --) {
        Solve ();
    } 
    return 0;
}
// Belligerent :)

Compilation message (stderr)

candies.cpp: In function 'int main()':
candies.cpp:113:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
candies.cpp:114:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  114 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...