NYOJ 927 The partial sum problem 【DFS】+【剪枝】

The partial sum problem

时间限制:1000 ms  |  内存限制:65535 KB

难度:2

描写叙述
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;
}
时间: 2024-10-26 23:28:18

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 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 firs

nyoj927 The partial sum problem(dfs)

The partial sum problem 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 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 multi

NYOJ-927 The partial sum problem

The partial sum problem 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 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 multi

NYoj The partial sum problem(简单深搜+优化)

题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=927 代码: 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <math.h> 5 #include <algorithm> 6 #include <iostream> 7 using namespace std; 8

HDU 5323(2015多校3)-Solve this interesting problem(dfs+剪枝)

题目地址:HDU 5323 题意:给一个l,r,表示区间[l,r],问是否存在区间为[0,n]的线段树的节点区间为[l,r],如果有求最小的n,如果没有输出-1. 思路:因为L/(R-L+1)<=2015,按照线段树的性质每次分区间序号扩大两倍,所以可以得出差不多有22层,所以用爆搜就可以,由上把[l,r]区间不断扩张,直到满足l==0为止.顺便剪剪枝. #include <stdio.h> #include <math.h> #include <string.h>

POJ 1564 Sum It Up (DFS+剪枝)

 Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5820   Accepted: 2970 Description 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

DFS+剪枝 HDOJ 5323 Solve this interesting problem

题目传送门 1 /* 2 题意:告诉一个区间[L,R],问根节点的n是多少 3 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - len -1,r]; 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 #include <queue> 10

hdu 1044(bfs+dfs+剪枝)

Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6739    Accepted Submission(s): 1564 Problem Description It is written in the Book of The Lady: After the Creation, the cruel

zoj 2734 Exchange Cards【dfs+剪枝】

Exchange Cards Time Limit: 2 Seconds      Memory Limit: 65536 KB As a basketball fan, Mike is also fond of collecting basketball player cards. But as a student, he can not always get the money to buy new cards, so sometimes he will exchange with his