Codeforces Round #332 (Div. 2)_B. Spongebob and Joke
2 seconds
256 megabytes
standard input
standard output
While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick's personal stuff and found a sequence a1, a2, ..., am of
length m, consisting of integers from 1 to n,
not necessarily distinct. Then he picked some sequence f1, f2, ..., fn of
length n and for each number ai got
number bi = fai.
To finish the prank he erased the initial sequence ai.
It's hard to express how sad Patrick was when he returned home from shopping! We will just say that Spongebob immediately got really sorry about what he has done and he is now trying to restore the original sequence. Help him do this or determine that this
is impossible.
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100 000) —
the lengths of sequences fi and bi respectively.
The second line contains n integers, determining sequence f1, f2, ..., fn (1 ≤ fi ≤ n).
The last line contains m integers, determining sequence b1, b2, ..., bm (1 ≤ bi ≤ n).
Print "Possible" if there is exactly one sequence ai,
such that bi = fai for
all i from 1 to m.
Then print m integers a1, a2, ..., am.
If there are multiple suitable sequences ai,
print "Ambiguity".
If Spongebob has made a mistake in his calculations and no suitable sequence ai exists,
print "Impossible".
3 3
3 2 1
1 2 3
Possible
3 2 1
3 3
1 1 1
1 1 1
Ambiguity
3 3
1 2 1
3 3 3
Impossible
In the first sample 3 is replaced by 1 and
vice versa, while 2 never changes. The answer exists and is unique.
In the second sample all numbers are replaced by 1, so it is impossible to unambiguously restore the original sequence.
In the third sample fi ≠ 3 for
all i, so no sequence ai transforms
into such bi and
we can say for sure that Spongebob has made a mistake.
用哈希过的
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std; const int max1 = 101000; struct node {
int x, y;
}a[max1]; int b[max1];
int f[max1];
int a1[max1]; int main() {
memset(f, 0, sizeof(f));
memset(a, 0, sizeof(a));
memset(a1, 0, sizeof(a1));
memset(b, 0, sizeof(b));
for (int i = 0; i<max1; i++)
a[i].x = 0;
int n, m;
cin >> n>> m;
for (int i = 1; i<=n; i++) {
cin >> f[i];
a[f[i]].x ++ ;
a[f[i]].y = i;
}
for (int i = 1; i<=m; i++)
cin >> b[i];
int flag1 = 0;
int flag = 0;
int num = 1;
for (int i = 1; i<=m; i++) {
if (a[b[i]].x == 1) {
a1[num++] = a[b[i]].y;
}
else if (a[b[i]].x >= 2) {
a1[num++] = a[b[i]].y;
flag = 1;
}
else if (a[b[i]].x == 0) {
flag = 2;
break;
}
}
if (flag == 2)
cout << "Impossible" << endl;
else if (flag == 1)
cout << "Ambiguity" << endl;
else {
cout << "Possible" << endl;
for (int i = 1; i<num-1; i++)
cout << a1[i] << " " ;
cout << a1[num-1] << endl; }
return 0; }
Codeforces Round #332 (Div. 2)_B. Spongebob and Joke的更多相关文章
- Codeforces Round #332 (Div. 2) B. Spongebob and Joke 水题
B. Spongebob and Joke Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/599 ...
- Codeforces Round #332 (Div. 2)B. Spongebob and Joke
B. Spongebob and Joke time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #332 (Div. 2) B. Spongebob and Joke 模拟
B. Spongebob and Joke While Patrick was gone shopping, Spongebob decided to play a little trick ...
- Codeforces Round #332 (Div. 二) B. Spongebob and Joke
Description While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. ...
- Codeforces Round #332 (Div. 2) D. Spongebob and Squares 数学题枚举
D. Spongebob and Squares Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...
- Codeforces Round #332 (Div. 2) D. Spongebob and Squares(枚举)
http://codeforces.com/problemset/problem/599/D 题意:给出一个数x,问你有多少个n*m的网格中有x个正方形,输出n和m的值. 思路: 易得公式为:$\su ...
- Codeforces Round #332 (Div. 2)D. Spongebob and Squares 数学
D. Spongebob and Squares Spongebob is already tired trying to reason his weird actions and calcula ...
- Codeforces Round #332 (Div. 2)
水 A - Patrick and Shopping #include <bits/stdc++.h> using namespace std; int main(void) { int ...
- Codeforces Round #332 (Div. 2) C. Day at the Beach 线段树
C. Day at the Beach Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/599/p ...
随机推荐
- iOS开发--数据库管理CoreData的使用
CoreData是iOS5后,苹果提供的原生的用于对象化管理数据并且持久化的框架.CoreData本质上是将底层数据库封装成对象进行管理.但数据库实际上只是CoreData的一个功能,并不是全部功能. ...
- fuser命令使用心得
fuser命令可用于查看正使用指定file, file system, socket port的进程信息.使用-k参数可将这些进程杀掉,-i则在杀掉进程前给出提示 例子: [root@bogon ~] ...
- iView的使用【小白向】
首先看这篇:构建Vue本地开发环境(现阶段还不知道怎么用CDN的方式做...) 安装iView(WindowsPowershell或cmd下用cnpm) 编辑上一篇博客创建的Vue工程 先到main. ...
- Visual simultaneous localization and mapping: a survey 论文解析(全)
当激光或声纳等距离传感器被用来构建小的静态环境的二维地图时,SLAM的问题被认为是解决的.然而,对于动态,复杂和大规模的环境,使用视觉作为唯一的外部传感器,SLAM是一个活跃的研究领域. 第一部分是简 ...
- nginx搭建rtmp协议流媒体服务器总结
最近在 ubuntu12.04+wdlinux(centos)上搭建了一个rtmp服务器,感觉还挺麻烦的,所以记录下. 大部分都是参考网络上的资料. 前提: 在linux下某个目录中新建一个nginx ...
- a:hover标签已经定义了text-decoration:none,并且生效,但是还是有下划线
a标签在F12计算出来的样式里 text-decoration:none; 确实有被应用到.但是链接的下划线并没有被去掉... 解决办法:p:commandLink <p:commandLink ...
- 删除redis 失效节点
cluster forget命令 cluster forget nodeid
- Android应用程序启动时发生AndroidRuntime : ClassNotFoundException for Activity class的解决方法
在android应用程序启动时抛出下面异常导致启动失败:07-09 17:12:35.709: ERROR/AndroidRuntime(3866): Uncaught handler: thread ...
- 关于 python 新式类和旧式类继承顺序的验证
参考:http://www.cnblogs.com/blackmatrix/p/5630515.html 官方:https://docs.python.org/2/tutorial/classes.h ...
- Mac下jenkins用户权限配置错误,导致登录时提示:没有Overall/read权限
由于初次接触jenkins,于是在搭建好jenkins以后,想要对用户进行管理,于是乎开始在系统管理->configure Global Security里设置用户的权限. 在启用安全-> ...