ZOJ2955 Interesting Dart Game DP

Recently, Dearboy buys a dart for his dormitory, but neither Dearboy nor his roommate knows how to play it. So they decide to make a new rule in the dormitory, which goes as follows:

  1. Given a number N, the person whose scores accumulate exactly to N by
    the fewest times wins the game.
  2. Notice once the scores accumulate to more than N, one loses the game.
  3. Now they want to know the fewest times to get the score N.

So the task is :
Given all possible dart scores that a player can get one time and N, you are required to calculate the fewest times to get the exact score N.

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number of test cases. And it will be followed by T consecutive test cases.
Each test case begins with two positive integers M(the number of all possible dart scores that a player can get one time) and N. Then the following M integers are the exact possible scores in the next line.
Notice: M (0 < M < 100), N (1 < N <= 1000000000), every possible score is (0, 100).

Output
For each test case, print out an integer representing the fewest times to get the exact score N.
If the score can’t be reached, just print -1 in a line.

Sample Input

3
3 6
1 2 3
3 12
5 1 4
1 3
2

Sample Output

2
3
-1

这个题大概是给你一个n和m,n是一次能得哪些分,m是你要凑出来得分,求最少几次就能凑出来,凑不出来-1。
这个题给的数据范围很大,但其实并没有多少,因为这个数能凑出来的话,在10k前就能凑出来,10k前凑不出来就是凑不出来了。所以,把m用最大值减到10k以内,跑一个背包求到某个数的最小次数就可以了。
这个题是今天整理题的时候翻出来的,好像当时没整理是因为不知道为什么在10k之前能凑出来,当然了,现在还是不知道。。。


#include <stdio.h>
#include <climits>
#include <cstring>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>

#define INF 0x3f3f3f3f
#define ll long long
#define Pair pair<int,int>
#define re return

#define mem(a,b) memset(a,b,sizeof(a))
#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;

int store[105];
int dp[10105];
inline bool cmp(const int &a,const int &b);
int main(){
    ios::sync_with_stdio(false);

    int k;
    cin>>k;
    while(k--){
        ll M,N;
        cin>>M>>N;
        rep(i,0,M)
            cin>>store[i];

        ll ans=0;
        sort(store,store+M,cmp);
        int lim=10000;

        mem(dp,INF);
        drep(i,M-1,0)
            dp[store[i]]=1;

        rep(i,store[M-1],10001){
            rep(j,0,M)
                dp[i+store[j]]=min(dp[i+store[j]],dp[i]+1);
        }

        if(lim<N){
            ans+=(N-lim)/store[0];
            N-=1LL*ans*store[0];
        }
        if(dp[N]!=INF)
            cout<<dp[N]+ans<<endl;
        else
            cout<<"-1"<<endl;

    }
    re 0;
}
inline bool cmp(const int &a,const int &b){
    re a>b;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值