bzoj 1334: [Baltic2008]Elect

Description

N个政党要组成一个联合内阁,每个党都有自己的席位数. 现在希望你找出一种方案,你选中的党的席位数要大于总数的一半,并且联合内阁的席位数越多越好. 对于一个联合内阁,如果某个政党退出后,其它党的席位仍大于总数的一半,则这个政党被称为是多余的,这是不允许的.

Input

第一行给出有多少个政党.其值小于等于300 下面给出每个政党的席位数.总席位数小于等于 100000

Output

你的组阁方案中最多能占多少个席位.

Sample Input

4
1 3 2 4

Sample Output

7

HINT

选择第二个政党和第四个

Source

 http://www.lydsy.com/JudgeOnline/problem.php?id=1334
 
 
任然是dp, 但要注意先排序
 
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 int cmp(int a,int b){
 7     return a>b;
 8 }
 9 int f[300001]={false};
10 int main(){
11     int n; scanf("%d",&n);
12     int s=0;
13     int a[301]={0};
14     for (int i=1;i<=n;i++) {
15         scanf("%d",&a[i]);
16         s+=a[i];
17     }
18     s=s/2; 
19     sort(a+1,a+n+1,cmp);
20     f[0]=true;
21     for (int i=1;i<=n;i++)
22         for (int j=s+a[i];j>=a[i];j--)
23             if (f[j-a[i]]) f[j]=true;
24     for (int i=s*2;i>=0;i--) 
25         if (f[i]) {
26             printf("%d",i);
27             break;
28     }
29     return 0;
30 }
原文地址:https://www.cnblogs.com/lztlztlzt/p/6244991.html