码迷,mamicode.com
首页 > 其他好文 > 详细

[Codeforces599B] Spongebob and Joke (模拟)

时间:2015-11-22 17:22:04      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

  • 题目概述:

  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.

  给你数列f1, f2, ..., fn和数列b1, b2, ..., bm,求数列a1, a2, ..., am,使得∀i ∈ [1, m], 1 ≤ a(i) ≤ n且b(i) = f(a(i))(将括号理解为下标)

  • 输入格式:

  第一行包括两个整数n, m(1 ≤ n, m ≤ 100000);

  第二行包括n个整数,表示数列f1, f2, ..., fn;

  第三行包括m个整数,表示数列b1, b2, ..., bm.

  • 输出格式:

  如果有唯一一组ai,第一行输出"Possible",第二行输出a1, a2, ..., am, 用空格隔开;

  如果有多组ai,输出"Ambiguity";

  如果不存在ai,输出"Impossible".

  • 样例输入1:

  3 3

  3 2 1

  1 2 3

  • 样例输出1:

  Possible

  3 2 1

  • 样例输入2:

  3 3

  1 1 1

  1 1 1

  • 样例输出2:

  Ambiguity

  • 样例输入3:

  3 3

  1 2 1

  3 3 3

  • 样例输出3:

  Impossible

  • 思路:

  如果存在多个fi = bj,就可能为Ambiguity; 如果不存在fi = bj,就可能为Impossible。

  并不是说如果两个f相同就是Ambiguity,例如:

  3 3

  1 2 1

  2 2 2

  虽然f1 = f3 = 1,但bi != 1,所有的ai = 2。

  先对fi进行一次桶排,将重复出现的fi标记一次,依题意模拟一遍求所有ai即可。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int a[100005], b[100005], f[100005], yf[100005], flag;
 4 bool vis[100005];
 5 int main() {
 6     int n, m;
 7     cin >> n >> m;
 8     for(int i = 1; i <= n; i++)
 9         cin >> f[i];
10     for(int i = 1; i <= m; i++)
11         cin >> b[i];
12     for(int i = 1; i <= n; i++)
13         if(!yf[f[i]]) yf[f[i]] = i;
14         else vis[f[i]] = true;
15     for(int i = 1; i <= m; i++)
16         if(vis[b[i]]) flag = 1;
17         else if(!vis[b[i]] && !yf[b[i]]) {
18             flag = 2; break;
19         }
20         else a[i] = yf[b[i]];
21     if(flag == 1) cout << "Ambiguity" << endl;
22     else if(flag == 2) cout << "Impossible" << endl;
23     else {
24         cout << "Possible" << endl;
25         for(int i = 1; i <= m; i++)
26             cout << a[i] <<  ;
27         cout << endl;
28     }
29     return 0;
30 }

 

[Codeforces599B] Spongebob and Joke (模拟)

标签:

原文地址:http://www.cnblogs.com/CtrlCV/p/4986075.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!