codeforces1341 D. Nastya and Scoreboard(dp + 贪心)

D. Nastya and Scoreboard

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nn (1≤n≤2000)(1≤n≤2000)  — the number of digits on scoreboard and kk (0≤k≤2000)(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.

 

题意:

每个数字由7根显示管组成,按顺序给出每个显示屏现在的状态,0表示显示管关闭,1表示打开;现在从原有的基础上精确地打开 k 个显示管,问是否可以显示出一串数字,如果可以,求出能显示的最大的数字

思路:

(我没有想到dp,爆搜T了……

dp:

dp[ i ][ j ]表示从第 i 个数字开始到最后,点亮 j 个显示管,能否显示出数字。(从后向前)

现预处理出原始的每个显示屏变到某个数字需要点亮的显示管数,或者不可以变成某个数字

贪心时从前向后,从9到0(保证数字尽量大)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1.0);
const int N = 2e3 + 10;

string ss[10] = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
string s[N];
int n, k, cn[N][10];
bool dp[N][N];

void init()
{
    memset(cn, 0, sizeof(cn));
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 0; j < 10; ++j)
        {
            for(int k = 0; k < 7; ++k)
            {
                if(s[i][k] == '1' && ss[j][k] == '0')
                {
                    cn[i][j] = -1;
                    break;
                }
                if(s[i][k] != ss[j][k])
                {
                    cn[i][j]++;
                }
            }
//            cout<<cn[i][j]<<' ';
        }
//        cout<<'\n';
    }
}

bool solve()
{
    memset(dp, 0, sizeof(dp));
    dp[n + 1][0] = 1;
    for(int i = n; i; --i)
    {
        for(int j = 0; j < 10; ++j)
        {
            if(cn[i][j] != -1)
            {
                for(int u = cn[i][j]; u <= k; ++u)
                {
                    dp[i][u] |= dp[i + 1][u - cn[i][j]];
                }
            }
        }
    }
    if(!dp[1][k])
        return 0;
    else
        return 1;
}

int main()
{
    while(~scanf("%d%d", &n, &k))
    {
        for(int i = 1; i <= n; ++i)
        {
            cin >> s[i];
        }
        init();
        if(!solve())
            cout<<-1<<'\n';
        else
        {
            int tmp = k;
            vector<int>vec;
            for(int i = 1; i <= n; ++i)
            {
                for(int j = 9; j >= 0; --j)
                {
                    if(cn[i][j] != -1 && tmp >= cn[i][j] && dp[i + 1][tmp - cn[i][j]])
                    {
                        tmp -= cn[i][j];
                        vec.push_back(j);
                        break;
                    }
                }
            }
            for(int i = 0; i < vec.size(); ++i)
            {
                cout<<vec[i];
            }
            cout<<'\n';
            vec.clear();
        }
    }
    return 0;
}

dfs剪枝:

等会来补

全部评论

相关推荐

面试官人很好!!!刚开始面试的时候其实超级超级紧张,但是面试官会诱导我回答,越聊越轻松。无算法无手撕。无自我介绍1.&nbsp;面试官自我介绍,给我介绍团队部门和业务(一个偏ToD基础设施,一个类claude)2.&nbsp;日常开发中你是怎么用ai的?3.&nbsp;你是怎么进行测试的?(tips:其实面试官从这里开始就想问我workflow,一直在诱导我回答)我:就是让ai编写一些测试脚本去处理正常情况和边界情况面:那你这样子每次手动让ai去编写测试代码不会很繁琐嘛?那如果现在已经有了一个相关的Skill你会怎么用他?我:(叽里呱啦一大堆,我大概讲了一下这个skill应该做什么事情,讲了讲渐进式加载skill)面:那你能确保这个skill一定能执行嘛?我:我们可以用workflow,主流程固定,节点任务交给agent或者llm面:我想听的就是这个,那workflow怎么用呢?我:这确实不知道面:我们可以把workflow写到system&nbsp;prompt中4.&nbsp;你用ai到现在花了多少钱?(一分钱没花,这里还唠了一会)5.&nbsp;项目中的打字机效果是怎么实现的?6.&nbsp;打字机的帧率是多少,是怎么计算的?7.&nbsp;markdown渲染你有没有什么安全措施?怎么防XSS(gfm白名单)8.&nbsp;CSFR攻击9.&nbsp;为什么要用SSE做ai输出,不用websocket?10.&nbsp;项目:ai生成歌曲肯定需要一定的事件,你怎么让用户等待更加舒畅?11.&nbsp;等待进度条的当前task是怎么做的?(SSE&nbsp;自定义事件)12.&nbsp;为什么播放器要全局状态管理?13.&nbsp;假设我现在要做一个效果,就是根据音乐有音波的震动感,你会怎么做,谈谈思路就可以。我:1.用现成的库。2.每隔0.5s或者固定时间对某个节点进行分析音乐特征作为主波频,小波频可以用噪声模拟。面:音乐文件是可以解析成数组的,里面有音乐相关的特征信息(感觉运气很好啊,因为面试官也做过类似的项目所以比较感兴趣)14.&nbsp;为什么你的项目要用Next.js,这里我吐槽了一波SSR很恶心,很多浏览器的api都不能用,面试官告诉我那就是SSR用的地方不对,确实醍醐灌顶了,有高交互的地方确实不应该也不适合使用SSR15.&nbsp;你知道哪些性能指标呢?你是怎么监测的呢?16.&nbsp;&nbsp;我看你简历上写了擅长性能优化,跟我说说你怎么做的吧。(因为我们的服务器很差,我做了超级多的性能优化,我个人觉得这里答得蛮不错的,面试官说很少人会写这个,答出来会很加分)17.&nbsp;还有一些个别问题吧,刚开始的时候太紧张了,确实不记得了录音了。。。18.&nbsp;你觉得ai会取代前端嘛?19.&nbsp;你说你更喜欢美学和设计之类的,你了解过哪些动画库或者UI库嘛?20.&nbsp;通知一面过了(真的开心坏了哈哈哈哈哈哈哈)反问:1.&nbsp;如果能顺利通过面试的话,老师您更推荐我去哪个部门?2.&nbsp;大厂的开发流程协作模式是怎么样的?3.&nbsp;我现在用ai有个问题,日常开发中使用ai,如果代码是ai写的我会有深深的愧疚感,但是又不能不用。用了ai写代码,代码能力就慢慢地下降了,导致进一步使用ai,形成恶性循环,不知道老师您是怎么看的?(随个简历和下一场面试邀请)
晨时念:羡慕期待明天我也是这样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务