Codeforces Round #279 (Div. 2)B. Queue(构造法,数组下标的巧用)
这道题不错,思维上不难想到规律,但是如何写出优雅的代码比较考功力。
首先第一个人的序号可以确定,那么接下来所有奇数位的序号就可以一个连一个的确定了。然后a[i].first==0时的a[i].secod就是第二个人的序号,然后偶数位的序号也可以一个连一个的确定了。
用一个next数组,其下标就是a[i].first,其值就是a[i].second,这样巧妙地使用数组下标就解决了“串链子”这个难点,我之前想的每次用二分来找真是弱爆了。。
而在找第一个人的序号时也是妙用flag数组下标。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const int eps=1e-;
const int INF=;
const int maxn=+;
pair<int,int>a[];
int next[maxn],flag[maxn],n,u,v,ans[];
int main()
{
//freopen("in2.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&a[i].first,&a[i].second);
flag[a[i].second]=;
next[a[i].first]=a[i].second;
if(a[i].first==) v=a[i].second;
}
for(int i=;i<n;i++)
{
if(flag[a[i].first]==)
{
u=a[i].first;
break;
}
}
for(int i=;i<n;i+=)
{
ans[i]=v;
v=next[v];
}
for(int i=;i<n;i+=)
{
ans[i]=u;
u=next[u];
}
for(int i=;i<n-;i++)
{
printf("%d ",ans[i]);
}
printf("%d\n",ans[n-]);
//fclose(stdin);
//fclose(stdout);
return ;
}
Codeforces Round #279 (Div. 2)B. Queue(构造法,数组下标的巧用)的更多相关文章
- Codeforces Round #279 (Div. 2) B. Queue
B. Queue time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- 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 #276 (Div. 2)C. Bits(构造法)
这道题直接去构造答案即可. 对于l的二进制表示,从右到左一位一位的使其变为1,当不能再变了(再变l就大于r了)时,答案就是l. 这种方法既可以保证答案大于等于l且小于等于r,也可以保证二进制表示时的1 ...
- 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 #298 (Div. 2) D. Handshakes 构造
D. Handshakes Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/problem ...
- 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 ...
随机推荐
- jquery mobile 带参数跳转收集(紧个人使用,测试完会补全)
//临时存储 var TempCache = { cache:function(value){ localStorage.setItem("EasyWayTempCache",va ...
- Serv-u 外网访问内网的FTP服务器
1. 背景简介 最近研究如何在内网搭架FTP服务器,同时要保证外网(公网)能访问的到.终成正果,但走了一些弯路,在此记下,以飨后人. 2. 基础知识 FTP 使用 2 个端口,一个数据端口和一个命令端 ...
- position:absolute width
position:absolute; left:0px; right:0px; top:0px; bottom:0px; 设置布满整个父范围,设置了absolute的元素可以通过以上4个属性来展开面, ...
- PHP日期与时间戳转换
设置时区 在php.ini中找到data.timezone去掉它前面的;号,然后设置data.timezone = "Asia/Shanghai"; 或者 ini_set('dat ...
- Linux系统资源查询命令(cpu、io、mem)
cpu/mem: 1. 指定pid top -p pid1,pid2,... 2. top排序 先top,然后 输入大写P,则结果按CPU占用降序排序.输入大写M,结果按内存占用降序排序. io: ...
- iOS 11 Xcode9开发 新特性学习 (新方法篇)
1 . 引入github (1) 在Xcode 9 中,引入了 gitHub,新源代码管理导航器 可以展示branch分支和 tag标签. (2)点进去,就可以看指定一次commit了哪些东西 2 ...
- 【leetcode刷题笔记】Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- Linux 邮件服务搭建
Linux 邮件服务搭建 邮件服务针对,在大型企业使用的比较多,一般小型企业都会买一些邮件服务,或者使用一些免费的邮件服务,达到我们使用的需求,并且不需要自己维护,下面我就来简单安装一下两个邮箱的案例 ...
- INSPIRED启示录 读书笔记 - 第31章 苹果公司给我的启示
苹果公司值得学习的经验 1.硬件为软件服务:苹果公司明白硬件必须为软件服务,软件直接服务用户,满足用户需求.采用多点触控显示屏.重力加速器.距离传感器这些硬件技术是为了配合软件满足用户需求 2.软件为 ...
- Jquery 获取地址位置
直接在浏览器地址 输入: http://pv.sohu.com/cityjson?ie=utf-8 可以查看数据格式 引入一个搜狐的js库: <script src="http://p ...