CCF 202012-3 带配额的文件系统
思路
关于机器学习中使用服务器训练模型时显存消耗过大问题,给每个人限额空间。
给根目录,子目录,文件限制存储空间,执行一些列操作
代码实现
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
typedef long long LL;
const int N = 2000010;
int n;
struct Node
{
string name;
int id, type; // type == 0: 文件夹;type == 1:文件
mutable LL ld, lr;
mutable LL sd, sr;
bool operator< (const Node& t) const
{
return name < t.name;
}
};
set<Node> son[N];
int idx;
char str[N];
bool F;
int U;
LL SZ;
vector<string> get(char* str)
{
vector<string> res(1, "root");
for (int i = 0; str[i]; i ++ )
{
if (str[i] == '/') continue;
string s;
int j = i;
while (str[j] && str[j] != '/') s += str[j ++ ];
res.push_back(s);
i = j - 1;
}
return res;
}
LL dfs_remove(vector<string>& t, int u, int p)
{
string name = t[u];
if (!son[p].count({
name})) return -1;
auto it = son[p].find({
name});
if (u == t.size() - 1)
{
if (it->type) t[u] = "#file";
auto sz = it->sr;
son[p].erase(it);
return sz;
}
if (it->type) return -1;
auto sz = dfs_remove(t, u + 1, it->id);
if (sz >= 0)
{
it->sr -= sz;
if (t[u + 1] == "#file") it->sd -= sz;
}
return sz;
}
bool dfs_create(vector<string>& t, int u, int p, LL sz)
{
string name = t[u];
if (u == t.size() - 1)
{
if (son[p].count({
name}))
{
auto it = son[p].find({
name});
if (it->type == 0) return false;
SZ = dfs_remove(t, 0, 0);
Node cur{
name, ++ idx, 1, 0, 0, 0, sz};
son[p].insert(cur);
return true;
}
else
{
Node cur{
name, ++ idx, 1, 0, 0, 0, sz};
son[p].insert(cur);
return true;
}
}
else
{
if (!son[p].count({
name}))
{
if (U == -1) U = u;
Node cur{
name, ++ idx, 0, 0, 0, 0, 0};
son[p].insert(cur);
}
auto it = son[p].find({
name});
if (it->type) return false;
auto res = dfs_create(t, u + 1, it->id, sz);
if (res)
{
it->sr += sz;
if (u