bzoj 1334: [Baltic2008]Elect

本文介绍了一个背包问题的具体解决过程,采用排序和动态规划的方法优化了算法效率。通过将物品按体积从大到小排序,并使用一维数组进行状态转移,实现了有效寻找最大装载量的目标。

这个sb题还想了一会,,完蛋。。。直接背包。。。总体积是总席位/2.

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #define N 100005
 5 using namespace std;
 6 int a[N],n,tot,f[N],ans;
 7 bool cmp(int a, int b){return a>b;}
 8 int main()
 9 {
10     scanf("%d",&n);
11     for (int i=1; i<=n; i++) scanf("%d",&a[i]),tot+=a[i];
12     tot/=2; sort(a+1,a+n+1,cmp);
13     f[0]=1;
14     for (int i=1; i<=n; i++)
15         for (int j=tot; j>=0; j--)
16             if (f[j]) f[j+a[i]]=1;
17     for (int i=tot+1; i<=tot*2; i++)
18         if (f[i]) ans=i;
19     printf("%d\n",ans);
20     return 0;
21 }

 

转载于:https://www.cnblogs.com/ccd2333/p/6498474.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值