跳过正文
  1. Posts/

codeforces #332 div 2 B. Spongebob and Joke

·2 分钟

B. Spongebob and Joke

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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 _a_1, _a_2, …, a__m of length m, consisting of integers from 1 to n, not necessarily distinct. Then he picked some sequence _f_1, _f_2, …, f__n of length n and for each number a__i got number b__i = f__a__i. To finish the prank he erased the initial sequence a__i.

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 f__i and b__i respectively.

The second line contains n integers, determining sequence _f_1, _f_2, …, f__n (1 ≤ f__i ≤ n).

The last line contains m integers, determining sequence _b_1, _b_2, …, b__m (1 ≤ b__i ≤ n).

Output

Print “Possible” if there is exactly one sequence a__i, such that b__i = f__a__i for all i from 1 to m. Then print m integers_a_1, _a_2, …, a__m.

If there are multiple suitable sequences a__i, print “Ambiguity”.

If Spongebob has made a mistake in his calculations and no suitable sequence a__i exists, print “Impossible”.

Sample test(s)

input

3 3

3 2 1 1 2 3

output

Possible

3 2 1

input

3 3

1 1 1 1 1 1

output

Ambiguity

input

3 3

1 2 1 3 3 3

output

Impossible

Note

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 f__i ≠ 3 for all i, so no sequence a__i transforms into such b__i and we can say for sure that Spongebob has made a mistake.

理解题意。。

需要注意的是。。。只有当在f中重复出现的那个在b中也出现了。。答案才是任意。。不然五影响。。。

 1#include <cstdio>
 2#include <iostream>
 3#include <cstring>
 4#include <algorithm>
 5
 6const int N=1E5+7;
 7int n,m;
 8int ans[N];
 9bool v[N];
10bool v2[N];
11using namespace std;
12
13struct node
14{
15int val;
16int id;
17}f[N],b[N];
18
19bool cmp( node a,node b)
20{
21return a.val<b.val;
22}
23int main()
24{
25cin>>n>>m;
26memset(v2,false,sizeof(v2));
27for ( int i = 0 ; i < n ; i++) scanf("%d",&f;[i].val),f[i].id=i;
28for ( int i = 0 ; i < m ; i++) scanf("%d",&b;[i].val),b[i].id=i;
29for ( int i = 0 ; i < m ; i++) v2[b[i].val] = true;
30
31sort(f,f+n,cmp);
32sort(b,b+m,cmp);
33
34bool multi = false;
35memset(v,false,sizeof(v));
36for ( int i = 0 ; i < n-1 ; i++) if (f[i].val==f[i+1].val&&v2;[f[i+1].val]) multi = true;
37for ( int i = 0 ; i < n ; i++) v[f[i].val] = true;
38bool sad = false;
39for ( int i = 0 ; i < m; i++) if (!v[b[i].val]) sad = true;
40memset(ans,0,sizeof(ans));
41int j = 0;
42bool flag = false;
43for ( int i = 0 ; i < n ; i++)
44{
45if (j==m) break;
46if (f[i].val==b[j].val&&j;<m)
47{
48while (f[i].val==b[j].val&&j;<m)
49{
50ans[b[j].id] = f[i].id;
51j++;
52}
53}
54
55}
56if (sad)
57{
58puts("Impossible");
59}
60else
61{
62if (multi) puts("Ambiguity");
63else
64{
65puts("Possible");
66for ( int i = 0 ; i < m-1 ; i++)
67printf("%d ",ans[i]+1);
68printf("%dn",ans[m-1]+1);
69}
70
71}
72
73
74
75
76}

相关文章

codeforces #332 div 2 A. Patrick and Shopping

·2 分钟
1#include <cstdio> 2#include <iostream> 3#include <cmath> 4using namespace std; 5long long d1,d2,d3; 6int main() 7{ 8cin>>d1>>d2>>d3; 9long long ans = 999999999999; 10ans = min(ans,d1+d2+d3); 11ans = min (ans,d1*2+d2*2); 12ans = min (ans,d1*2+2*d3); 13ans = min(ans,d2*2+2*d3); 14cout<<ans<<endl; 15return 0; 16} time limit per test 1 second

