思路
来源:互联网 发布:美国人的聊天软件 编辑:程序博客网 时间:2024/04/20 13:40
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 33 2 11 2 3
Possible3 2 1
3 31 1 11 1 1
Ambiguity
3 31 2 13 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 <bits/stdc++.h>using namespace std;#define pb push_back#define ll long long#define mp make_pair#define f first#define s second#define pii pair < int, int >#define pll pair < ll, ll >#define all(s) s.begin(), s.end()#define sz(s) (int) s.size()#define vi vector < int >const int inf = (1ll << 31) - 1;const int mod = (int) 1e9 + 7;int cnt[100100];int f[100100];int b[100100];int pos[100100];int n, m;int main () { #ifdef LOCAL freopen ("a.in", "r", stdin); freopen ("a.out", "w", stdout); #endif cin >> n >> m; for(int i = 0; i < n; i++) { cin >> f[i]; cnt[f[i]]++; pos[f[i]] = i; } for(int i = 0; i < m; i++){ cin >> b[i]; } for(int i = 0; i<m; i++){ if(cnt[b[i]] == 0 ){ cout << "Impossible\n"; return 0; } } for(int i = 0; i<m; i++){ if(cnt[b[i]] > 1){ cout << "Ambiguity\n"; return 0; } } cout << "Possible\n"; for(int i = 0; i < m; i++){ cout << pos[b[i]] + 1 << " "; } #ifdef LOCAL cerr << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0;}
- 思路!
- 思路
- 思路
- 思路
- 思路
- 思路
- 思路
- 思路
- 思路呀思路
- 学习思路
- 改变思路
- 设计思路
- 整理思路
- 思路图两张
- 原始思路
- 面试思路
- 面试思路
- 设计思路
- 文本分类入门(番外篇)特征选择与特征权重计算的区别
- 扯扯服务端并发
- 三年之痒(20151125)
- pop 传值
- 实现Cell的滑动删除
- 思路
- 按钮倒计时
- 换硬币
- oracle 先安装客户端再安装服务端导致的tns无法解析错误--2015.11.25
- 创建通知
- 爱勾搭技术沙龙第一期 ——互联网中高端技术从业者如何突破职业瓶颈
- 图算法:2、计算带有负权值的单源最短路径:Bellman-Ford算法
- oracle 11g中sql plus使用
- 前端2