B. Nastya and Scoreboard

Denis, after buying flowers and sweets (you will learn about this story in the next task), went to a date with Nastya to ask her to become a couple. Now, they are sitting in the cafe and finally... Denis asks her to be together, but ... Nastya doesn't give any answer.

The poor boy was very upset because of that. He was so sad that he punched some kind of scoreboard with numbers. The numbers are displayed in the same way as on an electronic clock: each digit position consists of 77 segments, which can be turned on or off to display different numbers. The picture shows how all 1010 decimal digits are displayed:

After the punch, some segments stopped working, that is, some segments might stop glowing if they glowed earlier. But Denis remembered how many sticks were glowing and how many are glowing now. Denis broke exactly kk segments and he knows which sticks are working now. Denis came up with the question: what is the maximum possible number that can appear on the board if you turn on exactly kk sticks (which are off now)?

It is allowed that the number includes leading zeros.

Input

The first line contains integer n(1n2000)(1≤n≤2000)  — the number of digits on scoreboard and k(0k2000)(0≤k≤2000)  — the number of segments that stopped working.

The next nn lines contain one binary string of length 77, the ii-th of which encodes the ii-th digit of the scoreboard.

Each digit on the scoreboard consists of 77 segments. We number them, as in the picture below, and let the ii-th place of the binary string be 00 if the ii-th stick is not glowing and 11 if it is glowing. Then a binary string of length 77 will specify which segments are glowing now.

Thus, the sequences "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011" encode in sequence all digits from 00 to 99 inclusive.

Output

Output a single number consisting of nn digits  — the maximum number that can be obtained if you turn on exactly kk sticks or 1−1, if it is impossible to turn on exactly kk sticks so that a correct number appears on the scoreboard digits.

Examples
input
Copy
1 7
0000000
output
Copy
8
input
Copy
2 5
0010010
0010010
output
Copy
97
input
Copy
3 5
0100001
1001001
1010011
output
Copy
-1
Note

In the first test, we are obliged to include all 77 sticks and get one 88 digit on the scoreboard.

In the second test, we have sticks turned on so that units are formed. For 55 of additionally included sticks, you can get the numbers 0707, 1818, 3434, 4343, 7070, 7979, 8181 and 9797, of which we choose the maximum  — 9797.

In the third test, it is impossible to turn on exactly 55 sticks so that a sequence of numbers appears on the scoreboard.

每个从后往前DP,dp[i][j]表示第i位数字使用j个灯泡是否可行

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
//#include <xfunctional>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define pb              push_back
#define mk              make_pair
using namespace std;
int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 998244353;
const int N = 1e6+5;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
ll lcm(ll m, ll n)
{
    return m*n / gcd(m, n);
}
int dif(string a, string b)
{
    int cnt = 0;
    for (int i = 0; i < 7; i++)
    {
        if (a[i] == '1' && b[i] == '0')
            return inf;
        if (a[i] != b[i])
            cnt++;
    }
    return cnt;
}
vector<string> ss = { "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
void f(vector<int> &a,string s)
{
    for (int i = 0; i < 10; i++)
    {
        a[i] = dif(s, ss[i]);
    }
}

int main()
{
    int n, k;    
    cin >> n >> k;
    vector<string> v(n);
    vector<vector<int>> a(n, vector<int>(10,inf));
    vector<vector<int>> dp(n+1,vector<int>(k+1,0));
    for (int i = 0; i < n; i++)
    {
        cin >> v[i];
        f(a[i], v[i]);
    }
    for (int i = 0; i <= 9; i++)
    {
        if (a[n - 1][i] <= k)
            dp[n - 1][a[n - 1][i]] = 1;
    }    
    int used = 0;
    if(n==1)
    {
        for (int i = 9; i >=0 ; i--)
        {
            if (a[0][i] != k && a[0][i]<=k)
                dp[0][a[0][i]] = 0;
            if(a[0][i]==k)
            {
                cout << i << endl;
                return 0;
            }
        }

    }
    for (int i = n-1 ; i > 0; i--)
    {
        for (int j = 0; j <= k; j++)
        {
            for (int t = 0; t < 10; t++)
            {
                if(j + a[i-1][t]<=k)
                    dp[i - 1][j+a[i-1][t]] |= dp[i][j];
            }
        }
    }
    if (!dp[0][k])
    {
        cout << -1 << endl;
        return 0;
    }

    string ans = "";
    for (int i = 0; i < n; i++)
    {
        for (int j = 9; j >= 0; j--)
        {
            if (used + a[i][j] <= k && dp[i+1][k-used-a[i][j]])
            {
                used += a[i][j];
                ans+=j+'0';
                break;
            }
        }
        if (i == n - 1)
        {
            for (int j = 9; j >= 0; j--)
            {
                if (used + a[i][j] == k && dp[i][k - used])
                {
                    used += a[i][j];
                    ans += j + '0';
                    break;
                }
            }
        }
    }
    cout << ans;
    return 0;
}
原文地址:https://www.cnblogs.com/dealer/p/12856259.html