题目链接:http://codeforces.com/problemset/problem/490/B

题目意思:给出每个人 i 站在他前面的人的编号 ai 和后面的人的编号 bi。注意,排在第一个位置的人他前面是无人的!于是 a1 = 0。最后那个人的后面是木有人的,即 bn = 0。然后根据这些条件求出整个序列是如何排的,输出答案。

这条题卡了好久.........啊........啊........啊

首先很容易知道第二个位置的人的编号 和 倒数第二个人的位置编号。用一个aft[]数组记录,aft[ai] = bi。表示 bi 在 ai 后面。然后从第 2 个位置开始,4、6,...n-2, n 就可以根据aft[] 数组来填出奇数位置的具体id number了。然后就开始卡在如何确定第 1 个位置应该填的id number了。然后好不容易用一个 bef[] 数组来求 (bef[bi] = ai),即从倒数第2个位置推回去直到第1个位置。赛后才发现这样只能处理 n 为偶数的情况。

这就表示一定要知道第 1 个位置具体填哪个 id number!知道之后 bef[] 数组就没必要使用了。

用一个cnt[] 数组来保存 ai 和 bi 的出现次数,通过观察可以发现,除了第 1 个和最后 1 个位置的人的 id number 的次数是 1 之外,其他位置都会出现 2 次。那么问题就是如何正确选出填在第 1 个位置的人的 id number 了。发现最后 1 个位置的人的 aft[] 是没有值的,那么如果有值,这个人就是排在第 1 个位置啦~~~~然后就可以根据 aft[] 数组来填1, 3, 5, ..., n-1 位置的数了。

想到 头 都快爆炸了 .............= =

不知道为什么题目类型归为 graphs,我觉得像想法题多点

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int N = 1e6 + ;
const int maxn = 2e5 + ; int aft[N], cnt[N];
int res[maxn]; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
int n, a, b, fst;
while (scanf("%d", &n) != EOF)
{
memset(aft, , sizeof(aft));
memset(cnt, , sizeof(cnt)); for (int i = ; i <= n; i++)
{
scanf("%d%d", &a, &b);
if (a == && b != )
res[] = b;
else if (a != && b == )
{
fst = a; // 这个很重要,因为有可能整个序列只有两个元素
res[n-] = a;
}
aft[a] = b;
cnt[a]++;
cnt[b]++;
}
for (int i = ; i <= N; i++)
{
if (cnt[i] == && aft[i] != )
{
fst = i;
break;
}
}
int tt = res[];
for (int i = ; i <= n; i += ) // 填偶数位置
{
if (aft[tt])
{
res[i] = aft[tt];
tt = res[i];
}
}
res[] = fst;
tt = fst;
for (int i = ; i <= n; i += ) // 填奇数位置
{
if (aft[tt])
{
res[i] = aft[tt];
tt = res[i];
}
}
for (int i = ; i <= n; i++)
printf("%d ", res[i]);
puts("");
}
return ;
}

codeforces 490B.Queue 解题报告的更多相关文章

  1. Codeforces #263 div2 解题报告

    比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...

  2. codeforces 31C Schedule 解题报告

    题目链接:http://codeforces.com/problemset/problem/31/C 题目意思:给出 n 个 lessons 你,每个lesson 有对应的 起始和结束时间.问通过删除 ...

  3. 【LeetCode】622. Design Circular Queue 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 用直的代替弯的 数组循环利用 日期 题目地址:htt ...

  4. hdu 1972.Printer Queue 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1972 题目意思:需要模拟打印机打印.打印机里面有一些 job,每个job被赋予1-9的其中一个值,越大 ...

  5. codeforces 499B.Lecture 解题报告

    题目链接:http://codeforces.com/problemset/problem/499/B 题目意思:给出两种语言下 m 个单词表(word1, word2)的一一对应,以及 profes ...

  6. codeforces 495C. Treasure 解题报告

    题目链接:http://codeforces.com/problemset/problem/495/C 题目意思:给出一串只有三种字符( ')','(' 和 '#')组成的字符串,每个位置的这个字符 ...

  7. CodeForces 166E -Tetrahedron解题报告

    这是本人写的第一次博客,学了半年的基础C语言,初学算法,若有错误还请指正. 题目链接:http://codeforces.com/contest/166/problem/E E. Tetrahedro ...

  8. codeforces 489A.SwapSort 解题报告

    题目链接:http://codeforces.com/problemset/problem/489/A 题目意思:给出一个 n 个无序的序列,问能通过两两交换,需要多少次使得整个序列最终呈现非递减形式 ...

  9. codeforces 485A.Factory 解题报告

    题目链接:http://codeforces.com/problemset/problem/485/A 题目意思:给出 a 和 m,a 表示第一日的details,要求该日结束时要多生产 a mod ...

随机推荐

  1. C语言动态内存分配

    考虑下面三段代码: 片段1 void GetMemory(char *p) { p = (); } void Test(void) { char *str = NULL; GetMemory(str) ...

  2. css3动画由浅入深总结

    阅读目录 一:过渡动画---Transitions 二:Animations功能 三:translate(tx,ty) 四:scale(x,y) 五:rotate(x): 5.1:skew(x,y): ...

  3. LazyLoad.js及scrollLoading.js

    http://blog.csdn.net/ning109314/article/details/7042829 目前图片延迟加载主要分两大块,一是触发加载(根据滚动条位置加载图片):二是自动预加载(加 ...

  4. PYTHON 自动化之路 (二)

    一.python 模块的使用 模块的使用: import os #调用 os 模块 cmd_s = os.popen("dir").read() #打开路径为结果保存为cmd_sp ...

  5. 对C++虚函数、虚函数表的简单理解

    一.虚函数的作用 以一个通用的图形类来了解虚函数的定义,代码如下: #include "stdafx.h" #include <iostream> using name ...

  6. Caffe学习系列(13):对训练好的模型进行fine-tune

    使用http://www.cnblogs.com/573177885qq/p/5804863.html中的图片进行训练和测试. 整个流程差不多,fine-tune命令: ./build/tools/c ...

  7. CCF 模拟B 无脑循环+输入输出外挂

    http://115.28.138.223:81/view.page?opid=2#code 代码一有WA点80分 #include<iostream> #include<cstdi ...

  8. windows安装rsync

    客户端:cwRsync 4.0.5 Installer 服务端:cwRsyncServer 4.0.5 Installer 安装配置Rsync服务端 1.直接双击安装包安装即可,在安装过程中会有要求用 ...

  9. 18.4---2出现了几次(CC150)

    思路:1,先给出LTE的代码: public static int countNumberOf2s(int n) { // write code here int res = 0; for(int i ...

  10. 7.5---两个正方形分成对半的直线(CC150)

    最主要的思路:1,这条直线就是要把两个正方形的中点链接. 2,注意的特殊情况:中心点重合. 答案: public class Solution { public static void main(St ...