题意:给定 n 个盒子,然后告诉你每个盒子在哪个盒子里,数值越大,盒子越大,给定你初态,和末态,问你最少要几步能完成,只有两种操作,一种是把一个盒子连同里面的小盒子放到一个空盒子里,另一种是把一个堆盒子里的最外面的那个盒子拿出来。

析:首先,先遍历一次,如果初态和不一样,那么初态后面的要全部拿出来,然后再遍历一次,然后如果发现不一样,然后要看把末态的父结点是不是孤立的,如果不是,也要全部拿出。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <sstream>
  17. #include <list>
  18. #include <assert.h>
  19. #include <bitset>
  20. #define debug() puts("++++");
  21. #define gcd(a, b) __gcd(a, b)
  22. #define lson l,m,rt<<1
  23. #define rson m+1,r,rt<<1|1
  24. #define fi first
  25. #define se second
  26. #define pb push_back
  27. #define sqr(x) ((x)*(x))
  28. #define ms(a,b) memset(a, b, sizeof a)
  29. //#define sz size()
  30. #define pu push_up
  31. #define pd push_down
  32. #define cl clear()
  33. #define all 1,n,1
  34. #define FOR(x,n) for(int i = (x); i < (n); ++i)
  35. #define freopenr freopen("in.txt", "r", stdin)
  36. #define freopenw freopen("out.txt", "w", stdout)
  37. using namespace std;
  38.  
  39. typedef long long LL;
  40. typedef unsigned long long ULL;
  41. typedef pair<int, int> P;
  42. const int INF = 0x3f3f3f3f;
  43. const LL LNF = 1e15;
  44. const double inf = 1e20;
  45. const double PI = acos(-1.0);
  46. const double eps = 1e-8;
  47. const int maxn = 1e5 + 100;
  48. const int mod = 7;
  49. const int dr[] = {-1, 0, 1, 0};
  50. const int dc[] = {0, 1, 0, -1};
  51. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  52. int n, m;
  53. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  54. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  55. inline bool is_in(int r, int c) {
  56. return r >= 0 && r < n && c >= 0 && c < m;
  57. }
  58.  
  59. int fa[maxn];
  60. int pa[maxn];
  61. int ans;
  62.  
  63. void dfs(int x){
  64. if(pa[x] == 0) return ;
  65. dfs(pa[x]); pa[x] = 0;
  66. ++ans;
  67. }
  68.  
  69. int main(){
  70. while(scanf("%d", &n) == 1){
  71. for(int i = 1; i <= n; ++i) scanf("%d", pa+i);
  72. for(int i = 1; i <= n; ++i) scanf("%d", fa+i);
  73. ans = 0;
  74. for(int i = 1; i <= n; ++i) // remove
  75. if(pa[i] != fa[i]) dfs(i);
  76. for(int i = 1; i <= n; ++i){ // unit
  77. if(pa[i] == fa[i]) continue;
  78. ++ans; dfs(fa[i]);
  79. }
  80. printf("%d\n", ans);
  81. }
  82. return 0;
  83. }

  

