B. Connecting Universities

大意: 给定树, 给定2*k个点, 求将2*k个点两两匹配, 每个匹配的贡献为两点的距离, 求贡献最大值

单独考虑每条边$(u,v)$的贡献即可, 最大贡献显然是左右两侧点的最小值.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 1e6+10;
int n, k;
int a[N], sz[N];
vector<int> g[N];
ll ans; void dfs(int x, int fa) {
sz[x] = a[x];
for (int y:g[x]) if (y!=fa) {
dfs(y,x), sz[x]+=sz[y];
ans += min(sz[y], k-sz[y]);
}
} int main() {
scanf("%d%d", &n, &k),k*=2;
REP(i,1,k) {
int t;
scanf("%d", &t);
a[t] = 1;
}
REP(i,2,n) {
int u, v;
scanf("%d%d", &u, &v);
g[u].pb(v),g[v].pb(u);
}
dfs(1,0);
printf("%lld\n", ans);
}

C. Break Up

大意: 无向有权图有重边自环, 求删除两条边使得s与t不连通, 且两条边的边权和最小.

先求出任意一条最短路径, 边数显然不超过$n$, 暴力枚举这$n$条边然后再tarjan即可, 复杂度O(n(m+n))

算是挺简单的了, 还是打了好久, 一直卡在怎么判断删除一条边后是否连通, 后来发现tarjan后从s->t经过的桥一定是一条链, 所以直接dfs就好了, 最后还要注意边权1e9+1e9爆掉0x3f3f3f3f了.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = ~0u>>1;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 3e4+10;
int n, m, S, T;
int w[N];
struct _ {int to,id;} fa[N];
vector<_> g[N];
int dfn[N], low[N], isbridge[N], clk;
void tarjan(int x, int fa, int z) {
dfn[x]=low[x]=++clk;
for (auto &&e:g[x]) if (e.id!=z) {
int y = e.to, id = e.id;
if (!dfn[y]) {
tarjan(y,id,z);
low[x]=min(low[x],low[y]);
if (low[e.to]>dfn[x]) isbridge[id]=1;
} else if (dfn[y]<dfn[x]&&id!=fa) {
low[x]=min(low[x],dfn[y]);
}
}
}
int vis[N], c[N];
int dfs(int x) {
if (x==T) return 1;
for (auto e:g[x]) if (!vis[e.id]) {
vis[e.id] = 1;
if (dfs(e.to)) return c[e.id] = 1;
}
return 0;
} int main() {
scanf("%d%d%d%d", &n, &m, &S, &T);
REP(i,1,m) {
int u, v;
scanf("%d%d%d", &u, &v, w+i);
g[u].pb({v,i}), g[v].pb({u,i});
}
queue<int> q;
fa[S].to=-1, q.push(S);
while (q.size()) {
int x = q.front(); q.pop();
for (auto &&e:g[x]) if (!fa[e.to].to) {
fa[e.to]={x,e.id}, q.push(e.to);
}
}
if (!fa[T].to) return puts("0\n0"),0;
int ans = INF;
vector<int> vec;
for (int x=T; x!=S; x=fa[x].to) {
int id = fa[x].id;
memset(vis,0,sizeof vis);
memset(c,0,sizeof c);
vis[id] = 1;
if (!dfs(S)) {
if (ans>w[id]) ans = w[id],vec.clear(),vec.pb(id);
continue;
}
memset(dfn,0,sizeof dfn);
memset(isbridge,0,sizeof isbridge);
clk = 0;
tarjan(S,0,id);
REP(i,1,m) if (c[i]&&isbridge[i]&&ans>w[id]+w[i]) {
ans=w[id]+w[i];
vec.clear();
vec.pb(id), vec.pb(i);
}
}
if (ans==INF) return puts("-1"),0;
printf("%d\n%d\n", ans, int(vec.size()));
for (int t:vec) printf("%d ", t); hr;
}

D. Huffman Coding on Segment

莫队一下, 然后将出现次数小于等于$\sqrt{n}$的暴力合, 其余的用堆合, 复杂度$O(m\sqrt{n}logn)$, 看了下最优解, 好像可以排序一下省去堆从而优化掉一个log

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n, m, sqn;
int blo[N], cnt[N], sum[N], s[N], a[N];
struct _ {
int l,r,id;
bool operator < (const _ & rhs) const {
return blo[l]^blo[rhs.l]?l<rhs.l:blo[l]&1?r<rhs.r:r>rhs.r;
}
} e[N];
ll ans[N];
vector<int> q; void upd(int x, int d) {
--sum[cnt[x]];
cnt[x]+=d;
++sum[cnt[x]];
} ll calc() {
ll ans = 0;
REP(i,1,sqn) s[i] = sum[i];
priority_queue<int,vector<int>,greater<int> > Q;
int pre = 0;
REP(i,1,sqn) if (s[i]) {
if (pre) {
int x = pre+i;
ans += x;
if (x>sqn) Q.push(x);
else ++s[x];
--s[i], pre = 0;
}
if (s[i]&1) --s[i], pre = i;
ans += s[i]*i;
if (i*2<=sqn) s[i*2]+=s[i]/2;
else {
REP(j,1,s[i]/2) Q.push(i*2);
}
}
if (pre) Q.push(pre);
for (auto i:q) if (cnt[i]>sqn) Q.push(cnt[i]);
while (Q.size()>1) {
int x = Q.top(); Q.pop();
x += Q.top(); Q.pop();
ans += x, Q.push(x);
}
return ans;
} int main() {
scanf("%d", &n), sqn = sqrt(n);
REP(i,1,n) scanf("%d",a+i),++cnt[a[i]],blo[i]=i/sqn;
REP(i,1,N-1) if (cnt[i]>sqn) q.pb(i);
memset(cnt,0,sizeof cnt);
scanf("%d", &m);
REP(i,1,m) scanf("%d%d",&e[i].l,&e[i].r),e[i].id=i;
sort(e+1,e+1+m);
int ql=1,qr=0;
REP(i,1,m) {
while (ql<e[i].l) upd(a[ql++],-1);
while (qr>e[i].r) upd(a[qr--],-1);
while (ql>e[i].l) upd(a[--ql],1);
while (qr<e[i].r) upd(a[++qr],1);
ans[e[i].id]=calc();
}
REP(i,1,m) printf("%lld\n", ans[i]);
}

E. Cool Slogans

后缀自动机还没学, 以后补了

Codeforces Round #364 (Div. 1) (差一个后缀自动机)的更多相关文章

  1. Codeforces Round #244 (Div. 2)D (后缀自己主动机)

    Codeforces Round #244 (Div. 2)D (后缀自己主动机) (标号为0的节点一定是null节点,不管怎样都不能拿来用,切记切记,以后不能再错了) 这题用后缀自己主动机的话,对后 ...

  2. Codeforces Round #572 (Div. 1) 差E

    Codeforces Round #572 (Div. 1) A2 题意:给一棵树,带边权,互不相同且为偶数.每次操作是选两个叶子然后在路径上同时加一个数.初始边权全是0,求一个操作序列使得最终边权与 ...

  3. Codeforces Round #573 (Div. 1) 差F

    Codeforces Round #573 (Div. 1) E 题意:二维平面上有 n 个点,你可以放至多 m 条直线使得 (0,0) 与每个点的连线至少与一条直线相交.求原点与所有直线的距离最小值 ...

  4. Codeforces Round #364 (Div. 2)

    这场是午夜场,发现学长们都睡了,改主意不打了,第二天起来打的virtual contest. A题 http://codeforces.com/problemset/problem/701/A 巨水无 ...

  5. Codeforces Round #364 (Div. 2)->A. Cards

    A. Cards time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  6. Codeforces Round #364 (Div.2) C:They Are Everywhere(双指针/尺取法)

    题目链接: http://codeforces.com/contest/701/problem/C 题意: 给出一个长度为n的字符串,要我们找出最小的子字符串包含所有的不同字符. 分析: 1.尺取法, ...

  7. Codeforces Round #364 (Div. 2) Cells Not Under Attack

    Cells Not Under Attack 题意: 给出n*n的地图,有给你m个坐标,是棋子,一个棋子可以把一行一列都攻击到,在根据下面的图,就可以看出让你求阴影(即没有被攻击)的方块个数 题解: ...

  8. Codeforces Round #364 (Div.2) D:As Fast As Possible(模拟+推公式)

    题目链接:http://codeforces.com/contest/701/problem/D 题意: 给出n个学生和能载k个学生的车,速度分别为v1,v2,需要走一段旅程长为l,每个学生只能搭一次 ...

  9. Codeforces Round #364 (Div. 2) D. As Fast As Possible

     D. As Fast As Possible time limit per test 1 second memory limit per test 256 megabytes input stand ...

随机推荐

  1. mysql事务(二)——控制语句使用

    事务控制 一般来说,mysql默认开启了事务自动提交功能,每条sql执行都会提交事务.可以使用如下语句关闭事务自动提交功能. show session variables like 'autocomm ...

  2. mysql错误日志与通用日志

    错误日志 MySQL错误日志是记录MySQL 运行过程中较为严重的警告和错误信息,以及MySQL每次启动和关闭的详细信息. 1.错误日志路径查询 show variables like '%log_e ...

  3. python练习题,写一个方法 传进去列表和预期的value 求出所有变量得取值可能性(例如list为[1,2,3,4,5,6,12,19],value为20,结果是19+1==20只有一种可能性),要求时间复杂度为O(n)

    题目:(来自光荣之路老师)a+b==valuea+b+c=valuea+b+c+d==valuea+b+c+d+...=valuea和b....取值范围都在0-value写一个方法 传进去列表和预期得 ...

  4. jQuery 遍历 - eq() 方法 查找当前元素

    jQuery 遍历 - eq() 方法 if(data[i].status !='已送达'){ $('.w-beget').eq(i).attr('disabled','disabled'); }

  5. Android LCD(二):LCD常用接口原理篇(转)

    源: Android LCD(二):LCD常用接口原理篇

  6. 定义c/c++全局变量/常量几种方法的区别(转载)

    出自:http://www.cnblogs.com/yaozhongxiao/archive/2010/08/08/1795338.html 在讨论全局变量之前我们先要明白几个基本的概念:  1. 编 ...

  7. 使用volley来json解析

    我对网络请求get和post的理解: 1.get只是从某网址获得固定数据,如我访问百度,返回就是百度的html语句: 2.post是我在访问的时候加了某些参数,如我访问某个服务器,访问的时候加了一些语 ...

  8. Git入门私房菜

    昨天下午参考廖雪峰的博客和其他一些文章,简单了解了一下传说中的Git,发现常见用法入门还是挺容易上手的,在此做一些笔记,方便以后查阅和复习. Git安装 Linux sudo apt-get inst ...

  9. thinkphp中的Ueditor的使用, 以及如何传递编辑器内容到后台?

    在线编辑器有很多很多, 而且大多是开源的. uediotr基于mit协议, 开源, 可以用于商业和非商业的 任意使用和修改都可以 如果两个相连接的 相邻的 元素之间 因为边框重叠 而显得中间的边框线很 ...

  10. printf("%f\n",5);

    http://zhidao.baidu.com/link?url=87OGcxtDa6fQoeKmk1KylLu4eIBLJSh7CA3n5NWY-Ipm9TxZViFnIui307duCXWhaM0 ...