UVALive 7752 Free Figurines (瞎搞)
题意:给定 n 个盒子,然后告诉你每个盒子在哪个盒子里,数值越大,盒子越大,给定你初态,和末态,问你最少要几步能完成,只有两种操作,一种是把一个盒子连同里面的小盒子放到一个空盒子里,另一种是把一个堆盒子里的最外面的那个盒子拿出来。
析:首先,先遍历一次,如果初态和不一样,那么初态后面的要全部拿出来,然后再遍历一次,然后如果发现不一样,然后要看把末态的父结点是不是孤立的,如果不是,也要全部拿出。
代码如下:
- #pragma comment(linker, "/STACK:1024000000,1024000000")
- #include <cstdio>
- #include <string>
- #include <cstdlib>
- #include <cmath>
- #include <iostream>
- #include <cstring>
- #include <set>
- #include <queue>
- #include <algorithm>
- #include <vector>
- #include <map>
- #include <cctype>
- #include <cmath>
- #include <stack>
- #include <sstream>
- #include <list>
- #include <assert.h>
- #include <bitset>
- #define debug() puts("++++");
- #define gcd(a, b) __gcd(a, b)
- #define lson l,m,rt<<1
- #define rson m+1,r,rt<<1|1
- #define fi first
- #define se second
- #define pb push_back
- #define sqr(x) ((x)*(x))
- #define ms(a,b) memset(a, b, sizeof a)
- //#define sz size()
- #define pu push_up
- #define pd push_down
- #define cl clear()
- #define all 1,n,1
- #define FOR(x,n) for(int i = (x); i < (n); ++i)
- #define freopenr freopen("in.txt", "r", stdin)
- #define freopenw freopen("out.txt", "w", stdout)
- using namespace std;
- typedef long long LL;
- typedef unsigned long long ULL;
- typedef pair<int, int> P;
- const int INF = 0x3f3f3f3f;
- const LL LNF = 1e15;
- const double inf = 1e20;
- const double PI = acos(-1.0);
- const double eps = 1e-8;
- const int maxn = 1e5 + 100;
- const int mod = 7;
- const int dr[] = {-1, 0, 1, 0};
- const int dc[] = {0, 1, 0, -1};
- const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
- int n, m;
- const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- inline bool is_in(int r, int c) {
- return r >= 0 && r < n && c >= 0 && c < m;
- }
- int fa[maxn];
- int pa[maxn];
- int ans;
- void dfs(int x){
- if(pa[x] == 0) return ;
- dfs(pa[x]); pa[x] = 0;
- ++ans;
- }
- int main(){
- while(scanf("%d", &n) == 1){
- for(int i = 1; i <= n; ++i) scanf("%d", pa+i);
- for(int i = 1; i <= n; ++i) scanf("%d", fa+i);
- ans = 0;
- for(int i = 1; i <= n; ++i) // remove
- if(pa[i] != fa[i]) dfs(i);
- for(int i = 1; i <= n; ++i){ // unit
- if(pa[i] == fa[i]) continue;
- ++ans; dfs(fa[i]);
- }
- printf("%d\n", ans);
- }
- return 0;
- }
UVALive 7752 Free Figurines (瞎搞)的更多相关文章
- UVALive - 7752 Free Figurines
题意:有n个娃娃,如果大娃娃j直接套小娃娃i,则fa[i] = j.若fa[i] = 0,则该娃娃自由.给出每个娃娃初始的父亲,和改变后的父亲,在满足以下合法操作的条件下,问最少需要多少次变换. 1. ...
- URAL 1203. Scientific Conference(瞎搞)
题目链接 本来觉得这不是经典的贪心吗..果断水一次,wa了,看了看discuss,发现貌似不好水,土土的DP了一下,复杂度很高了,又T了...然后想想单调队列,二分什么的...不好往上加,直接搞了标记 ...
- Codeforces Gym 100610 Problem H. Horrible Truth 瞎搞
Problem H. Horrible Truth Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1006 ...
- B. Salty Fish Go! -期望题(瞎搞题)
链接:https://www.nowcoder.com/acm/contest/104/B来源:牛客网 题意:A few days ago, WRD was playing a small game ...
- HDU5532 Almost Sorted Array(最长上升子序列 or 瞎搞个做差的数组)
题目链接:点我 题意:给定一个序列,询问是否能删除一个数让它成为非递减或者非递增的序列. 比如说 删除后的序列是1 3 3 5 或者5 3 3 1 或者1 3 5 或者5 3 1 都可以.只要满足删掉 ...
- TOJ3097: 单词后缀 (字典树 or map瞎搞)
传送门 (<---可以点击的~) 时间限制(普通/Java):1000MS/3000MS 内存限制:65536KByte 描述 有些英语单词后缀都是一样的,现在我们需要从给定的一堆单词里 ...
- 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 ...
- ubuntu--基础环境瞎搞集合
安装ubuntu系统后有很多东西需要自己瞎搞一下,这里把一些瞎搞的过程记录在这里,方便以后重新装系统后重新配置. 一.安装. 可以在windows下制作启动盘(软碟通),然后开机u盘启动即可安装,预留 ...
- Codeforces631C【栈维护+瞎搞】
题意: 百度. 思路: 如果该查询的R比前面的所有都大,那么前面所有都失效. 那么我先预处理出这些有效的. 那最坏的情况不就是栈里面元素(R)很多 n,n-1,n-2,n-3,n-4而且都是相反排序的 ...
随机推荐
- 第七章 伪分布式安装hive,sqoop
第一部分:先讲这么去安装hive.先去hive官网下载,我这里以hive-0.12.0为例子. 前面第二章讲了安装hadoop,hbase实例,我们继续讲这么安装hive,先说下hive配置文件 一, ...
- [转]第一次使用Android Studio时你应该知道的一切配置(三):gradle项目构建
目录: 1.gradle的概念 2.gradle配置jar包,和libs文件夹导入jar包的区别 3.签名打包: (1)Studio (2)命令行 (3)gradle wrapper的原理 4.Bui ...
- ALSA声卡07_分析调用过程_学习笔记
1.编译新的strace工具分析aplay和amixer应用程序对声卡的调用过程 (1)因为旧的strace工具不能识别不能识别alsa声卡驱动程序里面的ioctrl. (2)编译过程参考http:/ ...
- Druid.io系列(四):索引过程分析
原文链接: https://blog.csdn.net/njpjsoftdev/article/details/52956083 Druid底层不保存原始数据,而是借鉴了Apache Lucene.A ...
- HTTPS安全超文本传输协议
一.什么是HTTPS 简单的理解HTTPS就是使用SSL/TLS加密内容的.安全的HTTP协议 HTTPS = HTTP + SSL/TLS 二.对称加密与非对称加密 对称加密:加密和解密使用同一密钥 ...
- Java线程状态切换以及核心方法
1.Java线程状态 1.1 线程主要状态 ①初始(NEW):新创建了一个线程对象,但还没有调用start()方法.②运行(RUNNABLE):Java线程中将就绪(ready)和运行中(runnin ...
- quartz报错 Couldn't retrieve job because the BLOB couldn't be deserialized: null
今天线上添加定时任务之后 定时任务查询页面报出如上错误, 原因有两点 1.org.quartz.jobStore.useProperties = true 这个属性的意思存储的JobDataMaps是 ...
- Linux配置Oracle 11g自动启动
http://www.cnblogs.com/edwardcmh/archive/2012/05/11/2495671.html 安装完毕Oracle 11g每次都得手动启动 | 停止数据库(dbst ...
- C#中 标识符“XXX”不符合 CLS
标识符“XXX”不符合 CLS,意思是只要是不与外面有接口,比如在私有函数中操作,可是使用一些不符合cls的类型,但是如果是公共的,就必须要符合这个规范. 解决方法是,将这个类中的这些public类型 ...
- 跟我学算法-tensorflow 实现线性拟合
TensorFlow™ 是一个开放源代码软件库,用于进行高性能数值计算.借助其灵活的架构,用户可以轻松地将计算工作部署到多种平台(CPU.GPU.TPU)和设备(桌面设备.服务器集群.移动设备.边缘设 ...