Codeforces Round #637 (Div. 2) - Thanks, Ivan Belonogov! 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, 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.

bed1234ad2f319542a635519d6e40510e65f641b.pnguploading.4e448015.gif轉存失敗重新上傳取消

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.

題意:給你從0~9的數字編碼(只包含0或1,長度均爲7)。現在有n(n<=2000)個長度爲7的編碼。本來全是數字的編碼,但是其中某些編碼中,1變成了0。

你需要將正好k(k<=2000)個0變回1,求原來的數字最大是多少。

思路:出題人出這種難度的題目剛好就是給我這種程度的菜雞練的。。。

首先,很容易想到dp[i][j]表示前i個編碼,用了j個1,能得到的最大數字。預處理一下每個7位01編碼分別變成題目所給0~9需要變回1的數目。然後直接枚舉j進行狀態轉移就可以。

由於數字長度過大(最高2000位,前導零不能去掉),我用dp數組使用string類型,榮獲MLE一發。

發現i可以改爲滾動數組,榮獲TLE一發。

冷靜了一會兒,發現我們其實沒有必要記錄最大的數字。假設到dp[i][j]時我們已經獲得了最大數字,那麼下一個dp[i+1][j']就直接優先從這個dp[i][j]轉移即可(第dp[i+1][]優先從dp[i][j]轉移,用一個vector記錄優先獲得的j'),並且對於相同的j',我們只取首先轉移過來的狀態的數字即可。這樣第一個轉移到dp[n][m]的一定是(字典序)最大的數。

其實本質就是,我們貪心按照字典序來進行狀態轉移。每次優先把上一個先轉移的、數字最大的進行狀態轉移。再用一個pre[i][j]數組記錄一下上一次的狀態即可(因爲最後還要輸出高達2000位的答案)

細品,一定要細品。

代碼:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
using namespace std;
const int maxn=2e3+5;
//const double pi=acos(-1.0);
//const double eps=1e-9;
//const ll mo=1e9+7;
int n,m,k;
char s[maxn];
int dp[maxn][maxn],pre[maxn][maxn];
int count(int x){
    int sum=0;
    while(x){
        sum++;
        x&=(x-1);
    }
    return sum;
}
int a[]={119,18,93,91,58,107,111,82,127,123};
int zt[maxn][maxn];
vector<int>vc[maxn];
void print(int i,int zt){
    if(i==1) printf("%d",dp[i][zt]);
    else {
        print(i-1,pre[i][zt]);
        printf("%d",dp[i][zt]);
    }
}
int main(){
    rep(i,0,127)
    {
        dep(j,9,0)
        if((a[j]|i)==a[j])
        {
            zt[i][a[j]]=count(a[j])-count(i);
            vc[i].push_back(j);
        }
        else zt[i][a[j]]=-inf;
    }
    scanf("%d%d",&n,&m);
    rep(i,0,n)
    rep(j,0,m)
    dp[i][j]=-1;
    dp[0][0]=0;
    int ct=0;
    vector<int>vj,vv;
    vv.push_back(0);
    rep(i,1,n){
        scanf("%s",s);
        int ans=0,sum=1;
        dep(k,6,0) {
            if(s[k]&1) ans+=sum;
            sum<<=1;
        }
        vj=vv;
        vv.clear();
        for(int jj=0;jj<vj.size();jj++) {
            int j=vj[jj];
            for(int k=0;k<vc[ans].size();k++){
                int nk=vc[ans][k];
                if(j+zt[ans][a[nk]]>m) continue;
                if(dp[i-1][j]==-1) continue;
                if(dp[i][j+zt[ans][a[nk]]]!=-1) continue;
                dp[i][j+zt[ans][a[nk]]]=nk;
                vv.push_back(j+zt[ans][a[nk]]);
                //cout<<i<<" "<<j+zt[ans][a[nk]]<<" "<<ans<<" "<<zt[ans][a[nk]]<<" "<<nk<<endl;
                pre[i][j+zt[ans][a[nk]]]=j;
                //cout<<i<<" "<<j<<" "<<zt[ans][a[nk]]<<" "<<dp[i][j]<<endl;
            }
        }
    }
    if(dp[n][m]==-1) puts("-1");
    else {
        print(n,m);
        printf("\n");
    }

    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章