ZR#1005

ZR#1005

解法:

题解给了一个建图跑最短路的做法,但好像没有必要,因为 $ m $ 没有用,所以直接上完全背包就行了。

CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<bitset>

using namespace std;

#define LL long long
#define N 2010
#define rep(i, a, n)for (int i = a; i < n; i++)
#define per(i, a, n)for (int i = n-1; i >= a; i--)
 
bitset<N> nst, st, zero; 
int n,k,x,vis[N],a[N],m,res; 

int main() {
	scanf("%d%d%d",&k,&n,&res); 
	rep(i, 0, k) {
		scanf("%d",&x); 
		vis[x] = 1; 
	}
	m = 0; 
	rep(i, 0, 1001) {
        if (vis[i])a[m++] = i - n; 
    }
	st[1000] = 1; 
	rep(i, 1, 1001) {
		nst = zero; 
		rep(j, 0, m) {
            if (a[j] > 0) nst |= (st << a[j]); 
            else nst |= (st >> ( - a[j])); 
        }
		st = nst; 
		if(st[1000] == 1) {
			printf("%d\n", i); 
            //system("pause");
			return 0; 
		}
	}
	puts("-1"); 
    //system("pause");
    return 0;
}
posted @ 2019-10-20 21:16  西窗夜雨  阅读(166)  评论(0编辑  收藏  举报