Submission #1242485

#TimeUsernameProblemLanguageResultExecution timeMemory
1242485chienthanminh23Cloud Computing (CEOI18_clo)C++17
100 / 100
988 ms2328 KiB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MAX_CORES = 100001;
typedef long long ll;

ll dp_curr[MAX_CORES];  // dp[i] = max profit with i cores
ll dp_next[MAX_CORES];

bool vis_curr[MAX_CORES];  // vis[i] = is i cores achievable
bool vis_next[MAX_CORES];

int n, m;

struct Entry {
    bool isComputer;
    int cores;
    int freq;
    int value;

    Entry(bool isComp, int c, int f, int v) {
        isComputer = isComp;
        cores = c;
        freq = f;
        value = v;
    }
};

vector<Entry> entries;

bool cmp(const Entry &a, const Entry &b) {
    // Sort by frequency descending
    // If same freq, prioritize computers first (so cores are available early)
    if (a.freq != b.freq) return a.freq > b.freq;
    return a.isComputer > b.isComputer;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    for (int i = 0; i < n; ++i) {
        int c, f, v;
        cin >> c >> f >> v;
        entries.emplace_back(true, c, f, -v);  // Computer: cost = -v
    }

    cin >> m;
    for (int i = 0; i < m; ++i) {
        int c, f, v;
        cin >> c >> f >> v;
        entries.emplace_back(false, -c, f, v); // Order: gives profit, consumes cores
    }

    // Sort entries by frequency descending
    sort(entries.begin(), entries.end(), cmp);

    // Initialize
    vis_curr[0] = true;
    dp_curr[0] = 0;

    // Process each computer/order
    for (auto &e : entries) {
        for (int i = 0; i < MAX_CORES; ++i) {
            dp_next[i] = dp_curr[i];
            vis_next[i] = vis_curr[i];
        }

        for (int i = 0; i < MAX_CORES; ++i) {
            if (!vis_curr[i]) continue;

            int ni = i + e.cores;  // cores after applying this entry
            if (ni < 0 || ni >= MAX_CORES) continue;

            ll newProfit = dp_curr[i] + e.value;

            if (!vis_next[ni] || dp_next[ni] < newProfit) {
                dp_next[ni] = newProfit;
                vis_next[ni] = true;
            }
        }

        // Swap for next iteration
        for (int i = 0; i < MAX_CORES; ++i) {
            dp_curr[i] = dp_next[i];
            vis_curr[i] = vis_next[i];
        }
    }

    // Get max profit
    ll maxProfit = 0;
    for (int i = 0; i < MAX_CORES; ++i) {
        if (vis_curr[i]) {
            maxProfit = max(maxProfit, dp_curr[i]);
        }
    }

    cout << maxProfit << '\n';
    return 0;
}
#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...