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 ...
随机推荐
- zabbix3.2.4监控MySQL5.7.16状态
一.添加监控用户mysql> grant all privileges on *.* to 'zabbix'@'localhost' identified by 'zabbix';mysql&g ...
- 剑指offer 面试18题
面试18题: 题目:删除链表中的节点 题一:在O(1)时间内删除链表节点.给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间内删除该节点. 解题思路:我们要删除节点i,先把i的下一个节点j ...
- python更新模块
pip install -U 模块名 # 这是 python2+ 版本的用法更新模块 pip3 install -U 模块名 # 这是 python3+ 版本的用法更新模块
- Android:日常学习笔记(8)———探究UI开发(3)
Android:日常学习笔记(8)———探究UI开发(3) 详解四种基本布局 前言 布局定义用户界面的视觉结构,如Activity或应用小部件的 UI.您可以通过两种方式声明布局: 在 XML 中声明 ...
- ajax json 异步请求
function ajaxTest(){ if (true) { $.ajaxSettings.async = false; var dataJson; $.getJSON("/univer ...
- 前端 初级篇(CSS)
CSS 简介 css是英文Cascading Style Sheets的缩写,称为层叠样式表,用于对页面进行美化. 存在方式有三种:元素内联.页面嵌入和外部引入,比较三种方式的优缺点. 语法:styl ...
- UI控件之UIPickerView的协议方法
UIPickerView:选择视图,父类是UIView UIPickerView *pickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(1 ...
- openGL学习进程(1)OpenGL的简介
通过本节,我们来简要了解一下openGL. (1)OpenGL概述: openGL(Open Graphics Library)是个专业的图形程序接口,定义了一个跨编程语言.跨平台的编 ...
- hadoop03---nginx+keepalived
1.1.反向代理 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求 ...
- 配置NFS作为HDFS高可用的共享存储系统
所有命令或步骤: 首先,在各个节点上安装nfs服务 yum install -y nfs service rpcbind start service nfs start 配置开机自启动服务 chkco ...