time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Graph constructive problems are back! This time the graph you are asked to build should match the following properties.

The graph is connected if and only if there exists a path between every pair of vertices.

The diameter (aka "longest shortest path") of a connected undirected graph is the maximum number of edges in the shortest path between any pair of its vertices.

The degree of a vertex is the number of edges incident to it.

Given a sequence of nn integers a1,a2,…,ana1,a2,…,an construct a connected undirected graph of nn vertices such that:

  • the graph contains no self-loops and no multiple edges;
  • the degree didi of the ii-th vertex doesn't exceed aiai (i.e. di≤aidi≤ai);
  • the diameter of the graph is maximum possible.

Output the resulting graph or report that no solution exists.

Input

The first line contains a single integer nn (3≤n≤5003≤n≤500) — the number of vertices in the graph.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n−11≤ai≤n−1) — the upper limits to vertex degrees.

Output

Print "NO" if no graph can be constructed under the given conditions.

Otherwise print "YES" and the diameter of the resulting graph in the first line.

The second line should contain a single integer mm — the number of edges in the resulting graph.

The ii-th of the next mm lines should contain two integers vi,uivi,ui (1≤vi,ui≤n1≤vi,ui≤n, vi≠uivi≠ui) — the description of the ii-th edge. The graph should contain no multiple edges — for each pair (x,y)(x,y) you output, you should output no more pairs (x,y)(x,y) or (y,x)(y,x).

Examples
input

Copy
3
2 2 2
output

Copy
YES 2
2
1 2
2 3
input

Copy
5
1 4 1 1 1
output

Copy
YES 2
4
1 2
3 2
4 2
5 2
input

Copy
3
1 1 1
output

Copy
NO
Note

Here are the graphs for the first two example cases. Both have diameter of 22.

d1=1≤a1=2d1=1≤a1=2

d2=2≤a2=2d2=2≤a2=2

d3=1≤a3=2d3=1≤a3=2

d1=1≤a1=1d1=1≤a1=1

d2=4≤a2=4d2=4≤a2=4

d3=1≤a3=1d3=1≤a3=1

d4=1≤a4=1

这个题无语,自己打比赛的时候看错题,求成最短的了,mdzz。。。

直接构造一条最长链就可以。从大到小连,为1的两边最多能连2个,其他的中间连,然后就可以了。

代码:

 //D
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int n,m,t,p,ans;
int d[maxn*],first[maxn*],v[maxn*],w[maxn*],nextt[maxn*]; struct node{
int p,d; bool operator <(const node &a)const{
return d<a.d;
} }a[maxn]; vector<pair<int,int> > ve; ll pre[maxn]; int main()
{
int n;
cin>>n;
int flag=;
for(int i=;i<=n;i++){
cin>>a[i].d,a[i].p=i;
if(a[i].d!=)flag=;
}
if(flag==) {cout<<"NO"<<endl;return ;}
sort(a+,a++n);
for(int i=;i<=n;i++)
pre[i]=pre[i-]+a[i].d;
int i=n-,con=n,tail=n,len=,flag1=,flag2=;
while(){
if(a[con].d&&a[i].d) {ve.push_back(make_pair(a[con].p,a[i].p));a[con].d--;a[i].d--;con--;i--;len++;}
else if(!a[con].d&&flag1==) {ve.push_back(make_pair(a[tail].p,a[i].p));a[tail].d--;a[i].d--;i--;len++;flag1=;if(a[tail].d==) tail--;}
else{
if(a[tail].d!=) {ve.push_back(make_pair(a[tail].p,a[i].p));a[tail].d--;a[i].d--;i--;if(a[tail].d==) tail--;}
else {flag2=;break;}
}
if(i==) break;
}
if(flag2==) {cout<<"NO"<<endl;return ;}
vector<pair<int,int> >::iterator it;
cout<<"YES "<<len<<endl;
cout<<ve.size()<<endl;
for(it=ve.begin();it!=ve.end();it++){
cout<<(*it).first<<" "<<(*it).second<<endl;
}
}

