Codeforces Round #332 (Div. 二) B. Spongebob and Joke

Description

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.

Input

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).

Output

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".

Sample Input

Input

3 33 2 11 2 3

Output

Possible3 2 1 

Input

3 31 1 11 1 1

Output

Ambiguity

Input

3 31 2 13 3 3

Output

Impossible

Hint

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.

题意:给你数组f[]和数组b[]问你是否存在数组a[]使得bi==fai;

题解:三种情况1、数组b中的数字数组f中没有 则输出Impossible     2、数组b中的数字在数组f中重复出现且数组b中的数字都在数组f中输出Ambiguity      3、数组b中的数字都在数组f中且无重复输出Possible以及数组a[]

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#define LL long long
#define MAX 100100
using namespace std;
int b[MAX],f[MAX];
int a[MAX],vis[MAX];
int main()
{
	int n,m,i,j,ans;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		memset(vis,0,sizeof(vis));
		for(i=1;i<=n;i++)
		{
			scanf("%d",&ans);
			f[ans]=i;
			vis[ans]++;
		}
		for(i=1;i<=m;i++)
			scanf("%d",&b[i]);
		int flag=0;
		int Flag=0;
		for(i=1;i<=m;i++)
		{
			if(vis[b[i]]==0)
			{
				flag=1;
				break;
			}
			if(vis[b[i]]>1)
			    Flag=1;
		}
		if(flag)
		{
			printf("Impossible\n");
			continue;
		}
		if(Flag)
		{
			printf("Ambiguity\n");
			continue;
		}
		for(i=1;i<=m;i++)
			a[i]=f[b[i]];
		printf("Possible\n");
		for(i=1;i<m;i++)
		printf("%d ",a[i]);
		printf("%d\n",a[m]);
	}
	return 0;
}

  

时间: 2024-12-18 09:48:58

Codeforces Round #332 (Div. 二) B. Spongebob and Joke的相关文章

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 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

Codeforces Round #332 (Div. 2)D. Spongebob and Squares 数学

D. Spongebob and Squares Spongebob is already tired trying to reason his weird actions and calculations, so he simply asked you to find all pairs of n and m, such that there are exactly x distinct squares in the table consisting of n rows and m colum

Codeforces Round #332 (Div. 2) D. Spongebob and Squares

Spongebob is already tired trying to reason his weird actions and calculations, so he simply asked you to find all pairs of n and m, such that there are exactly x distinct squares in the table consisting of n rows and m columns. For example, in a 3?×

Codeforces Round #332 (Div. 2) D. Spongebob and Squares(枚举)

http://codeforces.com/problemset/problem/599/D 题意:给出一个数x,问你有多少个n*m的网格中有x个正方形,输出n和m的值. 思路: 易得公式为:$\sum_{i=0}^{n}(n-i)(m-i) $ 化简得:$\left [ n(n+1)-\frac{n(n+1)}{2}\right ]*m+\frac{n(n+1)(n+2)}{6}-\frac{n(n+1)}{2}*n$ 将n作为小的数,枚举n即可. 1 #include<iostream>

Codeforces Round #332 (Div. 2)

好菜,不说话了,说题. A - Patrick and Shopping 从一个点出发,要经过其他两个点,然后回到原地,求最小时间花费.只有四种情况,从中选一个最小的就行了. #include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <math.h> #include <algorithm> using namespace

2017-5-18-Train: Codeforces Round #332 (Div. 2)

A. Patrick and Shopping(模拟题) Today Patrick waits for a visit from his friend Spongebob. To prepare for the visit, Patrick needs to buy some goodies in two stores located near his house. There is a d1 meter long road between his house and the first sh

Codeforces Round #332 (Div. 2)A. Patrick and Shopping 水

A. Patrick and Shopping Today Patrick waits for a visit from his friend Spongebob. To prepare for the visit, Patrick needs to buy some goodies in two stores located near his house. There is a d1 meter long road between his house and the first shop an

Codeforces Round #427 (Div. 2) C. Star sky(二维前缀和)

题目链接:Codeforces Round #427 (Div. 2) C. Star sky 题意: 在一个二维平面上有n个星星,每个星星有一个初始的亮度,每过去一秒,星星的亮度变化为(s+1)%(c+1). 现在有q个询问,问t秒后一个矩形区域的星星的总亮度为多少. 题解: 由于c不大,将每颗星星按照初始亮点分类,每一类在任意时间的亮度都是相同的,然后对应加一加就行了. 1 #include<bits/stdc++.h> 2 #define RT(l,r) (l+r|l!=r) 3 #de

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd