[Meter garlic passenger T2238] segment tree gift _ _ _ probability merge sort expectations

gift

Subject to the effect :

Data range :


Problem solution :

This question is interesting ah ($ md $ card often

Direct do how to do?

Just last something about the maintenance of matrix multiplication and insert, such as constant fairly small $ KD-Tree $ (since I had seen people too

We missed a condition that all tuples are random.

This condition is very good, it almost ensures that a range of optional words, excellent tuple only $ log $ months.

Why is this?

In fact the interval, number of outstanding tuple, the equivalent interval in accordance with the sort $ X $, $ Y $ prefix value is the number of a desired maximum value.

Because tuple is random, so after ordering $ x $, $ y $ is still random.

Is given a random number sequence, a desired number of prefix find the maximum value.

This is the harmonic series.

So, we opened a segment tree , the tree each node maintains a line array of deposit outstanding tuple within the jurisdiction of the range of the node.

Combined with the merge , the complexity is $ O (log) $ a.

So every query complexity is $ O (log ^ 2n) $ a.

The total complexity is $ O (nlog ^ 2n) $, and often a little card, plus the output optimized for just over (read optimization is a must.

Code :

#include <bits/stdc++.h>

#define N 200010 

using namespace std;

int head[N], to[N << 1], nxt[N << 1], tot;

char *p1, *p2, buf[100000];

#define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++ )

int rd() {
	int x = 0, f = 1;
	char c = nc();
	while (c < 48) {
		if (c == '-')
			f = -1;
		c = nc();
	}
	while (c > 47) {
		x = (((x << 2) + x) << 1) + (c ^ 48), c = nc();
	}
	return x * f;
}

inline void add(int x, int y) {
	to[ ++ tot] = y;
	nxt[tot] = head[x];
	head[x] = tot;
}

int d[N], x[N], y[N];

int main() {
	int n = rd(), k = rd();
	for (int i = 2; i <= n; i ++ ) {
		x[i] = rd(), y[i] = rd();
		d[x[i]] ++ ;
		d[y[i]] ++ ;
	}

	int mx = 0;
	for (int i = 2; i <= n; i ++ ) {
		int s1 = d[x[i]], s2 = d[y[i]];
		if (s1 < s2)
			swap(s1, s2);
		if (s1 >= 3) {
			if (s2 >= 3) {
				mx = max(mx, 2);
			}
			else if(s2 <= 2) {
				mx = max(mx, 1);
			}
		}
	}

	int sum = 0;
	for (int i = 1; i <= n; i ++ ) {
		if (d[i] == 1) {
			sum ++ ;
		}
	}

	mx *= k;

	cout << sum - mx << endl ;
	return 0;
}

小结:这种期望的题还是要自己证才行,不然结论根本记不过来。

Guess you like

Origin www.cnblogs.com/ShuraK/p/11256723.html