Codeforces Round #279 (Div. 2) B. Queue
2 seconds
256 megabytes
standard input
standard output
During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.
Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).
After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.
Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue.
The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue.
Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist.
The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order.
Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one.
4
92 31
0 7
31 0
7 141
92 7 31 141
The picture illustrates the queue for the first sample.
题意:告诉你前驱和后继,求原序列;
思路:两个数位置相差2;
找到前后的起点,两个方向扫一遍;
#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define esp 0.00000000001
const int N=3e5+,M=1e6+,inf=1e9,mod=1e9+;
int a[M];
int b[M];
int ans[M];
int flag[M]; int main()
{
int x,y,z,i,t;
scanf("%d",&x);
for(i=;i<x;i++)
{
scanf("%d%d",&y,&z);
a[y]=z;
b[z]=y;
flag[y]++;
flag[z]++;
}
int st=;
int ji=;
while(a[st])
{
ans[ji+]=a[st];
flag[a[st]]=;
st=a[st];
ji+=;
}
if(x&)
{
int en=;
for(i=;i<=M;i++)
if(flag[i]&&!a[i])
en=i;
int ji=x;
while(en)
{
ans[ji]=en;
en=b[en];
ji-=;
}
}
else
{
int ji=x-;
int en=;
while(b[en])
{
ans[ji]=b[en];
en=b[en];
ji-=;
}
}
for(i=;i<=x;i++)
printf("%d ",ans[i]);
return ;
}
2 seconds
256 megabytes
standard input
standard output
During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.
Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).
After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.
Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue.
The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue.
Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist.
The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order.
Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one.
4
92 31
0 7
31 0
7 141
92 7 31 141
The picture illustrates the queue for the first sample.
Codeforces Round #279 (Div. 2) B. Queue的更多相关文章
- Codeforces Round #279 (Div. 2)B. Queue(构造法,数组下标的巧用)
这道题不错,思维上不难想到规律,但是如何写出优雅的代码比较考功力. 首先第一个人的序号可以确定,那么接下来所有奇数位的序号就可以一个连一个的确定了.然后a[i].first==0时的a[i].seco ...
- Codeforces Round #279 (Div. 2) B - Queue 水题
#include<iostream> #include<mem.h> using namespace std; ],q[]; int main() { int n,x,y; m ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- 水题 Codeforces Round #303 (Div. 2) D. Queue
题目传送门 /* 比C还水... */ #include <cstdio> #include <algorithm> #include <cstring> #inc ...
- 【Codeforces Round#279 Div.2】B. Queue
这题看别人的.就是那么诚实.http://www.cnblogs.com/zhyfzy/p/4117481.html B. Queue During the lunch break all n Ber ...
- Codeforces Round #279 (Div. 2) 题解集合
终于有场正常时间的比赛了...毛子换冬令时还正是好啊233 做了ABCD,E WA了3次最后没搞定,F不会= = 那就来说说做的题目吧= = A. Team Olympiad 水题嘛= = 就是个贪心 ...
- Codeforces Round #303 (Div. 2) D. Queue 傻逼题
C. Woodcutters Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...
- Codeforces Round #279 (Div. 2) C. Hacking Cypher 机智的前缀和处理
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #incl ...
- Codeforces Round #303 (Div. 2) D. Queue —— 贪心
题目链接:http://codeforces.com/problemset/problem/545/D 题解: 问经过调整,最多能使多少个人满意. 首先是排序,然后策略是:如果这个人对等待时间满意,则 ...
随机推荐
- python web框架 django wsgi 理论
django wsgi python有个自带的wsgi模块 可以写自定义web框架 用wsgi在内部创建socket对象就可以了 自己只写处理函数就可以了django只是web框架 他也不负责写soc ...
- Matlab GUI memo
有一段时间没写博客,一周4篇文章都坚持不下来,不知道写哪个方面的内容,写研究相关就怕论文查重查到,其他方面也没太多时间去学.还是花时间多学点其他方面.废话到此,很早就做过matlab gui相关,现在 ...
- VoIP应用系统大盘点
一.VoIP拓扑 PBX是程控交换机,程控交换机有实体交换机和软件模拟的交换机. 软件模拟的交换机,即交换机服务器,常用开源的sip服务器有asterisk,freepbx, opensip等,商用的 ...
- Spark SQL中UDF和UDAF
转载自:https://blog.csdn.net/u012297062/article/details/52227909 UDF: User Defined Function,用户自定义的函数,函数 ...
- POJ3254:Corn Fields(状压dp第一发)
题目:http://poj.org/problem?id=3254 直接上代码吧,刚开始做时主要的问题就是看不懂二进制,有个博客写的太好了,就直接把题解复制在下面了. #include <ios ...
- Hibernate错误:Could not bind factory to JNDI
使用hibernate时,将hibernate.cfg.xml中 <session-factory name="SessionFactory">的那么属性去掉即可.因为 ...
- C++学习笔记--异常简介
C++异常是对程序运行过程中发生的异常情况(如被0除)的一种响应.异常提供了将控制权从程序的一个部分传递到另一部分的途径. 1.对异常的处理有3个部分组成: (1)引发异常 (2)捕获有处理程序的异常 ...
- beego——日志处理
这是一个用来处理日志的库,它的设计思路来自于 database/sql,目前支持的引擎有 file.console.net.smtp,可以通过如下方式进行安装: go get github.com/a ...
- 牛客国庆集训派对Day3 I. - Metropolis (Dijkstra变型)
题意:求一个N个点无向图中,其中p个关键点间的最短距离. 分析:比较特殊的最短路,方式类似于多源BFS,将所有关键点装入优先队列,状态中需要包含其源点的id.对每条边都要遍历,对每个节点,需要记录其确 ...
- WebMagic 爬虫框架
官方网站[http://webmagic.io/](http://webmagic.io/) >webmagic是一个开源的Java垂直爬虫框架,目标是简化爬虫的开发流程,让开发者专注于逻辑功能 ...