NYOJ 927 The partial sum problem 【DFS】+【剪枝】
The partial sum problem
- 描写叙述
- One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choose some integers from the N integers and the sum of them is equal to K.
- 输入
- There are multiple test cases.
Each test case contains three lines.The first line is an integer N(1≤N≤20),represents the array contains N integers. The second line contains N integers,the ith integer represents A[i](-10^8≤A[i]≤10^8).The third line contains an integer K(-10^8≤K≤10^8). - 输出
- If Tom can choose some integers from the array and their them is K,printf ”Of course,I can!”; other printf ”Sorry,I can’t!”.
- 例子输入
-
4
1 2 4 7
13
4
1 2 4 7
15 - 例子输出
-
Of course,I can!
Sorry,I can't!
这题非常经典,剪枝的时候要细心。
#include <stdio.h>
#include <stdlib.h>
int n, arr[22], sum, vis[22], ok, count;
const char *sam[] = {"Sorry,I can't!\n", "Of course,I can!\n"}; int cmp(const void *a, const void *b){
return *(int *)a - *(int *)b;
} void DFS(int k){
if(count == sum){
ok = 1; return;
} for(int i = k; i < n; ++i){
if(i && arr[i] == arr[i-1] && !vis[i-1]) //cut
continue;
if(count > sum && arr[i] > 0) return; //cut count += arr[i]; vis[i] = 1;
DFS(i + 1);
if(ok) return;
count -= arr[i]; vis[i] = 0;
}
} int main(){
while(scanf("%d", &n) == 1){
for(int i = 0; i < n; ++i){
scanf("%d", arr + i);
vis[i] = 0;
}
scanf("%d", &sum);
qsort(arr, n, sizeof(int), cmp);
count = ok = 0; DFS(0);
printf(ok ? sam[1] : sam[0]);
}
return 0;
}
NYOJ 927 The partial sum problem 【DFS】+【剪枝】的更多相关文章
- nyoj 927 The partial sum problem(dfs)
描述 One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choo ...
- NYOJ--927--dfs--The partial sum problem
/* Name: NYOJ--927--The partial sum problem Author: shen_渊 Date: 15/04/17 19:41 Description: DFS,和 N ...
- NYoj The partial sum problem(简单深搜+优化)
题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=927 代码: #include <stdio.h> #include & ...
- ACM题目————The partial sum problem
描述 One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choo ...
- The partial sum problem
算法:搜索 描述 One day,Tom's girlfriend give him an array A which contains N integers and asked him:Can yo ...
- 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 ...
- ACdream 1431——Sum vs Product——————【dfs+剪枝】
Sum vs Product Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) S ...
- DFS+剪枝 HDOJ 5323 Solve this interesting problem
题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...
- *HDU1455 DFS剪枝
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
随机推荐
- 【Appium】每次启动是提示安装setting和unlock app的解决办法
进入appium安装目录,C:\Program Files (x86)\Appium\node_modules\appium\lib\devices\android,编辑android.js文件,注释 ...
- 【Henu ACM Round#20 E】Star
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找规律. 1,13,37.... 6n(n-1) + 1 [代码] #include <bits/stdc++.h> # ...
- 关于HttpClient模拟浏览器请求的參数乱码问题解决方式
转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/44407297 http://www.llwjy.com/blogdetail/9 ...
- Light oj 1138 - Trailing Zeroes (III) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】
1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...
- Broadleaf电商系统开发(一) - Broadleaf介绍
Broadleaf Commerce 是一个开源的 Java 电子商务平台,基于 Spring 框架开发.提供一个可靠.可扩展的架构,可进行深度的定制和高速开发. Broadleaf Commerce ...
- 使用Hadoop ACL 控制訪问权限
使用Hadoop ACL 控制訪问权限 一.HDFS訪问控制 hdfs-site.xml设置启动acl <property> <name>dfs.permissions.en ...
- tp5框架知识点
项目包含的关键点,后台,前台. 入口文件. 通用配置文件. 数据库配置文件. 共有文件,css,images,js. 控制器,模型,视图. 共有类. 共有函数. 属性,方法. 命名规范. 命名空间. ...
- BZOJ 3262 cdq分治 OR 树套树
注意判断 三个条件都一样的-- (CDQ分治 其实并不是很难理解 只是想不到--) CDQ分治: //By SiriusRen #include <cstdio> #include < ...
- 洛谷P3807 【模板】卢卡斯定理exgcd
题目背景 这是一道模板题. 题目描述 给定n,m,p(1\le n,m,p\le 10^51≤n,m,p≤105 ) 求 C_{n+m}^{m}\ mod\ pCn+mm mod p 保证P为pri ...
- C++包含头文件时尖括号和双引号区别
原文链接:http://c.biancheng.net/cpp/biancheng/view/66.html 如果你还看一些别的C++教程,那么你可能很早就发现了,有些书上的#include命令写作# ...