AtCoder Beginner Contest 118 D Match Matching 完全背包

Problem Statement

Find the largest integer that can be formed with exactly NN matchsticks, under the following conditions:

  • Every digit in the integer must be one of the digits A1,A2,...,AM(1≤Ai≤9)A1,A2,...,AM(1≤Ai≤9).
  • The number of matchsticks used to form digits 1,2,3,4,5,6,7,8,9 should be 2,5,5,4,5,6,3,7,6 respectively.

Constraints

  • All values in input are integers.
  • 2≤N≤10^4
  • 1≤M≤9
  • 1≤Ai≤9
  • Ai are all different.
  • There exists an integer that can be formed by exactly N matchsticks under the conditions.

Input

Input is given from Standard Input in the following format:

N M
A1 A2 ...... AM

Output

Print the largest integer that can be formed with exactly NN matchsticks under the conditions in the problem statement.

Sample Input 1 Copy

Copy

20 4
3 7 8 4

Sample Output 1 Copy

Copy

777773

The integer 777773777773 can be formed with 3+3+3+3+3+5=203+3+3+3+3+5=20 matchsticks, and this is the largest integer that can be formed by 2020 matchsticks under the conditions.

Sample Input 2 Copy

Copy

101 9
9 8 7 6 5 4 3 2 1

Sample Output 2 Copy

Copy

71111111111111111111111111111111111111111111111111

The output may not fit into a 64-bit integer type.

Sample Input 3 Copy

15 3
5 4 6

Sample Output 3 Copy

654

题意:

给你n根火柴,只能摆m种数字(1到9),问能摆出来的最大的数是多少

摆1,2,3,4,5,6,7,8,9 花费 2,5,5,4,5,6,3,7,6根火柴

思路:

完全背包

dp[i]表示考虑到第i个数字时的方案数

那么,最后贪心最大的数字,输出路径

就是说dp[n]是从谁转移过来的,然后一直判断下去直到0为止

即,dp[n]是否可以可以从dp[n-Wight[A[i]]]唯一转移过来,也就是dp[n] == dp[n-Wight[A[m]]]+1

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e4+7;
int dp[maxn] = {1};
int Wight[10] = {0,2,5,5,4,5,6,3,7,6},A[10];
int n,m;
signed main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)scanf("%d",&A[i]);
    for(int i=1;i<=m;i++)
        for(int j=Wight[A[i]];j<=n;j++)
            dp[j] = max(dp[j],dp[j-Wight[A[i]]]+1);
    sort(A+1,A+1+m);
    while(n){
        if(dp[n] == dp[n-Wight[A[m]]]+1)
            printf("%d",A[m]),n -= Wight[A[m]];
        else m--;
    }
    return 0;
}

 

 

另一种正向的思路,dp[i]表示准确的用掉i根火柴的最大答案

那么dp[i+Wight[A[i]]] = max(dp[i+Wight[A[i]]],dp[i]+value[A[i]])

这样就是正向的写,外层for就是容量了,但我感觉这就不是完全背包了、、

代码链接、

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e4+7;
int n,m;
int Wight[10] = {0,2,5,5,4,5,6,3,7,6},A[10];
vector<string> dp(maxn,"-");
string Max(string a,string b){
    if(a.size() != b.size())return a.size()>b.size()?a:b;
    return a>b?a:b;
}
signed main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)scanf("%d",&A[i]);
    dp[0] = "";
    for(int i=0;i<=n;i++)if(dp[i]!="-")
        for(int j=1;j<=m;j++)if(i+Wight[A[j]]<=n)
            dp[i+Wight[A[j]]] = Max(dp[i+Wight[A[j]]],dp[i]+char(A[j]+'0'));
    cout<<dp[n]<<endl;
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值