Codeforces 1082 D. Maximum Diameter Graph-树的直径-最长链-构造题 (Educational Codeforces Round 55 (Rated for Div. 2))的更多相关文章

  1. CodeForces 1082 D Maximum Diameter Graph

    题目传送门 题意:现在有n个点,每个点的度数最大为di,现在要求你构成一棵树,求直径最长. 题解:把所有度数为2的点先扣出来,这些就是这颗树的主干,也就是最长的距离. 然后我们把度数为2的点连起来,之 ...

  2. Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph

    D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...

  3. Educational Codeforces Round 55 (Rated for Div. 2) D. Maximum Diameter Graph (构造图)

    D. Maximum Diameter Graph time limit per test2 seconds memory limit per test256 megabytes inputstand ...

  4. Educational Codeforces Round 55 (Rated for Div. 2)

    D. Maximum Diameter Graph 题意 给出每个点的最大度,构造直径尽可能长的树 思路 让度数大于$1$的点构成链,考虑是否能在链的两端加度为$1$的点 代码 #include &l ...

  5. Educational Codeforces Round 55 (Rated for Div. 2) Solution

    A. Vasya and Book Solved. 三种方式取$Min$ #include <bits/stdc++.h> using namespace std; #define ll ...

  6. Educational Codeforces Round 55 (Rated for Div. 2) A/B/C/D

    http://codeforces.com/contest/1082/problem/A WA数发,因为默认为x<y = = 分情况讨论,直达 or x->1->y  or  x-& ...

  7. Codeforces 1082 C. Multi-Subject Competition-有点意思 (Educational Codeforces Round 55 (Rated for Div. 2))

    C. Multi-Subject Competition time limit per test 2 seconds memory limit per test 256 megabytes input ...

  8. Codeforces 1082 B. Vova and Trophies-有坑 (Educational Codeforces Round 55 (Rated for Div. 2))

    B. Vova and Trophies time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))

    A. Vasya and Book time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

随机推荐

  1. 替换Jar包中的一个文件 Replace a file in a JAR

    例如: jar uf myJarFile.jar com\vsoft\servlet\myServlet.class This will replace the class myServlet.cla ...

  2. 【BZOJ1043】下落的圆盘 [计算几何]

    下落的圆盘 Time Limit: 10 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Description 有n个圆盘从天而降,后面落下的可 ...

  3. 【BZOJ1598】牛跑步 [A*搜索]

    牛跑步 Time Limit: 10 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Description BESSIE准备用从牛棚跑到池塘的方 ...

  4. 【洛谷 P1073】 最优贸易 (Tarjan缩点+拓扑排序)

    题目链接 先\(Tarjan\)缩点,记录每个环内的最大值和最小值. 然后跑拓扑排序,\(Min[u]\)表示到\(u\)的最小值,\(ans[u]\)表示到\(u\)的答案,\(Min\)和\(an ...

  5. ButterKnife用法详解

    http://www.cnblogs.com/zhaoyanjun/p/6016341.html 本文出自[赵彦军的博客] 前言 ButterKnife 简介 ButterKnife是一个专注于And ...

  6. Java 关于微信公众号支付总结附代码

    很多朋友第一次做微信支付的时候都有蒙,但当你完整的做一次就会发现其实并没有那么难 业务流程和应用场景官网有详细的说明:https://pay.weixin.qq.com/wiki/doc/api/js ...

  7. android 内核调试

    这篇文档给出使用android emulator 和 arm-linux-androideabi-gdb 调试 android kernel 的方法 1. checkout goldfish 源码: ...

  8. python实战===用python对比两张图片的不同

    from PIL import Image from PIL import ImageChops def compare_images(path_one, path_two, diff_save_lo ...

  9. Centos_Lvm expand capacity without restarting CentOS

    Rescan the new disk(/dev/sdb): #ls /sys/class/scsi_host/ host0 host1 host2 [root@db210_13:56:14 /dat ...

  10. selenium 点击浏览器按钮

    利用以下的方法,selenium 也可以模拟点击各种浏览器按钮:browser.back()点击“返回”按钮.browser.forward()点击“前进”按钮.browser.refresh()点击 ...