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 怎么动态设置button 的style
网上找了很多,还是没有直接的解决办法,button没有setstyle这个方法.因此我的解决办法如下: 直接动态设置各个属性 Button themeBtn = new Button(this); t ...
- android 开发对gif解码(适配android 4.2、4.3、4.4版本)
android 开发对gif解码(适配android 4.2.4.3.4.4版本) 使用方法: public class ImageInputActivity extends Activity imp ...
- The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes limit
如果你是通过搜索来到本文的,相信你应该是遇到了如下的错误 The code of method _jspService(HttpServletRequest, HttpServletResponse) ...
- javascript与DOM -- 深入理解javascript
/* 一.文档对象模型Document Object Model DOM(Document Object Model,文档对象模型)是一个通过和JavaScript进行内容交互的API */ /* 注 ...
- 802.11 wireless 五
802.11 wireless 5CSMA/CA,采用倒计时的方法,退避的时间(当年时间+duration 为发送时间,每一个帧会有一个duration,这个位叫做duration[n.持续]) PS ...
- Leetcode#123 Best Time to Buy and Sell Stock III
原题地址 最直观的想法就是划分成两个子问题,每个子问题变成了:求在某个范围内交易一次的最大利润 在只能交易一次的情况下,如何求一段时间内的最大利润?其实就是找股价最低的一天买进,然后在股价最高的一天卖 ...
- fullscreen DXGI DX11
these days i am fullfilling full screen https://github.com/rufelt/simpled3d11window put this one in ...
- 对话框Dialog
QMainWindow QMainWindow是 Qt 框架带来的一个预定义好的主窗口类. 主窗口,就是一个普通意义上的应用程序(不是指游戏之类的那种)最顶层的窗口.通常是由一个标题栏,一个菜单栏,若 ...
- 一道PK赛题
Problem Description I think that you might have played the traditional Chinese ring game: The Chines ...
- .NET中JSON的序列化和反序列化
.NET 中有两种方法进行JSON的操作分别需要引用不同的命名空间 1.System.Runtime.Serialization.Json(System.Runtime.Serialization.d ...