UVALive - 7752 Free Figurines
题意:有n个娃娃,如果大娃娃j直接套小娃娃i,则fa[i] = j。若fa[i] = 0,则该娃娃自由。给出每个娃娃初始的父亲,和改变后的父亲,在满足以下合法操作的条件下,问最少需要多少次变换。
1、一个自由的娃娃可以被套在一个比自己大且为空的自由地娃娃里。
2、打开一个非空的娃娃,取出里面的娃娃。
分析:
1、若一个娃娃的父亲改变了,则必须打开套在他外面的所有娃娃。
2、若一个娃娃a成为另一个娃娃新的父亲,则这个娃娃a必须是自由的,即必须打开套在娃娃a外面的所有娃娃。
3、所有牵扯到改变的娃娃都变成了自由,满足变换条件,因此依次嵌套即可。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int fa1[MAXN];
int fa2[MAXN];
bool change[MAXN];
int main(){
int n;
while(scanf("%d", &n) == 1){
memset(change, false, sizeof change);
for(int i = 1; i <= n; ++i){
scanf("%d", &fa1[i]);
}
for(int i = 1; i <= n; ++i){
scanf("%d", &fa2[i]);
if(fa1[i] != fa2[i]){
change[i] = true;
change[fa2[i]] = true;
}
}
int ans = 0;
for(int i = 1; i <= n; ++i){
if(change[i] && fa1[i] != 0){
int cnt = 0;
for(int j = fa1[i]; j != 0;){
int tmpj = j;
j = fa1[j];
fa1[tmpj] = 0;
++cnt;
}
fa1[i] = 0;
ans += cnt;
}
}
for(int i = 1; i <= n; ++i){
if(fa1[i] != fa2[i]){
++ans;
}
}
printf("%d\n", ans);
}
return 0;
}
UVALive - 7752 Free Figurines的更多相关文章
- UVALive 7752 Free Figurines (瞎搞)
题意:给定 n 个盒子,然后告诉你每个盒子在哪个盒子里,数值越大,盒子越大,给定你初态,和末态,问你最少要几步能完成,只有两种操作,一种是把一个盒子连同里面的小盒子放到一个空盒子里,另一种是把一个堆盒 ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 6508 Permutation Graphs
Permutation Graphs Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- UVALive 6500 Boxes
Boxes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Pract ...
- UVALive 6948 Jokewithpermutation dfs
题目链接:UVALive 6948 Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...
随机推荐
- Linux命令:ss命令
ss功能:用来显示套接字信息的,类似于netstat,可以显示更多的信息,用于替代netstat. ss常用选项 ss -t:tcp协议的连接 -u:udp协议的链接 -w:裸套接字相关 -x:uni ...
- day21-Python运维开发基础(单个字符匹配 / 多字符匹配)
1. 正则表达式(单个字符匹配) # ### 正则表达式 => 单个字符匹配 import re """ lst = re.findall(正则表达式,字符串) & ...
- Linux进程管理(一)
目录 Linux进程管理(一) 参考 pstree命令 pidof命令 pmap命令 pwdx命令 ps命令 nice调优 发送信号 Linux进程管理(一)
- nginx_1_初始nginx
一.nginx简介: nginx是一个性能优秀的web服务器,同时还提供反向代理,负载均衡,邮件代理等功能.是俄罗斯人用C语言开发的开源软件. 二.安装nginx step1:安装依赖库 pcre(支 ...
- unity 骨骼 蒙皮
https://blog.csdn.net/weixin_44350205/article/details/100551233 https://www.jianshu.com/p/d5e2870eb3 ...
- pycharm安装PyQt框架
首先需要安装必要插件 File --> Settings --> Project Interpreter 如果出现以下这种情况:请升级pip,参考:https://www.cnbl ...
- P1002 A+B for Polynomials (25分)
1002 A+B for Polynomials (25分) This time, you are supposed to find A+B where A and B are two polyn ...
- php+ajax实现无刷新动态加载数据技术
我们浏览有些网页的时候,当拉动浏览器的滚动条时到页底时,页面会继续自动加载更多内容供用户浏览.这种技术我暂且称它为滚屏加载技术.我们发现很多网站用到这种技术,必应图片搜索.新浪微博.QQ空间等将该技术 ...
- 「POI2015」KIN
传送门 Luogu 解题思路 想要做这道题,只要会维护区间最大子段和就好了. 而这个可以用线段树维护模板点这里 对于重复的情况,我们可以对每一个位置记一个前驱表示和当前位置种类相同的前一个位置. 然后 ...
- 前端学习笔记系列一:9 js中数组的拷贝
拷贝分为浅拷贝和深拷贝,在JavaScript中能够实现这两种拷贝的方式也是多种多样.以下是一维数组实现深拷贝和浅拷贝的各种方式. 一.浅拷贝 1.赋值 赋值是最直接的一种浅拷贝. let arr3 ...