CodeForces_1340B Nastya and Scoreboard(动态规划)

Nastya and Scoreboard

time limit per test:1 seconds
memory limit per test:256 megabytes
Problem Description

Denis, who managed to buy flowers and sweets (you will learn about this story in the next task), went to a meeting with Nastya to invite her to become a couple. And so, they sit together in a cafe, chatting. Denis finally offers to be them together, but … Nastya doesn’t give any answer.

The poor boy was very upset due to that. He was so sad that he kicked 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 kick, some sticks stopped working, that is, some sticks might stop glowing if they glowed earlier. But Denis remembered how many sticks were glowing and how many are glowing now. Let exactly k sticks break and we know which sticks are working now. Denis came up with the question: what is the maximum number that can burn 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 n n ( 1 ≤ n ≤ 2000 1≤n≤2000 1n2000) — the number of digits on scoreboard and k k k ( 0 ≤ k ≤ 2000 0≤k≤2000 0k2000) — the number of sticks that stopped working.

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

Each digit on the scoreboard consists of 7 sticks. We number them, as in the picture below, and let the i i i-th place of the binary string be 0 if the i i i-th stick is not glowing and 1 if it is glowing. Then a binary string of length 7 will specify which sticks glowing.
在这里插入图片描述

Thus, the sequences “1110111”, “0010010”, “1011101”, “1011011”, “0111010”, “1101011”, “1101111”, “1010010”, “1111111”, “1111011” encode in sequence all digits from 0 to 9 inclusive.

Output

Print 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 some sort of sequence appears on the scoreboard digits.

Sample Input

2 5
0010010
0010010

Sample Output

97

题意

定义数字0~9由七位二进制码表示,1代表对应位置灯亮,0代表灭。现在有n个七位二进制码。每次可以将这7*n个二进制码中的一个0变为1,需要正好操作m次。求最后能得到的最大数字。

题解:

设数组 d p [ m a x n ] [ m a x n ] dp[maxn][maxn] dp[maxn][maxn], d p [ i ] [ j ] dp[i][j] dp[i][j]代表第i个位置还能进行j次操作,若后续操作能用完k次操作为1,否则为0。
s t r [ i ] str[i] str[i]为第i个7位二进制码,c(i,j)为第i个二进制码变为j需要的操作数,若不能,则返回-1.
初始设 d p [ n ] [ 0 ] = 1 dp[n][0] = 1 dp[n][0]=1,自底向上递推,

对于 0 ≤ k ≤ 9 0\le k\le9 0k9
d p [ i ] [ j ] = = 1 dp[i][j]==1 dp[i][j]==1 && c ( i − 1 , k ) ≥ 0 c(i-1, k)\ge0 c(i1,k)0 && j + c ( i − 1 , k ) ≤ m j+c(i-1,k)\le m j+c(i1,k)m
d p [ i − 1 ] [ j + c ( i − 1 , k ) ] = 1 dp[i-1][j+c(i-1,k)] = 1 dp[i1][j+c(i1,k)]=1

递推完成后,若 d p [ 0 ] [ m ] = 1 dp[0][m] = 1 dp[0][m]=1,则有解,否则无解。
有解:m为需要进行的操作次数,从第一位开始,从大到小枚举当前位置的合适的一个解,若 d p [ i + 1 ] [ m − c ( i , j ) ] = 1 dp[i+1][m-c(i,j)]=1 dp[i+1][mc(i,j)]=1,则当前位数字为 j j j, m − = c ( i , j ) m-=c(i,j) m=c(i,j)

同时可以通过预处理每组c(i,j)的结果降低时间复杂度。

#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<set>
#include<vector>
#include<queue>
#include<iterator>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define eps 1e-7
 
using namespace std;
typedef long long LL;   
typedef pair<int, int> P;
const int maxn = 2020;
const int mod = 3000001;
char str[12][12]={"1110111","0010010","1011101","1011011","0111010",
                    "1101011","1101111","1010010","1111111","1111011"};
char a[maxn][12];
int b[maxn], c[maxn][12];
bool dp[maxn][maxn];
void init();
int knum(int y, int x);

int main()
{
    init();
    int n, m, i, j, k;
    scanf("%d %d", &n, &m);
    for(i=0;i<n;i++){
        scanf(" %s", a[i]);
        b[i] = 0;
        for(j=0;j<7;j++)
            b[i] = b[i]*2+a[i][j]-'0';
    }
    dp[n][0] = 1;
    for(i=n;i>0;i--)
        for(j=0;j<=m;j++)
        if(dp[i][j])
        {
            for(k=0;k<10;k++)
                if(c[b[i-1]][k] != -1 && j+c[b[i-1]][k] <=m)
                    dp[i-1][j+c[b[i-1]][k]] = 1;
        }
    if(dp[0][m] == 0)printf("-1\n");
    else{
        for(i=0;i<n;i++)
            for(j=9;j>=0;j--){
                k = c[b[i]][j];
                if(k != -1 && k <= m && dp[i+1][m-k]){
                    printf("%d", j);
                    m -= k;
                    break;
                }
            }
    }
    return 0;
}
//预处理由状态i变为数字j的状态需要的操作次数。
void init()
{
    int stu = 1<<8, i, j;
    for(i=0;i<stu;i++)
        for(j=0;j<10;j++)
            c[i][j] = knum(i, j);
}

int knum(int y, int x)
{
    int num = 0;
    for(int i=0;i<7;i++)
        if((y&(1<<i)) && str[x][6-i] == '0')return -1;
        else if(!(y&(1<<i)) && str[x][6-i] == '1')num++;
    return num;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值