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

【题意】

在这里输入题意

【题解】

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

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

从底往上递归处理。

考虑以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. [Node.js] Manage Configuration Values with Environment Variables

    Storing configuration in files instead of the environment has many downsides, including mistakenly c ...

  2. NYOJ 145 聪明的小珂

    /* 题目大意:求解和输入数的互质的数 解题思路:求解和 n 互质的最大数.从n/2開始找 关键点:GCD函数的使用 解题人:lingnichong 解题时间:2014-10-04 16:11:55 ...

  3. leetCode(32):Power of Two

    Given an integer, write a function to determine if it is a power of two. 2的幂的二进制表示中,必定仅仅有一个"1&q ...

  4. Log使用

    学习参考:http://blog.csdn.net/hu_shengyang/article/details/6754031 log4j三种主要组件: logger记录对象 appender输出对象 ...

  5. tcMalloc 配置和优化 nginx 高性能

    tcMalloc优化nginx  记住:nginx一定要先启动 1>下载安装libunwind: #wget  http://download.savannah.gnu.org/releases ...

  6. IPK僵尸网络 看看其传播手法

    转自:http://www.freebuf.com/vuls/154975.html 一.IPK僵尸网络概述 IPK僵尸家族是自2012年底就开始出现并长期持续活跃在境外的DDoS僵尸网络.2016年 ...

  7. 数据库 The Network Adapter could not establish the connection解决方案

    连接数据库 注意 url ip地址换的时候 oracle 里的listener.ora thnsnames.ora也要随之变化 重启数据库 不然可能会报出 java.sql.SQLException: ...

  8. tomcat开启https服务

    一.创建证书 证书是单点登录认证系统中很重要的一把钥匙,客户端于服务器的交互安全靠的就是证书:本教程由于是演示所以就自己用JDK自带的keytool工具生成证书:如果以后真正在产品环境中使用肯定要去证 ...

  9. LinrFont UWP 字体预览工具下载

    Windows 10 用户 购买 https://www.microsoft.com/zh-cn/p/linrfont/9nkh5mlvt819

  10. 自定义view 之多个引导层动画效果

    SupernatantView 如果我英文还可以的话这个应该叫做漂浮在上层的view---引导层 今天闲来无事看了网上的一些引导层案例总感觉如果不是很舒服,就是类似于很死板的显示和消失 我在想能不能弄 ...