CF-599B - Spongebob and Joke
2 seconds
256 megabytes
standard input
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 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 3
3 2 1
1 2 3
Possible
3 2 1
3 3
1 1 1
1 1 1
Ambiguity
3 3
1 2 1
3 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.
思路:
这是一个7:43分的早晨,关于这个提交了15遍最后才过了的题,我想写点什么。
首先想先谈谈自己对CF的认识,由于不让c++和c的格式混用(刚才发现是编译器的问题),至少错了5、6次,教训是立马他妈的去学怎么用cin和cout
好了,学完回来了
然后再说说这个坑爹的题吧,首先理解题意大概就花了能有1h。。。
先说说自己理解后现在的完全抽象版:
就是有两个数列,让你对第二个数列中每个数找到在第一个数列中的位置,如果有哪一项是找不到的,用某个值给他标记出来,那么这个test就是Impossible
如果有两个值找到的pos是相同的,那么这个test的ans就是Ambiguity的
如果没什么问题,即第2个数列的所有元素都能找到唯一且存在的值,那么就输出Possible然后分别输出第二个数列每个数在第一个数列中的位置
再总结几个get的小技巧:
(1)获取某个值在一个数组中的位置,可以让value做为数组下标,然后记录数组的序号。
这个时候如果遇到序号重叠的问题,就要看看之后用到pos的量是否需要这个值
(2)判断一个数组中是否有重复的元素以及数组中各个元素的数量问题,可以专门创建一个数组来表示先前被判断数组元素的数量关系
(3)有n(n大于等于3)个要判断的可能性,可以设置n-1个bool变量,然后一次判断每种可能性
#include <iostream>
#include <cstring>
using namespace std; int main()
{
int n,m;
int f[];
int b[];
int a[];
int pos[];
int num[];
while(cin>>n>>m)
{ /*
错误一:不应该存在的代码
if(n < m) {
printf("Impossible\n");
continue;
}
*/
memset(pos,,sizeof(pos));
/*错误二:num数组忘记清零*/
memset(num,,sizeof(num));
int Im = ;
int Am = ;
/*错误三:输入的突然中止*/
for(int i = ;i <= n;i++){
cin>>f[i];
pos[f[i]] = i;
num[f[i]]++;
}
for(int i = ;i <= m;i++) {
int t;
cin>>b[i];
if(num[b[i]] > ) Am = ;
a[i] = pos[b[i]];
if(a[i] == ) Im = ;
} if(Im) {
cout<<"Impossible"<<endl;
continue;
}
else if(Am) {
cout<<"Ambiguity"<<endl;
continue;
}
else {
cout<<"Possible"<<endl;
for(int i = ;i <= m-;i++)
cout<<a[i]<<' ';
cout<<a[m]<<endl;
}
}
return ;
}
CF-599B - Spongebob and Joke的更多相关文章
- Codeforces 599B. Spongebob and Joke 模拟
B. Spongebob and Joke time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- codeforce 599B Spongebob and Joke
一道水题WA那么多发,也是醉了.f看成函数的话,其实就是判断一下反函数存不存在. 坑点,只能在定义域内判断,也就是只判断b[i].没扫一遍前不能确定Impossible. #include<bi ...
- CodeForces 599B Spongebob and Joke
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- Codeforces Round #332 (Div. 2)B. Spongebob and Joke
B. Spongebob and Joke time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #332 (Div. 2) B. Spongebob and Joke 水题
B. Spongebob and Joke Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/599 ...
- Codeforces Round #332 (Div. 2)_B. Spongebob and Joke
B. Spongebob and Joke time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 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 ...
- Codeforces Round #332 (Div. 二) B. Spongebob and Joke
Description While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. ...
- CF 599D Spongebob and Squares(数学)
题目链接:http://codeforces.com/problemset/problem/599/D 题意:定义F(n,m)为n行m列的矩阵中方阵的个数,比如3行5列的矩阵,3x3的方阵有3个.2x ...
随机推荐
- Android笔记之 文件保存、压缩与清空删除
这两天改进优化项目中图片上传的代码.考虑到可能有7.8M的比較大的图片,由于要先进行压缩.所以设计到文件的压缩,保存与清空删除操作. 在这里记下笔记. /** * 压缩并另存为,每次先清空再保存 */ ...
- JQuery window、document、 body (转)
转自:http://www.cnblogs.com/luhe/archive/2012/11/08/2760619.html 我电脑屏幕分辨率:1440 * 900 最大化浏览器,刷新浏览器 al ...
- TreeSet与TreeMap
TreeSet底层使用的存储容器为TreeMap TreeMap使用红黑树(一种自平衡的排序二叉树)实现,检索效率为O(logn) 红黑树的三个基本操作:左旋.右旋.着色 平衡二叉树:空树或左右子树高 ...
- Linux之make 、makefile的使用方法
◊make是什么? make是一个命令工具,是一个解释makefile中指令的命令工具.它可以简化编译过程里面所下达的指令,当执行 make 时,make 会在当前的目录下搜寻 Makefile (o ...
- MM32 RTC学习(兼容STM32)
RTC学习 RTC简述 实时时钟是一个独立的定时器. RTC模块拥有一组连续计数的计数器,在相应软件配置下,可提供时钟日历的功能. 修改计数器的值可以重新设置系统当前的时间和日期. RTC模块和时钟配 ...
- mysql的limit性能,数据库索引问题,dblog问题
mysql的limit性能,数据库索引问题,dblog问题,redis学习 继续学习. dblog实际上是把日志记录在另一个数据库里面. 问题1: 一张表定义了5个索引,但是sql语句中用到了3个有索 ...
- maven项目,去除jar包中的不想要的依赖关系(Document root element "beans", must match DOCTYPE root "null". )
maven dependencies中并不会删除 以下方法maven dependencies中并不会删除,可能程序引入的时候,会去掉这种依赖(猜的) 解释: 就是说项目中要用到某一个a.jar包,通 ...
- checking for oracle home incompatibilities failed
安装Oracle软件的过程中,报错: 出错原因: 这个错误主要是oracle上一次安装失败,而没有删除干净而留下的目录文件造成. 解决办法: 清除原先目录下的文件,再retry或者重新安装即可. 示例 ...
- C++面试题:++i和i++哪个效率高?
1.当变量i的数据类型是c++语言默认提供的类型的话,他们的效率是一样的. 从其汇编执行的条数是一样的,所以其执行效率是一样的(有兴趣可以用gdb查看汇编代码) 2.我们自定的数据类型,++i效率高 ...
- PHP如何防止SQL注入及开发安全 53
PHP如何防止SQL注入及开发安全 [php] function inject_check($sql_str) { $check=eregi('select|insert|update|de ...