D. Nastya and Scoreboard
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 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 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 kk 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 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 sticks 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 sticks. 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 sticks glowing.
Thus, the sequences "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011" encode in sequence all digits from 00 to 99 inclusive.
Output
Print 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 some sort of sequence 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.
题意:n个发光数码管(7段),上面已经有部分管子点亮,现在需要再点亮恰好k个管子,问能组成的最大的数是多少。
分析:比较套路的贪心策略。预处理出每个数码管到(0-9)每个数字的花费是多少,dp[i][j]=1表示从第i个数码管到最后一个可以恰好用完j次点亮,0则不可以。然后就可以从前往后,从大到小选取了,如果选取某个数字后后面可以恰好用完k次点亮,那么该数字一定最优。
#include <bits/stdc++.h>
using namespace std;
int n, k;
string s[10] = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111",
"1111011"};
int cost[2004][10];
int dp[2004][4004];
int main() {
cin >> n >> k;
string t;
for (int i = 1; i <= n; ++i) {
cin >> t;
for (int j = 0; j < 10; ++j) {
int res = 0;
for (int l = 0; l < 8; ++l) {
if (t[l] == '1' && s[j][l] == '0') {
cost[i][j] = -1;
break;
}
if (t[l] == '0' && s[j][l] == '1') {
res++;
}
}
if (cost[i][j] != -1)cost[i][j] = res;
}
}
dp[n + 1][0] = 1;
for (int i = n; i >= 1; --i) {
for (int l = 0; l < 10; ++l) {
if (cost[i][l] == -1)continue;
for (int j = 0; j <= k; ++j) {
if (dp[i + 1][j] == 1)dp[i][j + cost[i][l]] = 1;
}
}
}
string ans;
for (int i = 1; i <= n; ++i) {
for (int j = 9; j >= 0; --j) {
if (cost[i][j] == -1)continue;
if (k < cost[i][j])continue;
if (dp[i + 1][k - cost[i][j]] != 1)continue;
ans += '0' + j;
k -= cost[i][j];
break;
}
if (ans == "") {
ans = "-1";
break;
}
}
cout << ans << endl;
}