Nastya and Scoreboard(dp+贪心)

14 篇文章 0 订阅

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 7 segments, which can be turned on or off to display different numbers. The picture shows how all 10 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 k 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 k sticks (which are off now)?

It is allowed that the number includes leading zeros.

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

The next n lines contain one binary string of length 7, the i-th of which encodes the i-th digit of the scoreboard.

Each digit on the scoreboard consists of 7 segments. We number them, as in the picture below, and let the i-th place of the binary string be 0 if the i-th stick is not glowing and 1 if it is glowing. Then a binary string of length 7 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 0 to 9 inclusive.

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

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

Note
In the first test, we are obliged to include all 7 sticks and get one 8 digit on the scoreboard.

In the second test, we have sticks turned on so that units are formed. For 5 of additionally included sticks, you can get the numbers 07, 18, 34, 43, 70, 79, 81 and 97, of which we choose the maximum — 97.

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

思路
将输入的二进制串与10个数字串转为十进制储存,若输入串与数字串进行与操作等于输入串,意味着该输入串可以转为相应数字串,利用__builtin_popcount即可计算转换消耗,设定dp[i][j],表示第i位时剩余j次机会是否可以表示为一个数字,利用dp递推计算,每次寻找数字由大到小即可

代码实现

#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=2e3+5;
const int M=3e5;
const int INF=0x3f3f3f3f;
const ll LINF=1e18;
const ull sed=31;
const ll mod=998244353;
const double eps=1e-7;
const double PI=3.14159265358979;
typedef pair<int,int>P;
typedef pair<double,double>Pd;
 
template<class T>void read(T &x)
{
    x=0;int f=0;char ch=getchar();
    while(ch<'0'||ch>'9') {f|=(ch=='-');ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    x=f?-x:x;
    return;
}

int n,m;
char num[10][10]={"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
char s[10];
int iv[N],v[10];
bool vis[N][N];

int main()
{
    read(n);read(m);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",s);
        for(int j=0;j<7;j++) if(s[j]=='1') iv[i]|=(1<<j);
    }
    for(int i=0;i<10;i++)
        for(int j=0;j<7;j++) if(num[i][j]=='1') v[i]|=(1<<j);
    vis[n+1][0]=true;
    for(int i=n;i;i--)
    {
        for(int j=9;j>=0;j--)
        {
            if((v[j]&iv[i])==iv[i])
            {
                int dif=__builtin_popcount(v[j]^iv[i]);
                for(int k=dif;k<=m;k++) vis[i][k]|=vis[i+1][k-dif];
            }
        }
    }
    if(!vis[1][m])
    {
        puts("-1");
        return 0;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=9;j>=0;j--)
        {
            if((v[j]&iv[i])==iv[i])
            {
                int dif=__builtin_popcount(v[j]^iv[i]);
                if(vis[i+1][m-dif])
                {
                    putchar(j+'0');
                    m-=dif;
                    break;
                }
            }
        }
    }
    puts("");
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值