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 ...
随机推荐
- Windows下RabbitMQ安装及入门
1.Windows下安装RabbitMQ需要以下几个步骤 (1):下载erlang,原因在于RabbitMQ服务端代码是使用并发式语言erlang编写的,下载地址:http://www.erlang. ...
- CSS篇(上)
紧接着HTML篇的CSS篇开啦,老铁们快来围观... 1.介绍一下标准的CSS盒子模型?低版本IE的盒子模型有什么不同? 1>有两种:IE盒子模型 W3C盒子模型 2>盒模型:内 ...
- 新手了解.Nat
1.Net平台 -->.Net平台 -->.Net FrameWrok框架 2.C#编程语言 -->编程语言:计算机能听懂的语言 -->使用.Net平台 C#是在.NEt平台 ...
- Apache Avro# 1.8.2 Specification (Avro 1.8.2规范)一
h4 { text-indent: 0.71cm; margin-top: 0.49cm; margin-bottom: 0.51cm; direction: ltr; color: #000000; ...
- MongoDB数据库详解
第1章 数据库管理系统 1.1 前言 01.数据的定义:文字.图像.地理位置信息(坐标.经纬度)等 02.数据库管理系统的定义:建立.存取和管理数据,保证数据安全和完整性的软件 03.常见的数据库管理 ...
- 无法打开文件“freeglut.lib”解决方法:
资源: 链接:https://pan.baidu.com/s/1eSctT5K 密码:174s VS2010问题: 无法打开文件"freeglut.lib"解决方法: (1)下载f ...
- struts快速入门第一篇 —— struts相关XML配置映射及讲解
我们回忆一下在学习JavaWeb过程中(Jsp + servlet编程)所感受到的Servlet的不足: 1 Servllet很多时,web.xml中的代码会很多.这样一来,维护起来就不方便,不利于团 ...
- MyBatis单个多个参数传递
一.传入单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList ...
- TPYBoard V102:能跑Python的stm32开发板
近来micropython语言做硬件编程很火,随之而来的就开始带动着支持micropython语言编程的开发板也开始火的发烫,今天小编就来和大家介绍一款很经典的micropython开发板-TPYBo ...
- Hadoop源码篇--Client源码
一.前述 今天起剖析源码,先从Client看起,因为Client在MapReduce的过程中承担了很多重要的角色. 二.MapReduce框架主类 代码如下: public static void m ...