Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P4394 [BalticOI 2008]选举 Easy

P4394 [BalticOI 2008]选举 Easy

需要选择若干数,使得大于总数的一半;去掉某个数,一定不超过总数的一半。

考虑去掉的这个数,如果去掉最小的,满足不超过总数的一半,那么去掉任意一个数一定满足。

考虑背包,从大的数向小的枚举,选择时当前的数就是确定的当前选择的最小的数。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <cstdio>
#include <algorithm>
using namespace std;
template <class Type>
void read(Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
flag && (x = ~x + 1);
}
template <class Type>
void write(Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar(x % 10 + '0');
}
void chkmax(int &x, int k)
{
(x < k) && (x = k);
}
const int N = 310, M = 1e5 + 10;
int n, w[N];
bool f[M];
int main()
{
read(n);
int sum = 0, mid = 0;
for (int i = 1; i <= n; i++)
read(w[i]), sum += w[i];
mid = sum >> 1;
sort(w + 1, w + n + 1);
f[0] = true;
int res = 0;
for (int i = n; i; i--)
for (int j = sum; j >= w[i]; j--)
{
f[j] |= f[j - w[i]];
f[j] && j > mid &&j - w[i] <= mid && (chkmax(res, j), 0);
}
write(res);
return 0;
}