USACO Section 2.2: Subset Sums
dp题,一碰到dp我基本就是跪,搜了网上的答案分两种,一维和二维。
先讲二维,sum[i][j]表示前i个数的subset里差值为j的分法数量。当加入数字i时,有两种选择,某一个set和另外一个set,当加入其中一个总和大的set时,新的差值为j+i,当加入一个总和小的set时,新的差值为abs(j-i)。
/*
ID: yingzho1
LANG: C++
TASK: subset
*/
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <cstring>
using namespace std;
ifstream fin("subset.in");
ofstream fout("subset.out");
int N, s1, s2, acount;
][];
int main()
{
fin >> N;
sum[][] = ;
; i <= N; i++) {
; j < ; j++) {
sum[i][j+i] += sum[i-][j];
sum[i][abs(j-i)] += sum[i-][j];
}
}
fout << sum[N][] << endl;
;
}
一元的思路就是sum[i]表示总和为i的set数。
/*
ID: yingzho1
LANG: C++
TASK: subset
*/
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <cstring>
using namespace std;
ifstream fin("subset.in");
ofstream fout("subset.out");
int N;
];
int main()
{
fin >> N;
)*N/;
) {
fout << << endl;
;
}
part /= ;
sum[] = ;
; i <= N; i++) {
for (int j = part; j >= i; j--) {
sum[j] += sum[j-i];
}
}
fout << sum[part]/ << endl;
;
}
USACO Section 2.2: Subset Sums的更多相关文章
- 【USACO 2.2】Subset Sums (DP)
N (1 <= N <= 39),问有多少种把1到N划分为两个集合的方法使得两个集合的和相等. 如果总和为奇数,那么就是0种划分方案.否则用dp做. dp[i][j]表示前 i 个数划分到 ...
- 洛谷P1466 集合 Subset Sums
P1466 集合 Subset Sums 162通过 308提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 对于从1到N (1 ...
- Project Euler 106:Special subset sums: meta-testing 特殊的子集和:元检验
Special subset sums: meta-testing Let S(A) represent the sum of elements in set A of size n. We shal ...
- Project Euler P105:Special subset sums: testing 特殊的子集和 检验
Special subset sums: testing Let S(A) represent the sum of elements in set A of size n. We shall cal ...
- Project Euler 103:Special subset sums: optimum 特殊的子集和:最优解
Special subset sums: optimum Let S(A) represent the sum of elements in set A of size n. We shall cal ...
- Codeforces348C - Subset Sums
Portal Description 给出长度为\(n(n\leq10^5)\)的序列\(\{a_n\}\)以及\(m(m\leq10^5)\)个下标集合\(\{S_m\}(\sum|S_i|\leq ...
- CodeForces 348C Subset Sums(分块)(nsqrtn)
C. Subset Sums time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...
- DP | Luogu P1466 集合 Subset Sums
题面:P1466 集合 Subset Sums 题解: dpsum=N*(N+1)/2;模型转化为求选若干个数,填满sum/2的空间的方案数,就是背包啦显然如果sum%2!=0是没有答案的,就特判掉F ...
- spoj-SUBSUMS - Subset Sums
SUBSUMS - Subset Sums Given a sequence of N (1 ≤ N ≤ 34) numbers S1, ..., SN (-20,000,000 ≤ Si ≤ 20, ...
随机推荐
- Careercup - Microsoft面试题 - 6314866323226624
2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...
- volley框架 出现at com.android.volley.Request.<init>
请求json数据的时候出现这种情况: 原因: // 访问网络,初始化详情 JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET ...
- hdu 1548 A strange lift 宽搜bfs+优先队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...
- Js高程笔记->引用类型
1 . Object 对象 2 . Array 对象 : 检测方法:ES5 : isArray 转换方法: toLocaleString , toString , val ...
- 发布windows phone应用经历实谈
经过这一次艰辛的发布应用的过程,看来果然这不是个简单的过程,不过经历过了一次之后感觉其实也没这么难,下面我将介绍我通过学生账号发布windows phone 8的应用到商店的全过程,其实整个过程最为困 ...
- Codeforces Round #242 (Div. 2) C题
题目链接:http://codeforces.com/contest/424/problem/C, 想来一个小时,就是做不出,都做出来了,悲剧! 分析:我们知道交换异或的顺序不影响答案! 然后就是求t ...
- 实现WMSservice的时候,出现边缘的点或icon被切断的情况
可以通过为实际查询的boundary加一个buffer,使查询的范围比指定的大一点点,这样就会使tile之间在查询的时候有一定的重叠. 如:Geometry queryBoundary = JTS.t ...
- IT架构之IT架构模型——思维导图
参考: [日] 野村综合研究所系统咨询事业本部. 图解CIO工作指南. 周自恒译 人民邮电出版社,2014
- Android fill_parent、wrap_content和match_parent的区别
三个属性都用来适应视图的水平或垂直大小,一个以视图的内容或尺寸为基础的布局比精确地指定视图范围更加方便. 1)fill_parent 设置一个构件的布局为fill_parent将强制性地使构件扩展,以 ...
- Linux 双线策略路由的三种实现方式总结+端口映射
Linux 双线策略路由的三种实现方式总结+端口映射 Linux 双线策略路由的三种实现方式总结+端口映射 网络环境 服务器(网关): eth0 为LAN口,IP为 LAN_IP = 192.168. ...