2016长乐夏令营 Day7

T1:

找个规律排序一发+树状数组一发O(nlog^2n)

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

const int maxn = 2E5 + 20;
typedef long long LL;

struct Day{
	int num,t;
	Day(int _num = 0,int _t = 0) {num = _num; t = _t;}
	bool operator < (const Day &b) const {
		return t > b.t;	
	}
}day[maxn]; 

struct Work{
	int d,r,pos;
	Work(int _d = 0,int _r = 0,int _pos = 0){d = _d; r = _r; pos = _pos;}
	bool operator < (const Work &b) const {
		return d > b.d;	
	}
}job[maxn];

int n,m,ans[maxn];
LL c1[maxn],c2[maxn];

void Insert(int pos)
{
	for (int j = day[pos].num; j <= m; j += j&-j)
		c1[j] += 1LL*day[pos].t,c2[j] += 1LL;
}

bool Judge(int now,int x)
{
	LL sumt,sumd; sumt = sumd = 0;
	for (int j = now; j > 0; j -= j&-j)
		sumt += c1[j],sumd += c2[j];
	return sumt - 1LL*job[x].d*sumd >= 1LL*job[x].r;
}

int getint()
{
	int ret = 0;
	char ch = getchar();
	while (ch < '0' || '9' < ch) ch = getchar();
	while ('0' <= ch && ch <= '9') ret = ret*10 + ch - '0',ch = getchar();
	return ret;
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
		freopen("test.txt","w",stdout);
    #else
		freopen("work.in","r",stdin);
		freopen("work.out","w",stdout);
	#endif
	
	//scanf("%d%d",&n,&m);
	n = getint(); m = getint(); 
	for (int i = 1; i <= m; i++) {
		int x = getint(); 
		//scanf("%d",&x);
		day[i] = Day(i,x);
	}
	sort(day + 1,day + m + 1);
	for (int i = 1; i <= n; i++) {
		int x,y; x = getint(); y = getint();
		//scanf("%d%d",&x,&y);
		job[i] = Work(x,y,i); 
	}
	
	sort(job + 1,job + n + 1);
	int tail = 1;
	for (int i = 1; i <= n; i++) {
		while (tail <= m && day[tail].t > job[i].d) Insert(tail++);
		int L,R; L = 1; R = m+1;
		while (R - L > 1) {
			int mid = (L+R) >> 1;
			if (Judge(mid,i)) R = mid;
			else L = mid;
		}
		int Ans;
		if (Judge(L,i)) Ans = L;
		else Ans = R;
		if (Ans == m + 1) Ans = 0;
		ans[job[i].pos] = Ans;
	}
	for (int i = 1; i <= n; i++) printf("%d ",ans[i]);
	return 0;
}

T2:

画个图易发现两条路径若相交,必与LCA有关,两两枚举冲突路线,建边,做一发最大独立子集

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

const int maxn = 1E5 + 10;
const int S = 0;
const int T = 4901;
const int INF = ~0U>>1;
typedef long long LL;

struct E{
	int from,to,cap,flow;
	E(int _from = 0,int _to = 0,int _cap = 0,int _flow = 0) {
		from = _from; to = _to; cap = _cap; flow = _flow;
	}
}edgs[maxn*10];

int n,L,P,cnt,tot,fa[maxn][20],Deep[maxn],cur[maxn];
LL ans = 0;

vector  v[maxn];
vector  v2[maxn];
queue  Q;

void dfs(int x,int from)
{
	for (int i = 1; i < 20; i++)
		fa[x][i] = fa[fa[x][i-1]][i-1];
	for (int i = 0; i < v[x].size(); i++) {
		int to = v[x][i];
		if (to == from) continue;
		Deep[to] = Deep[x] + 1;
		fa[to][0] = x;
		dfs(to,x);
	}
}

