[Codeforces599B] 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 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 }
时间: 2024-10-06 16:55:29

[Codeforces599B] 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. 二) 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

CodeForces 599B Spongebob and Joke

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int maxn=100000+10; int n,m; int b[maxn],f[maxn]; int cnt[maxn]; int main() { scanf("%d%d",&n,&m); memset(cnt,

[cf 599B] Spongebob and Joke

判断题,有点绕,注意在答案不唯一时的判断条件要依据B数组判断 #include <iostream> #include <algorithm> #include <map> using namespace std; int b[110000],f[110000]; map <int,int> Map,A; int main() { int n,i,m; cin >> n >> m; for(i=1;i<=n;++i) { cin

codeforce 599B Spongebob and Joke

一道水题WA那么多发,也是醉了.f看成函数的话,其实就是判断一下反函数存不存在. 坑点,只能在定义域内判断,也就是只判断b[i].没扫一遍前不能确定Impossible. #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5+10; int f[maxn], b[maxn], a[maxn]; int g[maxn]; bool mul[maxn]; //#define L

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 从一个点出发,要经过其他两个点,然后回到原地,求最小时间花费.只有四种情况,从中选一个最小的就行了. #include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <math.h> #include <algorithm> using namespace

CentOS系统启动及内核大破坏模拟实验

讲过了centos的启动流程,此时是不是想来点破坏呢?那就尽情的玩耍吧,记得在实验之前拍个快照,万一哪个环节错误恢复不回来了呢,毕竟数据无价,话不多说,开始. 一.删除伪系统根.(ramdisk文件) (1)模拟误操作删除ramdisk文件. ①模拟误删除initramfs-3.10.0-514.el7.x86_64.img文件. ②为当前正在使用的内核重新制作ramdisk文件 格式为:mkinitrd /boot/initramfs-$(uname -r).img $(uname -r) (

NYOJ 2356: 哈希计划【模拟】

题目描述 众所周知,LLM的算法之所以菜,就是因为成天打游戏,最近LLM突然想玩<金庸群侠传X>,结果进去后各种被虐,LLM就开始研究这个游戏的代码,顺便还学会了一点点点点lua语言,然后就开始了伟大的改游戏代码之旅,然后LLM发现自己too young了,这个游戏把所有的文本都进行了哈希,如果自己改了代码或者剧情文本的话它哈希出来的值就会和原来的哈希值不一样......然后游戏就会打不开.....,现在LLM发现了文本的哈希函数,要求你写个程序,功能为: 输入一段字符串,输出一个哈希值 为了