ZOJ-1880
来源:互联网 发布:印度 网络空间作战部队 编辑:程序博客网 时间:2024/04/30 14:06
又是一道DP题搞了一晚上,我去啊,DP实在是太菜了,这题应该算DP里很简单的了。。刚开始题意看错了,说是两个组里面人数差不能超过1,我去,没看到这个条件写了半天逻辑全错的,还对着测试数据看了半天没看出问题。。感觉我的算法很土。。就是记录了下每个可能和的所有组合个数,最后再统计下那种所用组合人数刚好为总人数一半的方案。。
#include<cstdio>#include<cstring>#include<algorithm>#include<cstdlib>using namespace std;int main(){int n;bool dp[25001];bool count[25001][52];while (scanf("%d", &n) != EOF){memset(dp, 0, sizeof(dp));memset(count, 0, sizeof(count));count[0][0] = true;dp[0] = true;int w, sum = 0;for (int i = 0; i < n; i++){scanf("%d", &w);sum += w;for (int j = 25000; j >= w; j--)if (dp[j - w]){dp[j] = true;for (int k = 0; k <= 50; k++)if (count[j - w][k])count[j][k + 1] = true;}}int mind = 99999, res;for (int i = 0; i <= 25000; i++)if (count[i][n / 2]){int temp = abs(sum - i - i);if (temp < mind){mind = temp;res = i;}}printf("%d %d\n", min(res, sum - res), max(res, sum - res));}return 0;}
0 0
- ZOJ-1880
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- quick-cocos2d-x开发工具sublime text及其强力插件QuickXDev
- C#串口通讯类
- 线性结构完成&&一些总结
- Red Hat Enterprise Linux 5——tftp、NFS、Samba服务器配置
- leetcode - Merge Two Sorted Lists
- ZOJ-1880
- POJ 2836 Rectangular Covering 状态压缩DP
- etnjsrtjnryj食肉菌认识他你就身体夜啊他和
- 魔兽世界
- HOOK其他进程API和全局HOOK-API
- Android bitmap中Matrix的几个操作
- 精益设计:“纸上谈兵”的说服力
- 基于cocos2dx的2D手游美术资源制作技术选型(1)(2)
- 数据结构中的几种排序