Codeforces Round #332 (Div. 2) B. Spongebob and Joke
2016-09-02 08:34
399 查看
题目链接:http://codeforces.com/contest/599/problem/B
题目大意:给定你一个数组a1,a2,……,am,里面的包含的是1-n的整数(可以重复),再给定一个数组f1,f2,……,fn,然后规定数组b符合b(i)=f(ai)。a数组被擦掉了,现在给定你f数组和b数组,让你判断a数组的情况。n,m(1<=m,n<=100000)。 第二行n个数表示的是f数组(1<=f(i)<=n)。第三行m个数表示的是b数组(1<=b(i)<=n)。 如果a数组存在且唯一,要求输出Possible,站一行。接下来一行输出a1,a2,……,am用空格隔开,站一行。如果a数组存在但不是唯一存在,要求输出Ambiguity。如果a数组不存在要求输出Impossible。
解题思路:首先这道题基本很多人都会认为判断f数组是否重复就是判断是否是唯一的 其实不然 这里不应该这么判断 应该去判断b数组在f数组中是唯一的 反之则说明不是唯一的 开个数组c c[f[i]]=i c[b[i]]则是a数组 这里我们要判断c[b[i]]是否等于0 因为范围是在1 到 10W之间 不包括0 初始化为0 如果它等于0 就说明它找不到一个与之相匹配的 说明不存在
//Time:108 ms //Memory: 4720 KB #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <map> #include <cmath> #include <queue> #include <string> #include <vector> #include <set> #include <stack> using namespace std; const int maxn=100005; bool flag1,flag2; int n,m; int f[maxn],b[maxn],a[maxn],c[maxn]; map<int,int> mp; int main() { while(~scanf("%d %d",&n,&m)) { flag1=true;flag2=true; memset(a,0,sizeof(a)); memset(c,0,sizeof(c)); for(int i=1;i<=n;i++) { scanf("%d",&f[i]); c[f[i]]=i;//为了求a数组 mp[f[i]]++; } for(int i=1;i<=m;i++) { scanf("%d",&b[i]); a[i]=c[b[i]]; if(mp[b[i]]>1)//不唯一 出现多次 flag2=false; if(a[i]==0)//不存在的条件 flag1=false; } if(flag1) { if(!flag2) puts("Ambiguity"); else { puts("Possible"); for(int i=1;i<=m;i++) printf("%d%c",a[i],i==m?'\n':' '); } } else { puts("Impossible"); } mp.clear(); } return 0; }
相关文章推荐
- Codeforces Round #332 (Div. 二) B. Spongebob and Joke
- Codeforces Round #332 (Div. 2) B. Spongebob and Joke(水题,构造)
- 【Codeforces Round 332 (Div 2)B】【扭转题意 位置映射】Spongebob and Joke 给b[]中的每个数找f[]中的位置
- Codeforces Round #332 (Div. 2) B. Spongebob and Joke 水题
- Codeforces Round #332 (Div. 2)B. Spongebob and Joke(模拟)
- Codeforces Round #332 (Div. 2)B. Spongebob and Joke
- Codeforces Round #332 (Div. 2) B. Spongebob and Joke
- codeforces #332 div 2 B. Spongebob and Joke
- Codeforces Round #332 (Div. 2) B. Spongebob and Joke
- Codeforces Round #332 (Div. 2)_B. Spongebob and Joke
- Codeforces Round #332 (Div. 2)_B. Spongebob and Joke
- Codeforces 599 B Spongebob and Joke
- Spongebob and Joke(记录数据地址)
- K - Spongebob and Joke
- CF-599B - Spongebob and Joke
- CodeForces 599B A - Spongebob and Joke
- CF-599B - Spongebob and Joke
- 【Codeforces Round 332 (Div 2)D】【数学 公式推导】Spongebob and Squares 正方形数量恰好为x个的所有大矩形
- 【CodeForces】599B - Spongebob and Joke(STL)
- Codeforces 599B: Spongebob and Joke(技巧,规律)