Educational Codeforces Round (rated for Div. 2) D. degree Set

Source: Internet
Author: User
Tags set time time limit

D. degree Set time limit per test 2 seconds memory limit per test megabytes input standard input output standard OUTPU T

You are given a sequence of n positive integers d1, d2, ..., dn (D1 < D2 < ... < DN). Your task is to construct a undirected graph such that:there is exactly DN + 1 vertices; There is no self-loops; There is no multiple edges; There is no more than 106 edges; Its degree set are equal to D.

Vertices should be numbered 1 through (DN + 1).

Degree sequence is an array a with length equal to the number of vertices in a graph such this AI is the number of Vertice s adjacent to i-th Vertex.

Degree set is a sorted in increasing order sequence of all distinct values from the degree sequence.

It is guaranteed this there exists such a graph that all the conditions hold, and it contains no more than 106 edges.

Print the resulting graph. Input

The first line contains one integer n (1≤n≤300)-the size of the degree set.

The second line contains n integers d1, d2, ..., dn (1≤di≤1000, D1 < D2 < ... < DN)-the degree set. Output

In the first line print one integer m (1≤m≤106)-the number of edges in the resulting graph. It is guaranteed this there exists such a graph that all the conditions hold and it contains no more than 106 edges.

Each of the next m lines should contain the integers vi and UI (1≤VI, UI≤DN + 1)-the description of the i-th edge. Examples input Copy

3
2 3 4
Output Copy
8
3 1
4 2
4 5
2 5
5 1
3 2
2 1
5 3
Input Copy
3
1 2 3
Output Copy
4
1 2
1 3
1 4
2 3

Test instructions: Constructs a diagram of a d[n]+1 node that requires degrees to be included in {D[1]...d[n]} and appears at least once.

Principle:


Prove:


#include <bits/stdc++.h> #include <ext/hash_map> #include <ext/hash_set> #include <ext/pb_ds/
assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/priority_queue.hpp>
using namespace Std;
using namespace __gnu_cxx;
using namespace __gnu_pbds; #define Xinf Int_max #define INF 0x3f3f3f3f #define MP (x, y) make_pair #define PB (x) push_back (×) #define REP (X,n) for (int x=0; x<n; x + +) #define REP2 (X,L,R) for (int x=l; x<=r; x + +) #define DEP (x,r,l) for (int x=r; x>=l;
x--) #define CLR (a,x) memset (a,x,sizeof (A)) #define IT iterator #define RIT reverse_iterator typedef long Long LL;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<pii> VII;
typedef vector<int> VI;
typedef TREE&LT;PII, Null_type, Greater<pii>, Rb_tree_tag, Tree_order_statistics_node_update > Rb_tree_set;
typedef TREE&LT;PII, PII, Greater<pii>, Rb_tree_tag, Tree_order_statistics_node_update > Rb_tree; #defiNE PQ std::p riority_queue #define HEAP __gnu_pbds::p riority_queue #define X First #define Y second #define LSON (x) ((x) &lt
; <1) #define Rson (x) ((x) <<1|1) #define EPS 10e-6 #define PI ACOs ( -1) #define N + int d[n], in[n];
VII G;
int n;
int S, t;
        void Solve (int n, int d[], int s) {REP (I, d[0]) {for (int j = s; j < T; ++j) G.PB (MP (J, T));
    t--;
        } if (n > 2) {REP2 (i, 1, n-1) {d[i] = d[i]-d[0];
    } Solve (N-2, d+1, t-d[n-2]);
    }} int main () {scanf ("%d", &n);
    REP (i, N) scanf ("%d", &d[i]);
    s = 1, t = d[n-1]+1;
    Solve (n, D, s);
    printf ("%d\n", G.size ());
    for (auto p:g) {printf ("%d%d\n", p.x, P.Y);
} return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.