题目大意

一群人拔河,给出每个人的重量,要求两队人数之差不超过1人,且每队总重量之差最小。

思路

选出严格总人数一半(或+1)的人为一队,在该队重量不超过所有人总重量一半的情况下,使其重量最大。

人数一维,最终结果严格为其最大限制:总人数一半;队总重量一维,最终结果尽量大,不一定为其最大限制:所有人总重量一半。

所以初始化时,应将DP数组初始化为-∞,循环结束时,在DP[totV]中求最大值。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdarg>
using namespace std; const int MAX_W = 23000, MAX_V = 55, MAX_ORG = 110; int _max(int a, int b)
{
return b == 0 ? a : max(a, b);
} int Dp(int totV, int totObj, int totW, int *objW)
{
static int DP[MAX_W][MAX_V];
memset(DP, 0xcf, sizeof(DP));
DP[0][0] = 0;
for (int obj = 1; obj <= totObj; obj++)
for (int w = totW; w >= objW[obj]; w--)
for (int v = totV; v >= 1; v--)
DP[w][v] = _max(DP[w][v], DP[w - objW[obj]][v - 1] + objW[obj]);
int ans = 0;
for (int i = 0; i <= totW; i++)
ans = max(ans, DP[i][totV]);
return ans;
} int main()
{
#ifdef _DEBUG
freopen("c:\\noi\\source\\input.txt", "r", stdin);
#endif
int n, sum = 0, ws[MAX_ORG];
memset(ws, 0, sizeof(ws));
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", i + ws);
sum += ws[i];
}
int lighter = Dp(n / 2, n, (sum + 1) / 2, ws);
if ((n / 2) * 2 != n)
lighter = max(lighter, Dp(n / 2 + 1, n, (sum + 1) / 2, ws));
int weighter = sum - lighter;
if (lighter > weighter)
swap(lighter, weighter);
printf("%d %d\n", lighter, weighter);
return 0;
}

  

最新文章

  1. Livecoding.tv2.5发布,增加“用户搜索引擎”功能,方便用户找到匹配的程序员
  2. HBase vs. BigTable Comparison - HBase对比BigTable
  3. 黑马程序员——JAVA基础之程序控制流结构之循环结构,循环嵌套
  4. Can Live View boot up images acquired from 64bit OS evidence?
  5. 546B. Soldier and Badges
  6. 父 shell,子 shell ,export 与 变量传递
  7. 为什么用linear regression可以做classification
  8. InstallShield常用prq文件的下载地址
  9. wex5添加视频播放
  10. IOS的一个带动画的多项选择的控件(一)
  11. 解决一bug的流程复盘
  12. git的安装和环境配置过程(学习笔记)
  13. 每天一个Linux命令(17)--whereis命令
  14. Apriori和FPTree
  15. AT2369 Ants on a Circle (思路)
  16. 搭建Django链接MySQL流程(python2版)
  17. 转:RowVersion 用法
  18. 10个对Web开发者最有用的Python包
  19. [arm学习]makefile学习总结
  20. Spring+SpringMVC+MyBatis整合基础篇(三)搭建步骤

热门文章

  1. Oracle 关于oracle自带的行转列函数
  2. VmWare 安装 Centos
  3. mysql自动添加时间的方法
  4. 从XMLHttpRequest中获取请求的URL
  5. QT设计UI:QT模式对话框打开文件
  6. Visual Basic for Application
  7. 题解 P3258 【[JLOI2014]松鼠的新家】(From luoguBlog)
  8. tomcat实现连接数据库
  9. android keystore的生成和使用
  10. js对比for、forEach、map遍历数组速度