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, ...
随机推荐
- android开发 wifi开发不稳定性测试
场景:工厂定制机器,要求一个设备创建wifi热点,一个设备去连接.但是现在发现wifi连接很不稳定,主要以下3方面: 1.连接之前,不容易连接上 2.连接上之后,连不到外网 3.连接上之后,稳定性不好 ...
- 【Search a 2D Matrix】cpp
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- Spring MVC mapping[From Spring MVC Beginner's Guide]
In a Spring MVC application, the URL can logically be divided into five parts (see the following fig ...
- [nowCoder] 二进制中1的个数
题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. class Solution { public: int NumberOf1(int n) { ; while(n) ...
- 使用fwrite()函数和fprintf()函数输出数据到文件时的区别
使用书上的一个课后题为例 有5个学生,每个学生有3门课的成绩,从键盘输入学生数据(包括学号,姓名,3们课程成绩),计算出每个学生的平均成绩,将原有数据和计算出的平均分数存放在磁盘文件“stud”中. ...
- 理解Session的几种模式
一.写在前面 我们在使用ASP.NET开发的过程中,有时会进行数据存储以实现请求前后的状态保持(HTTP是无状态保持的协议),而Session作为一种快速简单易于实现的方式被我们经常使用,当然如果出于 ...
- Nginx SPDY Pagespeed模块编译——加速网站载入
在看<Web性能权威指南>的时候,看到了SPDY这货,于是便开始折腾起了这个了,也顺便把pagespeed加了进去. Nginx SPDY 引自百科~~ SPDY(读作“SPeeDY”)是 ...
- zend studio插件
1.安装使用Aptana插件(html,css,js代码提示功能) 安装步骤: 1).zend studio->Help->Install New Software->work wi ...
- vi/vim使用指北 ---- Beyond the Basic
更多的组合命令 [number]-[command]-[test object] number: 数字 comand: c,d,y (修改,删除,复制) test object: 移动光标的命 ...
- Comet技术浅论
1.如何实现一个轮询? function getMessage(url,callback){ var XHR=new XMLHttpRequest(); XHR.open('get',url,true ...