976D - Degree Set - CodeForces Solution


constructive algorithms graphs implementation *2500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;

// Extra
#define all(x) x.begin(), x.end()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//

const int MAX = 300010;
const ll MOD = (int)1e9 +7;
const int INF = 1e9;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-8;

void solve(vector<int> d, vector<pair<int,int>>& e) {
    if(d.size() == 1) {
        // assert(d[0] == 1);
        for(int i = 1; i <= d[0]+1; i++) {
            for(int j = i+1; j <= d[0]+1; j++) {
                e.pb({j, i});
            }
        }
        return;
    }
    if(d.size() == 0) {
        return;
    }

    int n = d.size();
    vector<int> d2(n-2);
    for(int i = 1; i < n-1; i++) {
        d2[i-1] = d[i] - d[0];
    }

    solve(d2, e);

    for(int i = d[n-1]+1; i > d[n-1]-d[0]+1; i--) {
        for(int j = 1; j <= d[n-1]+1; j++) {
            if(i <= j) continue;
            e.pb({i, j});
        }
    }
}

int main() {
    sws;
    int n;
    cin >> n;

    vector<int> d(n);
    for(int i = 0; i < n; i++) {
        cin >> d[i];
    }

    vector<pair<int,int>> e;
    solve(d, e);

    cout << e.size() << endl;
    for(auto [a, b] : e) {
        cout << a << " " << b << endl;
    }
    

    return 0;
}


Comments

Submit
0 Comments
More Questions

1515C - Phoenix and Towers
998A - Balloons
1734F - Zeros and Ones
1144B - Parity Alternated Deletions
92B - Binary Number
1144C - Two Shuffled Sequences
1154B - Make Them Equal
1272B - Snow Walking Robot
522B - Photo to Remember
608B - Hamming Distance Sum
1408F - Two Different
274B - Zero Tree
1726H - Mainak and the Bleeding Polygon
722A - Broken Clock
129B - Students and Shoelaces
697B - Barnicle
903D - Almost Difference
1443B - Saving the City
1215C - Swap Letters
1251C - Minimize The Integer
1494B - Berland Crossword
295A - Greg and Array
1433E - Two Round Dances
1612D - X-Magic Pair
41B - Martian Dollar
906C - Party
774F - Pens And Days Of Week
598B - Queries on a String
1303B - National Project
1303D - Fill The Bag