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 本题的数据量非常大,測试样例多.数据 ...
随机推荐
- SQL 经典回顾:JOIN 表连接操作不完全指南
2017-02-23 小峰 ITPUB 点击上方“蓝字”可以关注我们哦  |转载自:码农网 |原文链接:www.codeceo.com/article/sql-join-guide.html ...
- 接口测试--postman简介
一.什么是接口测试 接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.测试的重点是要检查数据的交换,传递和控制管理过程,以及系统间的相互逻辑 ...
- 【转】Vue 脱坑记 - 查漏补缺(汇总下群里高频询问的xxx及给出不靠谱的解决方案)
前言 文章内容覆盖范围,芝麻绿豆的破问题都有,不止于vue; 给出的是方案,但不是手把手一字一句的给你说十万个为什么! 有三类人不适合此篇文章: “喜欢站在道德制高点的圣母婊” – 适合去教堂 “无理 ...
- Qt_Pro详解
在QT中,有一个工具qmake可以生成一个makefile文件,它是由.pro文件生成而来的,.pro文件的写法如下: 1. 注释 从“#”开始,到这一行结束. 2.模板变量告诉qmake为这个应用程 ...
- mysql 数据库的CUDR
mysql删表和建表语句: DROP TABLE IF EXISTS `t_blog_user`;CREATE TABLE `t_blog_user` ( `id` int(11) NOT NULL ...
- 把旧系统迁移到.Net Core 2.0 日记(7) Tag Helpers /ResponseCache
Tag Helpers是Html Helpers的一种替换 比如,原来的视图模型定义是这样的: @using (Html.BeginForm("Register", "A ...
- linux文件权限多一个+啥意思
linux文件权限显示多了一个+,说明添加了acl权限 使用getfacl filename 能查看到 ower group other 还多了一个php 用户有rwx权限. acl详解 转: ht ...
- nyoj 0325 zb的生日(dp)
nyoj 0325 zb的生日 zb的生日 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 今天是阴历七月初五,acm队员zb的生日.zb正在和C小加.never在武汉集 ...
- 逆袭之旅DAY24.XIA.二重进阶、双色球
一. 选择题. 1. 以下关于二重循环的说法正确的是(D). A. 二重循环就是一般程序中只能有两个循环 B. While循环不能嵌套在for循环里 C. 两个重叠的循环不能嵌套在第三个循环里. D. ...
- Ubuntu 16.04 中安装谷歌 Chrome 浏览器
http://jingyan.baidu.com/article/335530da98061b19cb41c31d.html 根据教程安装成功!! http://askubuntu.com/quest ...