SCU 3172-Fisherman(0-1背包)

来源:互联网 发布:凉宫春日 知乎 编辑:程序博客网 时间:2024/05/29 13:29
D - Fisherman
Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit Status Practice SCU 3172
Appoint description: 

Description

Time Limit: 1000 MS    Memory Limit: 65536 K 

Description

Fisherman Zerg has caught N fish and brought them to the fish market. The people from fish market pack the fish in packages with different weights. Each package can have one or more fish. Suppose you are the first customer and you can choose the weight of your package. How many possible different weights of packages exist with these N fish?

Input

The first line of the input will be a integer to represent the number of test cases.For each test case there is two lines. The first line contains only one integer N.The second line contains N integers representing the weight of each fish.( 1 <= N <= 30 , 1 <= weight <= 1000 )There is a blank line before each test case.

Output

For each test case output the answer on a single line.

Sample Input

21505800 200 354 18 182

Sample Output

127
题意:
   这题说有n条鱼,问你装【1,n】条鱼可以组成多少个不同的重量。
思路:
这题很明显是0-1背包,一开始将每条鱼的重量赋值了,所以答案不正确。但是方法是这样做,最后发现是赋值先会对以后的操作造成影响,所以就将赋值放在了最后面,没想到竟然成功A了。
AC代码:
#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<cstdio>#include<cmath>#include<ctime>#include<cstdlib>#include<queue>#include<vector>#include<set>using namespace std;const int T=30150;#define inf 0x3f3f3f3fL#define mod 1000000000typedef long long ll;typedef unsigned long long ULL;bool v[T];int main(){#ifdef zsc freopen("input.txt","r",stdin); #endifint T,n,i,j,k;scanf("%d",&T);while(T--){memset(v,false,sizeof(v));scanf("%d",&n);for(i=0;i<n;++i){scanf("%d",&k);for(j=30000;j>=k;--j){v[j] |= v[j-k];}v[k] = true;}for(i=0,k=0;i<=30000;++i){if(v[i])k++;}printf("%d\n",k);}    return 0;}


0 0
原创粉丝点击