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 ...
随机推荐
- Yii2 使用 Beanstalk 队列系统
参考网址: Beanstalk:https://github.com/kr/beanstalkd Beanstalk console:https://github.com/ptrofimov/bean ...
- Loadrunder脚本篇——文件下载
下载简介 对 HTTP协议来说,无论是下载文件或者请求页面,对客户端来说,都只是发出一个GET请求,并不会记录点击后的“保存”.“另存为操作”. 如下,点击页面中tar.gz压缩包,用工具可以清楚的看 ...
- 【Tech】POI标签分类
寒假老板给的任务,让我重现这个实验http://www.liuhaihua.cn/archives/15565.html.自己就随便试了下,用的都是比较经典(lao)的算法和知识,记录一下. 一.从网 ...
- 最长公共子序列的C++实现---附二维指针的使用方法
想了挺久到底第一篇在这儿的博客写什么好,刚好这两天又一次看到动态规划的LCS算法觉得还是有点意思的,就拿来写了写,第一篇博客就发它吧. #include<iostream> #includ ...
- How to Google
程序员的基础生存技能 -- 关于搜索引擎的小贴士 如果票选近二十年最伟大的发明,我相信搜索引擎肯定会占据一个不容小觑的位置,它不单是一项发明,更是一项成就,最大程度消灭了信息的不平等.既然人人都可以接 ...
- Docker 数据管理-bind mount
Use bind mounts Bind mounts have been around since the early days of Docker. Bind mounts have limite ...
- mysql中的一些操作
查询mysql中事务提交的情况: show variables like '%commit%'; 可以查看当前autocommit值 在mysql数据库中它的默认值是"on"代表自 ...
- 用vim写python脚本的自动缩进格式设置
- SQL中的5种常用的聚集函数
首先你要知道 where->group by->having->order by/limit ,这个就是写sql语句时的顺序 常用的5个聚集函数: Max ...
- MySql 创建/删除数据库
C:\Users\Mr.Black>mysql -u root -pEnter password: ****Welcome to the MySQL monitor. Commands end ...