UVALive 7752 Free Figurines (瞎搞)的更多相关文章

  1. UVALive - 7752 Free Figurines

    题意:有n个娃娃,如果大娃娃j直接套小娃娃i,则fa[i] = j.若fa[i] = 0,则该娃娃自由.给出每个娃娃初始的父亲,和改变后的父亲,在满足以下合法操作的条件下,问最少需要多少次变换. 1. ...

  2. URAL 1203. Scientific Conference(瞎搞)

    题目链接 本来觉得这不是经典的贪心吗..果断水一次,wa了,看了看discuss,发现貌似不好水,土土的DP了一下,复杂度很高了,又T了...然后想想单调队列,二分什么的...不好往上加,直接搞了标记 ...

  3. Codeforces Gym 100610 Problem H. Horrible Truth 瞎搞

    Problem H. Horrible Truth Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1006 ...

  4. B. Salty Fish Go! -期望题(瞎搞题)

    链接:https://www.nowcoder.com/acm/contest/104/B来源:牛客网 题意:A few days ago, WRD was playing a small game ...

  5. HDU5532 Almost Sorted Array(最长上升子序列 or 瞎搞个做差的数组)

    题目链接:点我 题意:给定一个序列,询问是否能删除一个数让它成为非递减或者非递增的序列. 比如说 删除后的序列是1 3 3 5 或者5 3 3 1 或者1 3 5 或者5 3 1 都可以.只要满足删掉 ...

  6. TOJ3097: 单词后缀 (字典树 or map瞎搞)

    传送门 (<---可以点击的~) 时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte 描述 有些英语单词后缀都是一样的,现在我们需要从给定的一堆单词里 ...

  7. 8VC Venture Cup 2016 - Elimination Round B. Cards 瞎搞

    B. Cards 题目连接: http://www.codeforces.com/contest/626/problem/B Description Catherine has a deck of n ...

  8. ubuntu--基础环境瞎搞集合

    安装ubuntu系统后有很多东西需要自己瞎搞一下,这里把一些瞎搞的过程记录在这里,方便以后重新装系统后重新配置. 一.安装. 可以在windows下制作启动盘(软碟通),然后开机u盘启动即可安装,预留 ...

  9. Codeforces631C【栈维护+瞎搞】

    题意: 百度. 思路: 如果该查询的R比前面的所有都大,那么前面所有都失效. 那么我先预处理出这些有效的. 那最坏的情况不就是栈里面元素(R)很多 n,n-1,n-2,n-3,n-4而且都是相反排序的 ...

随机推荐

  1. 第七章 伪分布式安装hive,sqoop

    第一部分:先讲这么去安装hive.先去hive官网下载,我这里以hive-0.12.0为例子. 前面第二章讲了安装hadoop,hbase实例,我们继续讲这么安装hive,先说下hive配置文件 一, ...

  2. [转]第一次使用Android Studio时你应该知道的一切配置(三):gradle项目构建

    目录: 1.gradle的概念 2.gradle配置jar包,和libs文件夹导入jar包的区别 3.签名打包: (1)Studio (2)命令行 (3)gradle wrapper的原理 4.Bui ...

  3. ALSA声卡07_分析调用过程_学习笔记

    1.编译新的strace工具分析aplay和amixer应用程序对声卡的调用过程 (1)因为旧的strace工具不能识别不能识别alsa声卡驱动程序里面的ioctrl. (2)编译过程参考http:/ ...

  4. Druid.io系列(四):索引过程分析

    原文链接: https://blog.csdn.net/njpjsoftdev/article/details/52956083 Druid底层不保存原始数据,而是借鉴了Apache Lucene.A ...

  5. HTTPS安全超文本传输协议

    一.什么是HTTPS 简单的理解HTTPS就是使用SSL/TLS加密内容的.安全的HTTP协议 HTTPS = HTTP + SSL/TLS 二.对称加密与非对称加密 对称加密:加密和解密使用同一密钥 ...

  6. Java线程状态切换以及核心方法

    1.Java线程状态 1.1 线程主要状态 ①初始(NEW):新创建了一个线程对象,但还没有调用start()方法.②运行(RUNNABLE):Java线程中将就绪(ready)和运行中(runnin ...

  7. quartz报错 Couldn't retrieve job because the BLOB couldn't be deserialized: null

    今天线上添加定时任务之后 定时任务查询页面报出如上错误, 原因有两点 1.org.quartz.jobStore.useProperties = true 这个属性的意思存储的JobDataMaps是 ...

  8. Linux配置Oracle 11g自动启动

    http://www.cnblogs.com/edwardcmh/archive/2012/05/11/2495671.html 安装完毕Oracle 11g每次都得手动启动 | 停止数据库(dbst ...

  9. C#中 标识符“XXX”不符合 CLS

    标识符“XXX”不符合 CLS,意思是只要是不与外面有接口,比如在私有函数中操作,可是使用一些不符合cls的类型,但是如果是公共的,就必须要符合这个规范. 解决方法是,将这个类中的这些public类型 ...

  10. 跟我学算法-tensorflow 实现线性拟合

    TensorFlow™ 是一个开放源代码软件库,用于进行高性能数值计算.借助其灵活的架构,用户可以轻松地将计算工作部署到多种平台(CPU.GPU.TPU)和设备(桌面设备.服务器集群.移动设备.边缘设 ...