Gym - 101620H_Hidden Hierarchy(树+模拟)

Hidden Hierarchy

题目链接

题目描述

You are working on the user interface for a simple text-based file explorer. One of your tasks is to build a navigation pane displaying the directory hierarchy. As usual, the filesystem consists of directories which may contain files and other directories, which may, in turn, again contain files and other directories etc. Hence, the directories form a hierarchical tree structure. The top-most directory in the hierarchy is called the root directory. If directory d directly contains directory e we will say that d is the parent directory of e while e is a subdirectory od d. Each file has a size expressed in bytes. The directory size is simply the total size of all files directly or indirectly contained inside that directory.
All files and all directories except the root directory have a name — a string that always starts with a letter and consists of only lowercase letters and “.” (dot) characters. All items (files and directories) directly inside the same parent directory must have unique names. Each item (file and directory) can be uniquely described by its path — a string built according to the following rules:

  • Path of the root directory is simply “/” (forward slash).
  • For a directory d, its path is obtained by concatenating the directory names top to bottom along the hierarchy from the root directory to d, preceding each name with the “/” character and placing another “/” character at the end of the path.
  • For a file f , its path is the concatenation of the parent directory path and the name of file f .
    We display the directory hierarchy by printing the root directory. We print a directory d by outputting a line of the form “md pd sd” where pd and sd are the path and size of directory d respectively, while md is its expansion marker explained shortly. If d contains other directories we must choose either to collapse it or to expand it. If we choose to expand d we print (using the same rules) all of its subdirectories in lexicographical order by name. If we choose to collapse directory d, we simply ignore its contents.
    The expansion marker md is a single blank character when d does not have any subdirectories, “+” (plus) character when we choose to collapse d or a “-” (minus) character when we choose expand d.
    Given a list of files in the filesystem and a threshold integer t, display the directory hierarchy ensuring that each directory of size at least t is printed. Additionally, the total number of directories printed should be minimal. Assume there are no empty directories in the filesystem — the entire hierarchy can be deduced from the provided file paths. Note that the root directory has to be printed regardless of its size. Also note that a directory of size at least t only has to be printed, but not necessarily expanded.

输入

The ?rst line contains an integer n (1 ≤ n ≤ 1 000) — the number of ?les. Each of the following n lines contains a string f and an integer s (1 ≤ s ≤ 1e6) — the path and the size of a single ?le. Each path is at most 100 characters long and is a valid ?le path according to the rules above. All paths will be different.
The following line contains an integer t (1 ≤ t ≤ 1e9) — the threshold directory size.

输出

Output the minimal display of the ?lesystem hierarchy for the given threshold as described above.

样例

input
9
/sys/kernel/notes 100
/cerc/problems/a/testdata/in 1000000
/cerc/problems/a/testdata/out 8
/cerc/problems/a/luka.cc 500
/cerc/problems/a/zuza.cc 5000
/cerc/problems/b/testdata/in 15
/cerc/problems/b/testdata/out 4
/cerc/problems/b/kale.cc 100
/cerc/documents/rules.pdf 4000
10000
output
- / 1009727
- /cerc/ 1009627
  /cerc/documents/ 4000
- /cerc/problems/ 1005627
- /cerc/problems/a/ 1005508
  /cerc/problems/a/testdata/ 1000008
+ /cerc/problems/b/ 119
+ /sys/ 10
input
8
/b/test/in.a 100
/b/test/in.b 1
/c/test/in.a 100
/c/test/in.b 1
/c/test/pic/in.a.svg 10
/c/test/pic/in.b.svg 10
/a/test/in.a 99
/a/test/in.b 1
101
output
- / 322
+ /a/ 100
- /b/ 101
  /b/test/ 101
- /c/ 121
+ /c/test/ 121
input
2
/a/a/a 100
/b.txt 99
200
output
+ / 199

题意

这个题目超级长,废话也超级多。大概就是做一个目录。
有n条路径,每条路径下有一个大小为val的文件。
根文件为“/”;
给一个t;
如果目录下文件大小有一个大于等于t,展开“-”;
如果都小于t,折叠“+”;
如果没有文件,则输出一个“ ”。
注意:输出路径按字典序排序。

思路

首先把每个文件路径及其大小读入,按字典序排序。
然后把路径分开,建树。
根据题目要求输出就可以了。
有几个点注意:
内存问题,刚开始用数组存储,结果导致RE跟MLE二选一,后来改成了vector。
输出问题,这里需要采用递归输出。
剩下的就是疯狂模拟就可以了。

等下附上代码。

原文地址:https://www.cnblogs.com/luoxiaoyi/p/9715363.html

时间: 2024-10-18 21:22:11

Gym - 101620H_Hidden Hierarchy(树+模拟)的相关文章

hdu_5818_Joint Stacks(线段树模拟)