int LCA(int p,int q)
{
	if (Deep[p] < Deep[q]) swap(p,q);
	int Log; for (Log = 0; Deep[p] - (1< 0; Log++); --Log;
	for (int j = Log; j >= 0; j--)
		if (Deep[p] - (1<= Deep[q])
			p = fa[p][j];
	if (p == q) return p;
	for (int j = Log; j >= 0; j--)
		if (fa[p][j] != fa[q][j])
			p = fa[p][j],q = fa[q][j];
	return fa[p][0]; 
}

struct B{
	int a,b,w,num,lca;
	B(int _a = 0,int _b = 0,int _w = 0,int _num = 0) {
		a = _a; b = _b; w = _w; num = _num; lca = LCA(_a,_b);
	}
}lpf[maxn],pyz[maxn];

bool arg(int x,int y)
{
	B p = lpf[x],q = pyz[y];
	if (Deep[p.lca] > Deep[q.lca]) swap(p,q);
	/*if (q.a != q.lca) {
		int Lca = LCA(p.a,q.lca);
		if (Lca == q.lca && p.a != q.lca) {
			Lca = LCA(p.a,q.a);
			if (Lca == p.a || Lca == q.a) return 1;
		}
		Lca = LCA(p.b,q.lca);
		if (Lca == q.lca && p.b != q.lca) {
			Lca = LCA(p.b,q.a);
			if (Lca == p.b || Lca == q.a) return 1;
		}
	}
	if (q.b != q.lca) {
		int Lca = LCA(p.a,q.lca);
		if (Lca == q.lca && p.a != q.lca) {
			Lca = LCA(p.a,q.b);
			if (Lca == p.a || Lca == q.b) return 1;
		}
		Lca = LCA(p.b,q.lca);
		if (Lca == q.lca && p.b != q.lca) {
			Lca = LCA(p.b,q.b);
			if (Lca == p.b || Lca == q.b) return 1;
		}
	}*/
	int Lca = LCA(q.lca,p.a);
	if (Lca == q.lca) return 1;
	Lca = LCA(q.lca,p.b);
	if (Lca == q.lca) return 1;
	return 0;
}

void Add(int from,int to,int cap)
{
	v2[from].push_back(cnt); edgs[cnt++] = E(from,to,cap,0);
	v2[to].push_back(cnt); edgs[cnt++] = E(to,from,0,0);
}

bool BFS()
{
	for (int i = 1; i <= tot; i++) Deep[i] = 0;
	Deep[T] = 0; Deep[S] = 1; Q.push(S);
	while (!Q.empty()) {
		int k = Q.front(); Q.pop();
		for (int i = 0; i < v2[k].size(); i++) {
			E e = edgs[v2[k][i]];
			if (e.cap == e.flow) continue;
			if (Deep[e.to]) continue;
			Deep[e.to] = Deep[k] + 1;
			Q.push(e.to);
		}
	}
	return Deep[T];
}

int Dicnic(int x,int a)
{
	if (x == T) return a;
	int flow = 0;
	for (int &i = cur[x]; i < v2[x].size(); i++) {
		E &e = edgs[v2[x][i]];
		if (e.cap == e.flow) continue;
		if (Deep[e.to] != Deep[x] + 1) continue;
		int f = Dicnic(e.to,min(a,e.cap - e.flow));
		if (f > 0) {
			flow += f;
			e.flow += f;
			edgs[v2[x][i]^1].flow -= f;
			a -= f;
			if (!a) return flow;
		}
	}
	if (!flow) Deep[x] = -1;
	return flow;
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
    #else
		freopen("bus.in","r",stdin);
		freopen("bus.out","w",stdout);
	#endif
	
	cin >> n >> L >> P; 
	for (int i = 1; i < n; i++) {
		int x,y; scanf("%d%d",&x,&y);
		v[x].push_back(y); v[y].push_back(x);
	}
	Deep[1] = 1; dfs(1,0);
	for (int i = 1; i <= L; i++) {
		int x,y,w; scanf("%d%d%d",&x,&y,&w);
		lpf[i] = B(x,y,w,++tot);
		ans += 1LL*w;
	}
	for (int i = 1; i <= P; i++) {
		int x,y,w; scanf("%d%d%d",&x,&y,&w);
		pyz[i] = B(x,y,w,++tot);
		ans += 1LL*w;
	}
	
	for (int i = 1; i <= L; i++)
		for (int j = 1; j <= P; j++)
			if (arg(i,j))
				Add(lpf[i].num,pyz[j].num,INF);
	for (int i = 1; i <= L; i++) Add(S,lpf[i].num,lpf[i].w);
	for (int i = 1; i <= P; i++) Add(pyz[i].num,T,pyz[i].w);
	
	LL flow = 0;
	while (BFS()) {
		for (int i = 1; i <= tot; i++) cur[i] = 0;
		cur[S] = cur[T] = 0;
		flow += 1LL*Dicnic(S,INF); 
	}
	cout << ans - flow;
	return 0;
}

T3:

树dp  f[i][j] : i的子树中,是(否)已经完全填充,若是(否),还可以向上(下)扩展j个单位

显然,0 <= j <= 100,这个答案的优劣程度具有显然的单调性,即,如果f[i][j+1] < f[i][j] ,那么f[i][j]可以等于f[i][j+1]  未完全填满的情况也一样

我们让-100 <= j <= 100 (开一个第二维201的数组,两种0是要分开讨论的)

如果选i  f[i][w[i]] = 1 + ∑min(f[son][j])   -w[i] <= j <= 100

如果不选i f[i][j] = 

if (j >= 0)  f[k][j+1] + ∑min(f[son][x])  (son != k,-j <= x <= 100)

if (j <= 0)  f[k][j+1] + ∑min(f[son][x]) (son != k,j < x <= 100)

k通过枚举和最大值、次大值的特判

min可以用上述单调性O(1)维护与查询

对于f中的非法状态,直接赋值INF,f数组开LL,这样方便编写

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

const int maxn = 1E5 + 10; 
const int INF = maxn;
typedef long long LL;

LL sum[210],f[maxn][210];
int n,m,t,w[maxn],Maxw[maxn];

vector  v[maxn];

void Work(int x,int from)
{
	memset(sum,0,sizeof(sum));
	for (int i = 0; i < v[x].size(); i++) {
		int to = v[x][i];
		if (to == from) continue;
		for (int j = 0; j <= 201; j++) sum[j] += f[to][j];
	}
	for (int i = 0; i <= 200; i++) {
		f[x][i] = INF;
		if (i > 100)
			for (int j = 0; j < v[x].size(); j++) {
				int to = v[x][j];
				if (to == from) continue;
				//if (f[to][i+1] == INF) continue;
				f[x][i] = min(f[x][i],f[to][i+1] + sum[202-i] - f[to][202-i]);
			}
		else f[x][i] = sum[i+1];
	}
	f[x][201] = INF;
}

void dfs(int x,int from)
{
	if (v[x].size() == 1 && from) {
		for (int i = 101; i <= w[x] + 101; i++) f[x][i] = 1;
		for (int i = w[x] + 102; i <= 201; i++) f[x][i] = INF;
		for (int i = 0; i <= 100; i++) f[x][i] = 0;
		return;
	}
	for (int i = 0; i < v[x].size(); i++) {
		int to = v[x][i];
		if (to == from) continue;
		dfs(to,x);
	}
	Work(x,from);
	LL tot = 1LL + sum[101-w[x]];
	f[x][w[x]+101] = min(f[x][w[x]+101],tot);
	for (int i = 200; i >= 0; i--) 
		f[x][i] = min(f[x][i],f[x][i+1]);
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
    #else
		freopen("city.in","r",stdin);
		freopen("city.out","w",stdout);
	#endif
	
	cin >> t;
	while (t--) {
		cin >> n;
		for (int i = 1; i <= n; i++) scanf("%d",&w[i]);		
		for (int i = 1; i < n; i++) {
			int x,y; scanf("%d%d",&x,&y);
			v[x].push_back(y); 
			v[y].push_back(x);
		}	 
		dfs(1,0);
		printf("%d\n",f[1][101]);
		for (int i = 1; i <= n; i++) v[i].clear();
	}
	return 0;
}

你可能感兴趣的:(网络流,dp,树状数组,树,2016长乐夏令营)