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 ...
随机推荐
- Qt--自定义Delegate
这是Model/View中的最后一篇了,Qt官方显然弱化了Controller在MVC中的作用,提供了一个简化版的Delegate:甚至在Model/View框架的使用中,提供了默认的委托,让这个控制 ...
- 通过 JS 实现简单的拖拽功能并且可以在特定元素上禁止拖拽
前言 关于讲解 JS 的拖拽功能的文章数不胜数,我确实没有必要大费周章再写一篇重复的文章来吸引眼球.本文的重点是讲解如何在某些特定的元素上禁止拖拽.这是我在编写插件时遇到的问题,其实很多插件的拖拽功能 ...
- BZOJ 4816 数字表格
首先是惯例的吐槽.SDOI题目名称是一个循环,题目内容也是一个循环,基本上过几年就把之前的题目换成另一个名字出出来,喜大普奔亦可赛艇.学长说考SDOI可以考出联赛分数,%%%. 下面放解题报告.并不喜 ...
- 2、各种折腾,安装交叉环境的gcc和qt,测试c++和qt程序
本博文仅作本人操作过程的记录,留作备忘.自强不息 QQ1222698 1.安装gcc和qt 把光盘里带的gcc-4.4.4-glibc-2.11.1-multilib-1.0_EasyARM-iMX2 ...
- 快速开发 jQuery 插件的 10 大技巧
在开发过很多 jQuery 插件以后,我慢慢的摸索出了一套开发jQuery插件比较标准的结构和模式.这样我就可以 copy & paste 大部分的代码结构,只要专注最主要的逻辑代码就行了. ...
- Maven项目不打包*.hbm.xml文件
<build> <finalName>basic</finalName> <plugins> <plugin> <groupId> ...
- C# DataGridView中DataGridViewComboBoxCell列,下拉框事件的处理【完美解决】
http://blog.csdn.net/a312100321/article/details/25195311 问题:DataGridView绑定数据源之后,有一列需要用下拉框DataGridVie ...
- DJango_生命周期
在django中,当我们访问一个url时,会通过路由匹配进入到响应的html页面中. Django的生命周期,指的就是当用户在浏览器上输入url,到用户看到整个页面之前,django后台都做了哪些事情 ...
- js解析xml浏览器兼容性处理
/****************************************************************************** 说明:xml解析类 ********** ...
- 微信小程序开发之picker
一.绑定简单数组 通过bindChange控制index,使得当前选择值发生改变 示例1 data: { Data: ['A','B'], Index: 0, }, <picker class= ...