题目链接:hdu_5818_Joint Stacks 题意: 给你两个栈,多了个合并操作,然后让你模拟 题解: 很容易想到O(1)的单个栈操作,O(n)的合并操作,这样肯定超时,所以我们要将时间复杂度均摊一下,让每个操作都是logn的,于是用上了线段树模拟. 线段树考虑染色,线段树的区间代表的是操作编号,0代表为A栈,1代表为B栈,每次merge 就把1到i这个区间染成指定颜色,然后pop就在线段树中找最右边的对应颜色的点.这样每次操作都是logn 不过官方题解给的是O(n)的解法,反正我是没想

POJ 2886 Who Gets the Most Candies?(线段树模拟约瑟夫环,高合成数)

POJ 2886 Who Gets the Most Candies?(线段树模拟约瑟夫环,高合成数) ACM 题目地址:POJ 2886 Who Gets the Most Candies? 题意: N 个小孩围成一圈,他们被顺时针编号为 1 到 N.每个小孩手中有一个卡片,上面有一个非 0 的数字,游戏从第 K 个小孩开始,他告诉其他小孩他卡片上的数字并离开这个圈,他卡片上的数字 A 表明了下一个离开的小孩,如果 A 是大于 0 的,则下个离开的是左手边第 A 个,如果是小于 0 的, 则是

POJ-2886 Who Gets the Most Candies?(线段树+模拟)

题目大意:n个小孩按顺时针站成一圈,每次会有一个小孩出队(第一个出队的小孩已知),在他出队时会指定下一个出队的小孩,直到所有的小孩全部出队游戏结束.第p个出队的小孩会得到f(p)个糖果,f(p)为p的正约数个数.问获得最多糖果的小孩是谁?并求出他获得的糖果数.如果有多解,只输出最先出队的那个小孩. 题目分析:先将1~n之内的具有最多约数个数并且最小的那个数打出来,然后模拟相应的次数操作即可.模拟时利用到线段树,用线段树维护区间中有多少个小孩还没有出队,每次出队一个小孩就将他在树中删除,最后利用线

【BZOJ4523】[Cqoi2016]路由表 Trie树模拟

[BZOJ4523][Cqoi2016]路由表 Description 路由表查找是路由器在转发IP报文时的重要环节.通常路由表中的表项由目的地址.掩码.下一跳(Next Hop)地址和其他辅助信息组成.例如: 当路由器收到一个IP报文时,会将报文中的目的IP地址与路由表中的表项逐条进行比较,选择匹配且最明确的表项,将报文转发给该表项中指定的下一跳. 匹配的过程是将报文中的目的地址和表项中的目的地址分别转为二进制串,再查看表项中的掩码长度,若掩码长度为x,则将两个二进制串的前x位进行比较,如果相

K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)

题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子树上的所有节点的权值的乘积x,及x的因数个数. 2.SEED:将节点u的权值乘以x. 思路: 比赛时少看了因数不大于13这句话,然后本题难度增加数倍,肝了两个小时都没肝出来,对不起队友啊,今天的组队训练赛实力背锅…… 这题一眼线段树,由于是对一棵子树进行处理,因此我们采用常规套路,借助dfs序将子树

A .Gaby And Addition (Gym - 101466A + 字典树)

题目链接:http://codeforces.com/gym/101466/problem/A 题目: 题意: 给你n个数,重定义两个数之间的加法不进位,求这些数中两个数相加的最大值和最小值. 思路: 字典树.我们首先将前i-1为放入字典树中,然后在查询第i位时,我们去字典树中查询,对每一位进行寻找,找到满足题意的当前位的最大值和最小值,然后继续更新下一位,最后维护总的最大值和最小值即可. 代码实现如下: 1 #include <set> 2 #include <map> 3 #i

Gym 100803G 线段树

好长时间前做的题,来补一下题解. 给出括号化的序列,每次改变一个括号方向,求出下标p,是的改变p处的括号方向可以使括号化仍然成立,且p最小. 保证括号化看似和线段树没有联系,我们可以把括号"("表示为1,把括号")"表示为-1,则保证括号化的充要条件就是使数字序列前缀和始终大于等于零 用线段树维护前缀和 查询有两种情况 1. "(" 变成 ")" 这种情况我们只需找到最左边的一个")",就是答案.(可以用一

Gym 100625C 密文匹配-模拟题-(map)

题意:已知n个明文和一个密文,推出可能的匹配关系,然后输出字符串ss的密文. 分析: 一个模拟题,当时想偏了,还想着要同一字母可能在任意位置,然后要记录每个字母的位置,找密文的相应位置必须是同一字母,balabala的,不知道什么鬼. 其实就是简单的对应关系,不用管位置啥的,只管同一字母对应的密文是一样的就行了.26个字母,枚举一遍就是了.对应关系匹配啥的用map是最好不过的了.小tirck是,如果已知25个字母,那么剩下的一个也就知道了. 代码: #include<iostream> #in

Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 线段树模拟

E. Correct Bracket Sequence Editor Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical express