Codeforces 700ABCDE

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]);
	}
	return;
}

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))

算是挺简单的了, 还是打了好久QAQ

D. Huffman Coding on Segment

莫队一下, 然后将出现次数小于等于$\sqrt(n)$的暴力合, 其余的用堆合, 复杂度$O(m\sqrt{n}logn)$

#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]);
}

猜你喜欢

转载自www.cnblogs.com/uid001/p/10574977.html
今日推荐