【链接】 我是链接,点我呀:)

【题意】

在这里输入题意

【题解】

先建立一棵字典树。
显然,某一些节点上会被打上标记。
问题就转化成求所有标记的深度的和的最小值了。
(标记可以上移,但是不能在同一位置

则我们用树形动规的方法。

从底往上递归处理。

考虑以x为根的一棵子树。

如果这个节点被打上了标记。

那么就直接将答案累加上这个节点的深度。

如果没有打上标记。

那么就把这个子树下面某个深度最高的点移动到这个位置上来。

显然这样贪心做是最优的。

用multiset维护某个子树下面的深度最大值。

然后用启发式合并合并multiset就好。

O(能过)

【代码】

#include <bits/stdc++.h>
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define all(x) x.begin(),x.end()
#define pb push_back
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std; const double pi = acos(-1);
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};
const int N = 1e5; int n,tot=1,root=1,ch[N+10][26+5],tag[N+10],index[N+10];
multiset<int> myset[N+10];
LL ans = 0;
string s; void ins(string s){
int p = root;
for (int i = 0;i < (int)s.size();i++){
if (!ch[p][s[i]-'a']) ch[p][s[i]-'a'] = ++tot;
p = ch[p][s[i]-'a'];
}
tag[p] = 1;
} void dfs(int x,int dep){
for (int i = 0;i < 26;i++)
if (ch[x][i]){
dfs(ch[x][i],dep+1);
}
if (x==root) return; index[x] = x;
for (int i = 0;i < 26;i++)
if (ch[x][i]){
if((int)myset[index[ch[x][i]]].size()>(int)myset[index[x]].size())
swap(index[ch[x][i]],index[x]);
for (int y:myset[index[ch[x][i]]]) myset[index[x]].insert(y);
} if (tag[x]){
ans+=dep;
myset[index[x]].insert(dep);
}else
if (!myset[index[x]].empty()){
auto temp = myset[index[x]].end();temp--;
int bottomdep = *(temp);
myset[index[x]].erase(temp);
ans-=bottomdep-dep;
myset[index[x]].insert(dep);
}
} int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
cin >> n;
rep1(i,1,n){
cin >> s;
ins(s);
}
dfs(root,0);
cout<<ans<<endl;
return 0;
}

【Codeforces Round #476 (Div. 2) [Thanks, Telegram!] E】Short Code的更多相关文章

  1. 【Codeforces Round #476 (Div. 2) [Thanks, Telegram!] C】Greedy Arkady

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举那个人收到了几次糖i. 最好的情况显然是其他人都只收到i-1次糖. 然后这个人刚好多收了一次糖 也即 (i-1)kx + x & ...

  2. 【Codeforces Round #476 (Div. 2) [Thanks, Telegram!] D】Single-use Stones

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 设长度为L的所有区间里面,石头的个数的最小值为k 设取到k的区间为l,r 那么k就为最多能通过的青蛙个数. 假设k再大一点.比如为k ...

  3. 【Codeforces Round #476 (Div. 2) [Thanks, Telegram!] A】Paper Airplanes

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 统计每个人需要的sheet个数. 乘上k 然后除p就是需要的pack个数了 [代码] #include <bits/stdc+ ...

  4. 【Codeforces Round #476 (Div. 2) [Thanks, Telegram!] B】Battleship

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 暴力枚举船的左上角. 然后统计每个点被覆盖次数就好. [代码] #include <bits/stdc++.h> #de ...

  5. Codeforces Round #476 (Div. 2) [Thanks, Telegram!] ABCDE

    修仙场,没脑子,C边界判错一直在写mdzz..D根本没怎么想. 第二天起来想了想D这不是水题吗立马A了.看了看E一开始想分配问题后来发觉想岔了,只需要每次都把树最后分配的点移到最前面就好了. A. P ...

  6. Codeforces Round #476 (Div. 2) [Thanks, Telegram!] C

    http://codeforces.com/contest/965/problem/C 题目大意:n个糖,k个人,每次最多只能拿M个糖,从第一个人开始拿,可以循环D次.问Arkady最多可以拿几块糖? ...

  7. 【Codeforces Round #431 (Div. 1) D.Shake It!】

    ·最小割和组合数放在了一起,产生了这道题目. 英文题,述大意:     一张初始化为仅有一个起点0,一个终点1和一条边的图.输入n,m表示n次操作(1<=n,m<=50),每次操作是任选一 ...

  8. 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes

    [题目链接]:http://codeforces.com/contest/821/problem/C [题意] 给你2*n个操作; 包括把1..n中的某一个数压入栈顶,以及把栈顶元素弹出; 保证压入和 ...

  9. 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees

    [题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...

随机推荐

  1. selenium+java处理鼠标悬停

    1.元素比较明确(可视) 2.元素隐藏,需要鼠标移动到一定地方才显现(下图为某论坛列表,需要将鼠标移动到列表才显示操作按钮)

  2. Struts2校验

    struts2校验有两种实现方法: 手工编写代码实现(基本验证) //login.jsp <font color="red"><s:fielderror/> ...

  3. php 将多个txt文件合并成

    function test() { $hostdir= iconv("utf-8","gbk","C:\Users\原万里\Desktop\日常笔记& ...

  4. ZOJ 3203

    很简单的一题,注意墙上的影子是放大就行.用三分. #include <iostream> #include <cstdio> #include <cstring> ...

  5. bram和dram差别

    选择distributed memory generator和block memorygenerator标准: Dram和bram差别: 1.bram 的输出须要时钟,dram在给出地址后既可输出数据 ...

  6. SSH学习之中的一个 OpenSSH基本使用

    在Linux系统中.OpenSSH是眼下最流行的远程系统登录与文件传输应用,也是传统Telenet.FTP和R系列等网络应用的换代产品. 当中,ssh(Secure Shell)能够替代telnet. ...

  7. 最全Pycharm教程(38)——Pycharm版本号控制之远程共享

    1.主题 介绍怎样通过GitHub共享你的本地Git版本号库 2.准备工作 (1)Pycharm版本号为2.7或者更高 (2)Git以及GitHub可用 (3)有GitHub storage的读写权限 ...

  8. 怎样在同一台电脑使用不同的账号提交到同一个github仓库

    近期这段时间使用github.有时在公司办公,想要用git提交代码到自己的github仓库,提交是显示的作者是自己在公司的账户.而不是自己的github账户.这就相当于提交到github的代码不是自己 ...

  9. oracle (9I/10G/11G)数据库日志挖掘(审计误操作)

    文档结构: 资料来自官方网站: https://docs.oracle.com/cd/E11882_01/server.112/e22490/logminer.htm#SUTIL019 来自论坛: h ...

  10. iris中间件

    最近使用golang写的时候涉及到权限校验,用中间件(使用iris框架内的东西) 自己摸索出一种自己的方式 iris.UseFunc(MiddlewareFunc)使用这个方法,会在所有的请求之前执行 ...