DMOJ · ccc22s3

CCC 2022 S3 Good Samples

This C++ solution uses simulation for CCC 2022 S3 Good Samples. Read the reasoning, inspect the code, or try your own test case below.

ccc22s3CCC 2022 S3Arrays & prefix sumsSimulationC++75 lines
Solution101of 248
Open official problem ↗ Download C++ file ↓ Search the library → Open full Code Lab ↗ Report an issue ↗

Approach

Simulation

Good Samples construction uses N, M, and K with the same good-subarray target implemented here.

Arrays & prefix sums

Problem and code

Useful links.

Written by benbenyaojifen. Try the problem first, then compare your approach with the code.

Open official problem ↗View exact source file ↗
Implementation

S_3_Good_Samples.cpp

C++

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const long long INF = 1e17; //❄️
    int main() {
        ios::sync_with_stdio(0); cin.tie(0); 
        int n, m;              
        ll k;
        cin >> n >> m >> k;
        m = min(n, m);
        vector<int> ans(n);
        int cur = 1;
        int index = 0;
        bool all = true;
        //first m distinct pitch
        for(int i = 0; i < m; i++){
            // this checks if we can still place it or not since we have to remain the slots for placing one -> each slot must place at least one
            if(n - (i + 1) + i + 1 > k){
                all = false;
                break;
            }
            ans[i] = i + 1;
            index++;
            k -= i + 1;
        } 
        // cout << k << endl;
     
        //cannot place anything 
        if(index == 0){
            cout << -1 << '\n';
            return 0;
        } 
        //reset to form cycle 
        while(index < n && k > 0){
            // check if we can place the current pitch without going over -(n - index - 1) to account for the ones that we must place at the end
            if(k - m - (n - index - 1) >= 0 && all){
                ans[index] = cur;
                index++;
                cur++;
                if(cur > m) cur = 1;// the ensure it is distinct we do 1 to m repeatedly
                //every time we place a distinct pitch we add m good samples
                k -= m;
                // cout << "run" << '\n';
            } else {
                // we cannot place distinct pitch anymore
                int remain = n - index - 1; // how many ones that we must place at the end 
                int need = k - remain;
                int place = ans[index - need];
                ans[index] = place;
                k -= need;
                index++;
                // cout << remain << " " << need << " " << place << endl;
                break;
            }
            // cout << k << endl;
        }
        // for(int i = 0; i < ans.size(); i++){
        //     cout << ans[i] << (i == ans.size() - 1 ? "\n" : " ");
        // }
         //place the ones 
        while(index < n){
            ans[index] = ans[index - 1];
            index++;
            k--;
        }
        // cout << k << endl;
        if(k != 0){
            cout << -1 << '\n';
            return 0;
        }
        for(int i = 0; i < ans.size(); i++){
            cout << ans[i] << " \n"[i == ans.size() - 1];
        }
        return 0;
    }
        

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗Keep studying →

Test this problem

Run your code here.

Paste your code, run a test case, compare the output, or trace selected values.

Full trace, comparison & stress testing ↗
StatusReady
Output
No run yet.
Diagnostics
No diagnostics yet.

Each run is isolated and has strict limits. Passing one test does not guarantee the judge will accept the solution.