2016 多校联赛7 Elegant Construction
A city with N towns (numbered 1 through N) is under construction. You, the architect, are being responsible for designing how these towns are connected by one-way roads. Each road connects two towns, and passengers can travel through in one direction.
For business purpose, the connectivity between towns has some requirements. You are given N non-negative integers a1 .. aN. For 1 <= i <= N, passenger start from town i, should be able to reach exactly ai towns (directly or indirectly, not include i itself). To prevent confusion on the trip, every road should be different, and cycles (one can travel through several roads and back to the starting point) should not exist.
Your task is constructing such a city. Now it's your showtime!
InputThe first line is an integer T (T <= 10), indicating the number of test case. Each test case begins with an integer N (1 <= N <= 1000), indicating the number of towns. Then N numbers in a line, the ith number ai (0 <= ai < N) has been described above.OutputFor each test case, output "Case #X: Y" in a line (without quotes), where X is the case number starting from 1, and Y is "Yes" if you can construct successfully or "No" if it's impossible to reach the requirements.
If Y is "Yes", output an integer M in a line, indicating the number of roads. Then M lines follow, each line contains two integers u and v (1 <= u, v <= N), separated with one single space, indicating a road direct from town u to town v. If there are multiple possible solutions, print any of them.
Sample Input
3
3
2 1 0
2
1 1
4
3 1 1 0
Sample Output
Case #1: Yes
2
1 2
2 3
Case #2: No
Case #3: Yes
4
1 2
1 3
2 4
3 4 启发来自于队内大佬余神和楼主
首先判读能否构成这样的有向无环图,对拿到的出度进行排序,然后看比一个点出度小的点数有没有小于等于它本身的出度,如果有大于的则输出no,全部小于等于则输出yes
判断yes后,有一个最傻瓜的思路,就是全先连0,然后连1,然后连下去。因为小的数连完以后再连大的数之后就不用考虑大数所连数,直接+1即可。
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std; int b[]; struct node
{
int u,v;
node(int x,int y){u=x;v=y;}
}; struct node2
{
int num,id;
}a[]; bool cmp(node2 m,node2 n)
{
return m.num<n.num;
}
int main()
{
int T;
scanf("%d",&T);
for(int t=;t<=T;t++)
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i].num);
a[i].id=i;
}
printf("Case #%d: ",t);
sort(a+,a+n+,cmp);
memset(b,,sizeof(b));
int s=,tmp=;
for(int i=;i<=n;i++)
{ if(a[i].num==a[i-].num)
{
b[i]=s;
tmp++;
}
else
{
s+=tmp;
b[i]=s;
tmp=;
}
}
bool flag=true;
for(int i=;i<=n;i++)
if(a[i].num>b[i])
{
flag=false;
break;
}
if(!flag)
printf("No\n");
else
{
printf("Yes\n");
queue<node>Q;
int num=;
for(int i=;i<=n;i++)
{
for(int j=;j<=a[i].num;j++)
{
Q.push(node(a[i].id,a[j].id));
num++;
}
}
printf("%d\n",num);
while(!Q.empty())
{
printf("%d %d\n",Q.front().u,Q.front().v);
Q.pop();
}
}
}
return ;
}
2016 多校联赛7 Elegant Construction的更多相关文章
- hdu5737(2016多校联赛第2场D)
题意:给2组数据a和b数组,每次有2种操作:(+,l,r,x)把a数组第l个到第r个元素全置为x,(?,l,r)查询[l,r]之间哪些位置满足a[i]>=b[i](i>=l &&a ...
- 2016 多校联赛7 Joint Stacks (优先队列)
A stack is a data structure in which all insertions and deletions of entries are made at one end, ca ...
- 2016 多校联赛7 Balls and Boxes(概率期望)
Mr. Chopsticks is interested in random phenomena, and he conducts an experiment to study randomness. ...
- HDU 5813 Elegant Construction (贪心)
Elegant Construction 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5813 Description Being an ACMer ...
- HDU5813 Elegant Construction
Elegant Construction Time Li ...
- HDU 5813 Elegant Construction(优雅建造)
HDU 5813 Elegant Construction(优雅建造) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65 ...
- HDU 5813 Elegant Construction 构造
Elegant Construction 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5813 Description Being an ACMer ...
- 2015 HDU 多校联赛 5363 Key Set
2015 HDU 多校联赛 5363 Key Set 题目: http://acm.hdu.edu.cn/showproblem.php? pid=5363 依据前面给出的样例,得出求解公式 fn = ...
- 2015 HDU 多校联赛 5317 RGCDQ 筛法求解
2015 HDU 多校联赛 5317 RGCDQ 筛法求解 题目 http://acm.hdu.edu.cn/showproblem.php? pid=5317 本题的数据量非常大,測试样例多.数据 ...
随机推荐
- React文档(一)安装
React是一个灵活的可以用于各种不同项目的框架,你可以用它来写新应用,你也可以逐步将它引进已有的代码库而不用重写整个项目. 试用React 如果你想玩一玩React,那么就去CodePen上试一试. ...
- navicat 连接 mysql 解决出现client does not support authentication protocol requested by server的问题
MySQL8换了加密插件,数据库管理客户端都来不及更新,连接方式缺乏sha2的加密方式首先第一步, UPDATE mysql.user SET plugin = 'mysql_native_passw ...
- 78. Subsets C++回溯法
本题还是基本的回溯法.就是回溯函数的参数选择上要花点心思! class Solution { public: void backTrack(vector<int> ans, vector& ...
- 牛客第二场A-run
链接:https://www.nowcoder.com/acm/contest/140/A 来源:牛客网 White Cloud is exercising in the playground. Wh ...
- ActiveMQ 的连接和会话
要了解 connection 和 session 的概念,可以先从 ConnectionState 和 SessionState 入手: // 省略部分代码 public class Connecti ...
- JavaScript 上万条数据 导出Excel文件(改装版)
最近项目要js实现将数据导出excel文件,网上很多插件实现~~那个开心呀,谁知道后面数据量达到上万条时出问题:浏览器不仅卡死,导出的excel文件一直提示网络失败.... debug调试发现var ...
- MySQL变量变更小记
MySQL会随版本的更新,在新版本中淘汰一些variable和引入一些新的variable.在配置variable后不起作用或安全扫描取不到variable值产生告警时,可能正是variable变更的 ...
- 创建含有多module的springboot工程(八)
创建根工程 创建一个maven 工程,其pom文件为: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <?xml version="1.0" enc ...
- linux下top命令参数详解
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法. 内存信息.内容如下: Mem: 191272k to ...
- DLL的Export和Import及extern "C"
今天使用Unrar.dll,在调用RARProcessFileW时,VS总是提示“error LNK2001: 无法解析的外部符号”. Unrar.dll中是使用 extern "C&quo ...