CodeForces-1341D Nastya and Scoreboard


传送门:CodeForces-1341D

思路分析

当前状态可以通过加灯来转移到新的状态
预处理出每个状态能转移的状态集合,可以通过枚举子集的方法实现
设$dp[i][j]=0/1$表示前$i-1$个字符串加了$k$个字符能够是否组成数字
倒着dp,初始dp[n+1][k]=1,如果最后dp[1][0]=0,说明不合法
dp的过程其实是一个可行性背包问题$dp[i][j]|=dp[i+1][j+val]$
答案贪心就行了

样例输入

2 5
0010010
0010010

样例输出

97

AC代码

#include <functional>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <vector>
#include <string>
#include <cstdio>
#include <chrono>
#include <random>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#if __cplusplus >= 201103L
#include <unordered_map>
#include <unordered_set>
#endif
#define ls x<<1
#define rs x<<1|1
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
    bool IOerror=0;
    inline char nc() {
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if(p1==pend) {
            p1=buf;
            pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if(pend==p1) {
                IOerror=1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
    }
    template<class T> inline bool read(T &x) {
        bool sign=0;
        char ch=nc();
        x=0;
        for(; blank(ch); ch=nc());
        if(IOerror)return false;
        if(ch=='-')sign=1,ch=nc();
        for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
        if(sign)x=-x;
        return true;
    }
    template<class T,class... U>bool read(T& h,U&... t) {
        return read(h)&&read(t...);
    }
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N=2e3+10;

int dp[N][N];
string a[N];
int s[10]= {119,18,93,91,58,107,111,82,127,123};
vector<int>ste[200];

void init() {
    for(int i=9; i>=0; i--) {
        for(int j=s[i]; j!=0; j=(j-1)&s[i]) ste[j].push_back(i);
        ste[0].push_back(i);
    }
}

int Count(int n) {
    int count = 0;
    n=s[n];
    while(n) {
        ++count;
        n = n & (n - 1);
    }
    return count;
}


int main() {

#ifdef xiaofan
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif

    int n,k;
    cin>>n>>k;
    init();
    for(int i=1; i<=n; i++) cin>>a[i];
    dp[n+1][k]=1;
    for(int i=n; i>=1; i--) {
        int sum=0,cnt=0;
        for(auto x:a[i]) {
            sum<<=1;
            sum+=x-'0';
            if(x-'0'==1) cnt++;
        }
        for(auto x:ste[sum]) {
            int val=Count(x)-cnt;
            for(int j=k; j>=0; j--) dp[i][j]|=dp[i+1][j+val];
        }
    }
    if(!dp[1][0]) return cout<<-1,0;
    string ans;
    int used=0;
    for(int i=1; i<=n; i++) {
        int sum=0,cnt=0;
        for(auto x:a[i]) {
            sum<<=1;
            sum+=x-'0';
            if(x-'0'==1) cnt++;
        }
        for(auto x:ste[sum]) {
            int val=Count(x)-cnt;
            if(dp[i+1][used+val]) {
                ans.push_back('0'+x);
                used+=val;
                break;
            }
        }
    }
    cout<<ans<<endl;




    return 0;
}


文章作者: 小凡
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小凡 !
评论
  目录
隐藏
{% if theme.sakura.enable %}{% endif %}