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的更多相关文章

  1. 【USACO 2.2】Subset Sums (DP)

    N (1 <= N <= 39),问有多少种把1到N划分为两个集合的方法使得两个集合的和相等. 如果总和为奇数,那么就是0种划分方案.否则用dp做. dp[i][j]表示前 i 个数划分到 ...

  2. 洛谷P1466 集合 Subset Sums

    P1466 集合 Subset Sums 162通过 308提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 对于从1到N (1 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. Codeforces348C - Subset Sums

    Portal Description 给出长度为\(n(n\leq10^5)\)的序列\(\{a_n\}\)以及\(m(m\leq10^5)\)个下标集合\(\{S_m\}(\sum|S_i|\leq ...

  7. CodeForces 348C Subset Sums(分块)(nsqrtn)

    C. Subset Sums time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...

  8. DP | Luogu P1466 集合 Subset Sums

    题面:P1466 集合 Subset Sums 题解: dpsum=N*(N+1)/2;模型转化为求选若干个数,填满sum/2的空间的方案数,就是背包啦显然如果sum%2!=0是没有答案的,就特判掉F ...

  9. spoj-SUBSUMS - Subset Sums

    SUBSUMS - Subset Sums Given a sequence of N (1 ≤ N ≤ 34) numbers S1, ..., SN (-20,000,000 ≤ Si ≤ 20, ...

随机推荐

  1. Kibana4学习<三>

    discover 功能 Discover 标签页用于交互式探索你的数据.你可以访问到匹配得上你选择的索引模式的每个索引的每条记录.你可以提交搜索请求,过滤搜索结果,然后查看文档数据.你还可以看到匹配搜 ...

  2. IOS常用加密DES

    NSString+DES.h // // NSString+DES.h // haochang // // Created by Administrator on 14-4-15. // Copyri ...

  3. android中的“visible ”、“invisible”、“gone”的区别(转载)

    在Android开 发中,大部分控件都有visibility这个属性,其属性有3个分别为“visible ”.“invisible”.“gone”.主要用来设置控制控件的显示和隐藏.有些人可能会疑惑I ...

  4. django Forgienkey字段 在前台用js做处理

    在我做的项目中有个选择省城市的选项,这两个字段的关系是一对多的关系class Province(models.Model): # 省会      name = models.CharField(max ...

  5. 设计模式之建造者模式(Builder)

    建造者模式原理:建造模式主要是用于产生对象的各个组成部分,而抽象工厂模式则用于产生一系列对象,建造者模式而且要求这些对象的组成部分有序. 代码如下: #include <iostream> ...

  6. 【BZOJ】【1406】【AHOI2007】密码箱

    数论 Orz iwtwiioi 果然数论很捉鸡>_>完全不知道怎么下手 $$x^2 \equiv 1 \pmod n \rightarrow (x+1)*(x-1)=k*n $$ 所以,我 ...

  7. Leetcode#108 Convert Sorted Array to Binary Search Tree

    原题地址 对于已排序数组,二分法递归构造BST 代码: TreeNode *buildBST(vector<int> &num, int i, int j) { if (i > ...

  8. NYOJ-289 苹果 TLE 分类: NYOJ 2013-12-29 17:52 282人阅读 评论(0) 收藏

    #include<stdio.h> struct apple{ int m; int v; }app[1010]; int money(int i,int v); int main(){ ...

  9. 重启nginx后丢失nginx.pid解决

    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

  10. POJ 1548 Robots (Dilworth)

    Robots Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3621 Accepted: 1643 Description Yo ...