#include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; int n; const int MAXN=1000000+10; vector<int> tab; int main(){ ios_base::sync_with_stdio(0); cin>>n; set<int> tree; int suma=0; int ilekrawedzi=0; for(int i=0;i<n;i++){ int a; cin>>a; tab.push_back(a); suma+=a; } sort(tab.begin(),tab.end()); ilekrawedzi=suma; if(suma%2==1) ilekrawedzi++; ilekrawedzi/=2; int i=n-1; int maks=n-1; while(ilekrawedzi>n-1){ if(n<=2) break; int a=suma-tab[i]; int iletemp=a; if(a%2==1) iletemp++; iletemp/=2; if(iletemp>=n-2){ // cout<<"WYPIERDALAM I:"<<i<<endl; ilekrawedzi=iletemp; suma-=tab[i]; tab[i]=0; if(i==maks) maks--; } //cout<<"I:"<<i<<" N:"<<n<<" Ilekrawedzi:"<<ilekrawedzi<<" a:"<<a<<" iletemp:"<<iletemp<<" SUMA:"<<suma<<endl; i--; n--; } int x=0; if(ilekrawedzi<n-1){ int a=(((n-1)*2)-suma); tab[maks]+=(((n-1)*2)-suma); x=a; } else if(ilekrawedzi>n-1){ //cout<<"TAK"<<endl; int a=(((n-1)*2)-suma); a=abs(a); tab[maks]-=a; x=a; } vector<int> tab2; for(int i=0;i<n;i++){ if(tab[i]!=0) tab2.push_back(tab[i]); } //cout<<"SIZE:"<<tab2.size()<<endl; if(int(tab2.size())<n){ int p=tab2.size()-1; for(int i=0;i<n-int(tab2.size());i++){ if(tab2[p]-1>0){ tab2.push_back(1); tab2[p]--; } else{ p--; i--; } } } sort(tab2.begin(),tab2.end()); cout<<x<<endl; cout<<n<<endl; for(int i=n-1;i>=0;i--){ //cout<<"I:"<<i<<" TAB2[i]:"<<tab2[i]<<endl; int ile=tab2[i]; int point=i; while(ile>0){ //cout<<"IDE DLA:"<<i<<endl; point--; if(point<0) break; if(tab2[point]>0){ cout<<i+1<<" "<<point+1<<endl; ile--; } tab2[point]--; } } /*set<int>::iterator it; it=tree.end(); cout<<(*it)<<endl; while(!tree.empty()){ it=tree.end(); int ile=*it; cout<<"ILE:"<<ile<<endl; set<int>::iterator it2; it2=tree.begin(); int b=*it2; for(int i=0;i<ile;i++){ cout<<"XD"<<endl; cout<<(*it)<<" "<<(*it2)<<endl; it2++; } tree.erase(it); for(int i=0;i<ile;i++){ int c=*it2 if(b<=1) tree.erase(it2); else{ tree.erase(it2); tree.insert(b-1); } } cout<<"NEXT"<<endl; }*/ /*cout<<ilekrawedzi<<" N:"<<n<<endl; for(int i=0;i<n;i++){ cout<<tab2[i]<<" "; } cout<<endl;*/ return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | #include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; int n; const int MAXN=1000000+10; vector<int> tab; int main(){ ios_base::sync_with_stdio(0); cin>>n; set<int> tree; int suma=0; int ilekrawedzi=0; for(int i=0;i<n;i++){ int a; cin>>a; tab.push_back(a); suma+=a; } sort(tab.begin(),tab.end()); ilekrawedzi=suma; if(suma%2==1) ilekrawedzi++; ilekrawedzi/=2; int i=n-1; int maks=n-1; while(ilekrawedzi>n-1){ if(n<=2) break; int a=suma-tab[i]; int iletemp=a; if(a%2==1) iletemp++; iletemp/=2; if(iletemp>=n-2){ // cout<<"WYPIERDALAM I:"<<i<<endl; ilekrawedzi=iletemp; suma-=tab[i]; tab[i]=0; if(i==maks) maks--; } //cout<<"I:"<<i<<" N:"<<n<<" Ilekrawedzi:"<<ilekrawedzi<<" a:"<<a<<" iletemp:"<<iletemp<<" SUMA:"<<suma<<endl; i--; n--; } int x=0; if(ilekrawedzi<n-1){ int a=(((n-1)*2)-suma); tab[maks]+=(((n-1)*2)-suma); x=a; } else if(ilekrawedzi>n-1){ //cout<<"TAK"<<endl; int a=(((n-1)*2)-suma); a=abs(a); tab[maks]-=a; x=a; } vector<int> tab2; for(int i=0;i<n;i++){ if(tab[i]!=0) tab2.push_back(tab[i]); } //cout<<"SIZE:"<<tab2.size()<<endl; if(int(tab2.size())<n){ int p=tab2.size()-1; for(int i=0;i<n-int(tab2.size());i++){ if(tab2[p]-1>0){ tab2.push_back(1); tab2[p]--; } else{ p--; i--; } } } sort(tab2.begin(),tab2.end()); cout<<x<<endl; cout<<n<<endl; for(int i=n-1;i>=0;i--){ //cout<<"I:"<<i<<" TAB2[i]:"<<tab2[i]<<endl; int ile=tab2[i]; int point=i; while(ile>0){ //cout<<"IDE DLA:"<<i<<endl; point--; if(point<0) break; if(tab2[point]>0){ cout<<i+1<<" "<<point+1<<endl; ile--; } tab2[point]--; } } /*set<int>::iterator it; it=tree.end(); cout<<(*it)<<endl; while(!tree.empty()){ it=tree.end(); int ile=*it; cout<<"ILE:"<<ile<<endl; set<int>::iterator it2; it2=tree.begin(); int b=*it2; for(int i=0;i<ile;i++){ cout<<"XD"<<endl; cout<<(*it)<<" "<<(*it2)<<endl; it2++; } tree.erase(it); for(int i=0;i<ile;i++){ int c=*it2 if(b<=1) tree.erase(it2); else{ tree.erase(it2); tree.insert(b-1); } } cout<<"NEXT"<<endl; }*/ /*cout<<ilekrawedzi<<" N:"<<n<<endl; for(int i=0;i<n;i++){ cout<<tab2[i]<<" "; } cout<<endl;*/ return 0; } |