CodeForces 1152E Neko and Flashback
题目链接:http://codeforces.com/problemset/problem/1152/E
题目大意
有一个 1~n-1 的排列p 和长度为 n 的数组 a,数组b,c定义如下:
b:bi = min(ai, ai + 1),1 <= i <= n-1。
c:ci = max(ai, ai + 1),1 <= i <= n-1。
数组b',c'定义如下:
b':b'i = bpi,1 <= i <= n-1。
c':c'i = cpi,1 <= i <= n-1。
给定数组 b',c',求 a。
分析
代码如下
#include <bits/stdc++.h>
using namespace std; #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< string, int > PSI;
typedef set< int > SI;
typedef vector< int > VI;
typedef map< int, int > MII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; struct Edge{
int id;
int from, to; Edge() {}
Edge(int id, int from, int to) : id(id), from(from), to(to) {}
}; struct Vertex{
int in = ;
vector< Edge > edges;
}; int n;
int b[maxN], c[maxN], a[maxN];
unordered_map< int, Vertex > mIV;
int vis[maxN];
int S;
int cnt; // Hierholzer算法求欧拉路
inline void hierholzer(int s) {
vector< Edge > &tmp = mIV[s].edges;
while(!tmp.empty()) {
Edge t = tmp.back();
tmp.pop_back(); if(vis[t.id]) continue;
vis[t.id] = ;
hierholzer(t.to);
a[cnt--] = t.to;
}
} int main(){
INIT();
cin >> n;
For(i, , n - ) cin >> b[i];
For(i, , n - ) {
cin >> c[i];
if(c[i] < b[i]) {
cout << - << endl;
return ;
}
}
For(i, , n - ) {
mIV[b[i]].edges.PB(Edge(i, b[i], c[i]));
++mIV[b[i]].in;
mIV[c[i]].edges.PB(Edge(i, c[i], b[i]));
++mIV[c[i]].in;
} // 找起点
// cnt记录奇度顶点个数
foreach(i, mIV) {
if(i->sd.in % == ) {
++cnt;
S = i->ft;
}
}
if(S == ) S = b[]; // 没有奇度顶点就随便选一个顶点
if(cnt > || cnt == ) { // 有两个或0个奇度顶点,才可能有欧拉通路
cout << - << endl;
return ;
} cnt = n;
hierholzer(S);
a[cnt--] = S; // 整个图可能不连通
if(cnt == )For(i, , n) cout << a[i] << " ";
else cout << -;
cout << endl;
return ;
}
CodeForces 1152E Neko and Flashback的更多相关文章
- [欧拉路]CF1152E Neko and Flashback
1152E - Neko and Flashback 题意:对于长为n的序列c和长为n - 1的排列p,我们可以按照如下方法得到长为n - 1的序列a,b,a',b'. ai = min(ci, ci ...
- Codeforces Round #554 (Div. 2) E Neko and Flashback (欧拉路径 邻接表实现(当前弧优化..))
就是一欧拉路径 贴出邻接表欧拉路径 CODE #include <bits/stdc++.h> using namespace std; const int MAXN = 100005; ...
- codeforces#1152D. Neko and Aki's Prank(dp)
题目链接: https://codeforces.com/contest/1152/problem/D 题意: 给出一个$n$,然后在匹配树上染色边,每个结点的所有相邻边只能被染色一次. 问,这颗树上 ...
- codeforces#1152C. Neko does Maths(最小公倍数)
题目链接: http://codeforces.com/contest/1152/problem/C 题意: 给出两个数$a$和$b$ 找一个$k(k\geq 0)$得到最小的$LCM(a+k,b+k ...
- Codeforces C.Neko does Maths
题目描述: C. Neko does Maths time limit per test 1 second memory limit per test 256 megabytes input stan ...
- CodeForces 1152D Neko and Aki's Prank
说明 Catalan(i) 表示卡特兰数的第 i 项. 题目链接:http://codeforces.com/problemset/problem/1152/C 题目大意 有 n 个左括号和 n 个右 ...
- CodeForces 1152F2 Neko Rules the Catniverse (Large Version)
题目链接:http://codeforces.com/problemset/problem/1152/F2 题目大意 见http://codeforces.com/problemset/problem ...
- CodeForces 1152F1 Neko Rules the Catniverse (Small Version)
题目链接:http://codeforces.com/problemset/problem/1152/F1 题目大意 有 n 个星球,给定限制 m,从 x 星球走到 y 星球的条件是,$1 \leq ...
- E. Neko and Flashback
传送门: 题意:假定我们已知a[]={3,4,6,5,7}, 那么b[]通过min(a[i],a[i+1])得到 那么b[]={3,4,5,5}, c[]通过max(a[i],a[i+1])得到 c ...
随机推荐
- Android中对应用程序的行为拦截实现方式概要
这次是真的好长时间都没有写博客了,主要不是因为工作上的事,主要还是这个问题真的有点复杂,实现起来有点麻烦,所以研究了很长时间(大约有一个月的时间).但是幸好最后问题搞定了~~ 一.问题场景 想实现36 ...
- Shell基础(一):Shell基础应用、简单Shell脚本的设计、使用Shell变量、变量的扩展应用
一.Shell基础应用 目标: 本案例要求熟悉Linux Shell环境的特点,主要练习以下操作: 1> 切换用户的Shell环境 2> 练习命令历史.命令别名 3 ...
- Minimum Snap轨迹规划详解(1)轨迹规划
一. 轨迹规划是什么? 在机器人导航过程中,如何控制机器人从A点移动到B点,通常称之为运动规划.运动规划一般又分为两步: 1.路径规划:在地图(栅格地图.四\八叉树.RRT地图等)中搜索一条从A点到B ...
- ES6(简单了解)
1.import类似于var,不过是定义对外接口的,接受外部的文件. import xx from xx ,有点像var i =3: 如import profile from './prof ...
- Ext OOP基础
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 如何从ST官网下载STM32标准库
Frm:https://blog.csdn.net/k1ang/article/details/79645044
- Xen的半虚拟化(Paravirtualization)
三个特权级 IA-32体系提供了4个特权级别,正常情况下只用了2个, 操作系统运行在Ring 0,而应用程序运行在Ring 3. Xen让自己运行在Ring 0, 而操作系统运行在Ring 1, 应用 ...
- 41-Ubuntu-用户管理-06-su切换用户
su 切换用户 序号 命令 作用 说明 01 su - 用户名 切换用户,并且切换家目录 '-'可以切换到用户家目录,否则保持位置不变 02 exit 退出当前登录账户 返回上一级用户 图:su与ex ...
- 第十二篇 requests模拟登陆知乎
了解http常见状态码 可以通过输入错误的密码来找到登陆知乎的post:url 把Headers拉到底部,可以看到form data _xsrf是需要发送的,需要发送给服务端,否则会返回403错误,提 ...
- kafka消息深入学习
Kafka是一个分布式的基于发布/订阅模式的消息队列,主要应用于大数据实时处理领域. 1 快写 快读 看下面的图: 传统应用是 硬件到缓存,到应用 再socket进行传输,再进行网络传输,再到用 ...