幻方....

·1 分钟
c语言上机。。。。 c写的幻方。 1/************************************************************************* 2> File Name: code/class/7.c 3> Author: 111qqz 4> Email: rkz2013@126.com 5> Created Time: 2015年11月11日 星期三 19时31分50秒 6************************************************************************/ 7 8#include<stdio.h> 9#include <string.h> 10 11int n; 12int a[105][105]; 13 13 14 15void swap(int *a,int *b) 16{ 17int tmp; 18tmp = *a; 19*a = *b; 20*b = tmp; 21} 22int fix_x( int x,int k,int n) 23{ 24if (k%2==1) 25{ 26if (x==0) 27return n; 28else return x; 29} 30else 31{ 32if (x==n) 33return n+n; 34else return x; 35} 36} 37int fix_y ( int y,int k,int n) 38{ 39if (k<3) 40{ 41if (y==n+1) 42return 1; 43else return y; 44} 45else 46{ 47if (y==2*n+1) 48return n+1; 49else return y; 50} 51// if (y==n+1) 52// return 1; 53// else return y; 54} 55void print() 56{ 57for ( int i = 1 ; i <= n ; i++) 58{ 59for ( int j = 1 ; j <= n ; j++) 60printf("%d ",a[i][j]); 61 61 62printf("n"); 63} 64 64 65} 66 66 67void OddMagic(int n,int x,int y,int k) //k表示4中状态。。。。 68{ 69 69 70int cur ; 71if (k==1) cur = 1; 72if (k==4) cur = n*n+1; 73if (k==3) cur = n*n*2+1; 74if (k==2) cur = n*n*3+1; 75int cnt = 1; 76while (cnt<=n*n) 77{ 78a[x][y]=cur; 79int prex = x; 80int prey = y; 81cur++; 82cnt++; 83x--; 84y++; 85x = fix_x(x,k,n); 86y = fix_y(y,k,n); 87if (a[x][y]) 88{ 89x = prex+1; 90y = prey; 91} 92 92 93} 94 94 95} 96int main() 97{ 98memset(a,sizeof(a),0); 99scanf("%d",&n); 100if (n%2==1) 101{ 102int x = 1; 103int y = n/2+1; 104OddMagic(n,x,y,1); 105} 106else 107{ 108if (n%4==0) 109{ 110for ( int i = 1,num=1 ; i <= n ; i++) 111for ( int j = 1 ; j <= n ; j++,num++) 112a[i][j]=num; 113 114 115for ( int i = 1 ; i <= n ; i++) 116{ 117for ( int j = 1 ; j <= n ; j++) 118{ 119if (i==j||i+j>=n+1) continue; 120int tmp; 121tmp = a[i][j]; 122a[i][j] = a[n+1-i][n+1-j]; 123a[n+1-i][n+1-j] = tmp; 124} 125} 126} 127else 128{ 129int x = 1; 130int y = n/4+1; 131int hn = n/2; 132 133OddMagic(hn,x,y,1); 134OddMagic(hn,x+hn,y,2); 135OddMagic(hn,x,y+hn,3); 136OddMagic(hn,x+hn,y+hn,4); 137 138int m = n/4; 139for ( int i = 1 ; i <= hn ;i++) 140{ 141for ( int j = 1 ; j <= m ; j++) 142{ 143int tmp; 144if (i==m+1&&j;==m) 145{ 146tmp = a[m+1][m+1]; 147a[m+1][m+1] = a[m+1+hn][m+1]; 148a[m+1+hn][m+1] = tmp; 149continue; 150 151} 152tmp = a[i][j]; 153a[i][j] = a[i+hn][j]; 154a[i+hn][j] = tmp; 155// swap(a[i][j],a[i+n][j]); 156} 157} 158 159for ( int i = 1 ; i <= hn ; i++) 160{ 161for ( int j = n ; j>=n-m+2 ; j--) 162{ 163int tmp; 164tmp = a[i][j]; 165a[i][j] = a[i+hn][j]; 166a[i+hn][j] = tmp; 167} 168} 169 170 171 172 173} 174} 175print(); 176 177}