Codeforces 313

A. Ilya and Bank Account 水题

B. Ilya and Queries 水,维护区间和

C. Ilya and Matrix

贪心。大的明显会被多次取。

/**
 * Problem:313C
 * Author:Shun Yao
 * Time:2013.5.31
 * Result:Accepted
 * Memo:greedy
 */

#include <cstdio>
#include <algorithm>
#include <functional>

const long MaxN = 2000001;

long n, N, a[MaxN];
long long s[MaxN], ans;

int main() {
	static long i;
	scanf("%ld", &N);
	for (i = 1; i <= N; ++i)
		scanf("%ld", a + i);
	std::sort(a + 1, a + N + 1, std::greater<long>());
	s[0] = 0;
	for (i = 1; i <= N; ++i)
		s[i] = s[i - 1] + a[i];
	ans = 0;
	for (i = 1; i <= N; i <<= 2)
		ans += s[i];
	printf("%lld", ans);
	return 0;
}

 D. Ilya and Roads

好吧,dp。。。

 状态很好想,f[i][j]代表前i个人覆盖了j个。

然后是g[i][j]代表覆盖区间 i -- j 的最小cost;

转移见code:

/**
 * Problem:313D
 * Author:Shun Yao
 * Time:2013.6.3
 * Result:Accepted
 * Memo:DP
 */

#include <cstdio>

long long min(long long x, long long y) {
	return x < y ? x : y;
}

const long Maxn = 305;
const long long inf = 1LL << 60;

long n, m, K;
long long g[Maxn][Maxn], f[Maxn][Maxn], ans;

int main() {
	static long a, b, c, i, j, k;
	scanf("%ld%ld%ld", &n, &m, &K);
	for (i = 0; i <= n; ++i)
		for (j = 0; j <= n; ++j)
			g[i][j] = inf;
	for (i = 1; i <= m; ++i) {
		scanf("%ld%ld%ld", &a, &b, &c);
		g[a][b] = min(g[a][b], c);
	}
	for (i = 1; i <= n; ++i)
		for (j = i; j <= n; ++j)
			g[i][j] = min(g[i][j], g[i - 1][j]);
	for (i = 0; i <= n; ++i) {
		f[i][0] = 0;
		for (j = 1; j <= n; ++j)
			f[i][j] = inf;
	}
	for (i = 1; i <= n; ++i)
		for (j = 0; j <= i; ++j) {
			f[i][j] = f[i - 1][j];
			for (k = i - j + 1; k <= i; ++k)
				if (f[k - 1][j - (i - k + 1)] != inf && g[k][i] != inf)
					f[i][j] = min(f[i][j], f[k - 1][j - (i - k + 1)] + g[k][i]);
		}
	ans = inf;
	for (i = K; i <= n; ++i)
		if (ans > f[n][i])
			ans = f[n][i];
	printf("%I64d", ans == inf ? -1 : ans);
	return 0;
}

E. Ilya and Two Numbers

贪心,构造。

/**
 * Problem:313E
 * Author:Shun Yao
 * Time:2013.6.15
 * Result:Acceped
 * Memo:constructive algorithms, greedy
 */

#include <cstring>
#include <cstdio>
#include <functional>
#include <algorithm>

#define Maxn 100005

long n, m, a[Maxn], b[Maxn], c[Maxn], d[Maxn], ans[Maxn];

int main() {
	static long i, x, l;
#ifndef ONLINE_JUDGE
	freopen("e.in", "r", stdin);
	freopen("e.out", "w", stdout);
#endif
	memset(a, 0, sizeof a);
	memset(b, 0, sizeof b);
	scanf("%ld%ld", &n, &m);
	for (i = 1; i <= n; ++i) {
		scanf("%ld", &x);
		++a[x];
	}
	for (i = 1; i <= n; ++i) {
		scanf("%ld", &x);
		++b[x];
	}
	ans[0] = 0;
	d[0] = 0;
	for (i = m - 1; i >= 0; --i) {
		while (b[m - 1 - i]--)
			c[++l] = m - 1 - i;
		while (a[i]--) {
			if (l)
				ans[++ans[0]] = i + c[l--];
			else
				d[++d[0]] = i;
		}
	}
	for (i = 1; i <= d[0]; ++i)
		ans[++ans[0]] = d[i] + c[l--] - m;
	std::sort(ans + 1, ans + n + 1, std::greater<long>());
	for (i = 1; i <= n; ++i)
		printf("%ld ", ans[i]);
	return 0;
}
原文地址:https://www.cnblogs.com/hsuppr/p/3118614.html