思路
2015-11-25 12:23
260 查看
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 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 test(s)
input
output
input
output
input
output
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 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.
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define pii pair < int, int >
#define pll pair < ll, ll >
#define all(s) s.begin(), s.end()
#define sz(s) (int) s.size()
#define vi vector < int >
const int inf = (1ll << 31) - 1;
const int mod = (int) 1e9 + 7;
int cnt[100100];
int f[100100];
int b[100100];
int pos[100100];
int n, m;
int main () {
#ifdef LOCAL
freopen ("a.in", "r", stdin);
freopen ("a.out", "w", stdout);
#endif
cin >> n >> m;
for(int i = 0; i < n; i++) {
cin >> f[i];
cnt[f[i]]++;
pos[f[i]] = i;
}
for(int i = 0; i < m; i++){
cin >> b[i];
}
for(int i = 0; i<m; i++){
if(cnt[b[i]] == 0 ){
cout << "Impossible\n";
return 0;
}
}
for(int i = 0; i<m; i++){
if(cnt[b[i]] > 1){
cout << "Ambiguity\n";
return 0;
}
}
cout << "Possible\n";
for(int i = 0; i < m; i++){
cout << pos[b[i]] + 1 << " ";
}
#ifdef LOCAL
cerr << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
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 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 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 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.
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define pii pair < int, int >
#define pll pair < ll, ll >
#define all(s) s.begin(), s.end()
#define sz(s) (int) s.size()
#define vi vector < int >
const int inf = (1ll << 31) - 1;
const int mod = (int) 1e9 + 7;
int cnt[100100];
int f[100100];
int b[100100];
int pos[100100];
int n, m;
int main () {
#ifdef LOCAL
freopen ("a.in", "r", stdin);
freopen ("a.out", "w", stdout);
#endif
cin >> n >> m;
for(int i = 0; i < n; i++) {
cin >> f[i];
cnt[f[i]]++;
pos[f[i]] = i;
}
for(int i = 0; i < m; i++){
cin >> b[i];
}
for(int i = 0; i<m; i++){
if(cnt[b[i]] == 0 ){
cout << "Impossible\n";
return 0;
}
}
for(int i = 0; i<m; i++){
if(cnt[b[i]] > 1){
cout << "Ambiguity\n";
return 0;
}
}
cout << "Possible\n";
for(int i = 0; i < m; i++){
cout << pos[b[i]] + 1 << " ";
}
#ifdef LOCAL
cerr << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
相关文章推荐
- 实现Cell的滑动删除
- asp.net缓存(转)
- Python面向对象关系
- pop 传值
- storm-starter的运行
- 扯扯服务端并发
- Unity3D-FSM有限状态机
- 文本分类入门(番外篇)特征选择与特征权重计算的区别
- Base64就是一种 基于64个可打印字符来表示二进制数据的表示方法
- wampYou don't have permission to access / on this server (WampServer 配置域名后 内网能访问 外网不能访问)
- 【已解决】Linux下出现Segmentation Fault(core dump)错误
- iOS开发中可以节省50%编译等待时间的2个措施
- IOS 关于Text Field设置键盘
- 一道字符串排列组合算法题
- VS2008多文本,CDockablePane里的视图位置调整!
- SCRUM敏捷开发教程
- 51单片机中断详解(中)
- nankai100_4
- React的一个简单示例
- 设计模式之禅之六大设计原则-接口隔离原则