ZOJ 1711 H-Sum It Up
https://vjudge.net/contest/67836#problem/H
Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t = 4, n = 6, and the list is [4, 3, 2, 2, 1, 1], then there are four different sums that equal 4: 4, 3+1, 2+2, and 2+1+1. (A number can be used within a sum as many times as it appears in the list, and a single number counts as a sum.) Your job is to solve this problem in general.
Input
The input file will contain one or more test cases, one per line. Each test case contains t, the total, followed by n, the number of integers in the list, followed by n integers x 1 , . . . , x n . If n = 0 it signals the end of the input; otherwise, t will be a positive integer less than 1000, n will be an integer between 1 and 12 (inclusive), and x 1 , . . . , x n will be positive integers less than 100. All numbers will be separated by exactly one space. The numbers in each list appear in nonincreasing order, and there may be repetitions.
Output
For each test case, first output a line containing `Sums of', the total, and a colon. Then output each sum, one per line; if there are no sums, output the line `NONE'. The numbers within each sum must appear in nonincreasing order. A number may be repeated in the sum as many times as it was repeated in the original list. The sums themselves must be sorted in decreasing order based on the numbers appearing in the sum. In other words, the sums must be sorted by their first number; sums with the same first number must be sorted by their second number; sums with the same first two numbers must be sorted by their third number; and so on. Within each test case, all sums must be distinct; the same sum cannot appear twice.
Sample Input
4 6 4 3 2 2 1 1
5 3 2 1 1
400 12 50 50 50 50 50 50 25 25 25 25 25 25
0 0
Sample Output
Sums of 4:
4
3+1
2+2
2+1+1
Sums of 5:
NONE
Sums of 400:
50+50+50+50+50+50+25+25+25+25
50+50+50+50+50+25+25+25+25+25+25
时间复杂度:$O(2^n)$
题解:dfs , 按照字典序输出
代码:
#include <bits/stdc++.h>
using namespace std; int t, n, add, cnt;
int a[15], key[15], vis[15]; struct Ans{
int b[15];
int len;
}ans[4200];
int sz; bool cmp2(const Ans& a, const Ans& b) {
for(int i = 0; i < min(a.len, b.len); i ++) {
if(a.b[i] != b.b[i]) return a.b[i] > b.b[i];
}
return a.len > b.len;
} bool cmp(int n1, int n2) {
return n1 > n2;
} void dfs(int x, int sum) {
if(sum > t) return;
if(x == n + 1) {
if(sum == t) {
ans[sz].len = 0;
for(int i = 1; i <= n; i ++) {
if(key[i]) {
ans[sz].b[ans[sz].len ++] = a[i];
}
}
sz ++;
}
return;
}
key[x] = 1;
dfs(x + 1, sum + a[x]);
key[x] = 0;
dfs(x + 1, sum);
} int main() {
while(~scanf("%d %d", &t, &n)) {
if(n == 0)
break; for(int i = 1; i <= n; i ++) {
scanf("%d", &a[i]);
} sort(a + 1, a + 1 + n, cmp);
sz = 0;
dfs(1, 0);
printf("Sums of %d:\n", t);
if(sz) {
sort(ans, ans + sz, cmp2);
for(int i = 0; i < sz; i ++) {
int fail = 1;
if(i == 0) fail = 0;
else {
if(ans[i].len != ans[i - 1].len) fail = 0;
for(int j = 0; j < ans[i].len; j ++) {
if(ans[i].b[j] != ans[i - 1].b[j])
fail = 0;
}
}
if(fail)
continue; for(int j = 0; j < ans[i].len; j ++) {
if(j != 0) printf("+");
printf("%d", ans[i].b[j]);
}
printf("\n");
}
} else {
printf("NONE\n");
}
}
return 0;
}
ZOJ 1711 H-Sum It Up的更多相关文章
- poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)
Sum It Up Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)
题目链接:http://poj.org/problem?id=1564 题目大意:给定一个整数t,和n个元素组成的集合.求能否用该集合中的元素和表示该整数,如果可以输出所有可行解.1<=n< ...
- poj1564 Sum It Up (zoj 1711 hdu 1258) DFS
POJhttp://poj.org/problem?id=1564 ZOJhttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=711 ...
- POJ 1775 (ZOJ 2358) Sum of Factorials
Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematic ...
- zoj 3813 Alternating Sum(2014ACMICPC Regional 牡丹江站网络赛 E)
1.http://blog.csdn.net/dyx404514/article/details/39122743 思路:题目意思很清楚了,这里只说思路. 设区间[L,R],区间长度为len=(R-L ...
- URAL 1146 Maximum Sum 最大子矩阵和
题目:click here #include <bits/stdc++.h> using namespace std; typedef unsigned long long ll; con ...
- Uva10290 - {Sum+=i++} to Reach N
Problem H {sum+=i++} to Reach N Input: standard input Output: standard output Memory Limit: 32 MB A ...
- ZOJ 2059 The Twin Towers
双塔DP. dp[i][j]表示前i个物品,分成两堆(可以不全用),价值之差为j的时候,较小一堆的价值为dp[i][j]. #include<cstdio> #include<cst ...
- [leetcode-560-Subarray Sum Equals K]
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
随机推荐
- CAT 安装运行配置教程
CAT安装教程 首先安装mysql数据库,具体步骤参阅<mysql免安装教程>--http://www.cnblogs.com/halberts/p/8723938.html 下载CAT代 ...
- Linux3.5—IIC学习分析
I2C控制器的设备对象内核已经实现并关联到platform总线. I2C控制器的驱动对象内核已经实现. 看mach-tiny4412.h /plat-samsung/目录下 /drivers/i2c/ ...
- 利用主成分分析(PCA)简化数据
一.PCA基础 线性映射(或线性变换),简单的来说就是将高维空间数据投影到低维空间上,那么在数据分析上,我们是将数据的主成分(包含信息量大的维度)保留下来,忽略掉对数据描述不重要的成分.即将主成分维度 ...
- python基础的一些知识点
ord 将字符转换为ASCIIchr 将ASCII转换为字符 元组不可修改,当只有一个元素时,要添加一个逗号集合不可修改,元素无序,不能重复 列表.元组.字典都是可迭代对象,就是可以遍历的对象多层循环 ...
- 20145202mc《计算机病毒》实践2
网站检测 http://www.virustotal.com太慢了实在,所以我换成了http://www.virscan.org/ lab01-01.exe 文件行为 lab01-01.dll 可以基 ...
- 《图说VR入门》——googleVR入门
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/52959035 作者:car ...
- 广州Uber优步司机奖励政策(1月4日~1月10日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 【LG4175】[CTSC2008]网络管理
[LG4175][CTSC2008]网络管理 题面 洛谷 题解 感觉就和普通的整体二分差不太多啊... 树上修改就按时间添加,用树状数组维护一下即可 代码 #include<iostream&g ...
- 二进制描述子 BRIEF(ORB), BRISK, FREAK
二进制描述子设计原则体现在三个部分: 采样pattern 方向orientation compensation 配对sampling pairs ORB基于BRIEF: BRISK是用于OKVIS的描 ...
- you selected does not support x86-64 instruction set
centos 安装redis时报you selected does not support x86-64 instruction set 解决方法 make CFLAGS="-march=x ...