zoukankan      html  css  js  c++  java
  • Codeforces Round #300 Quasi Binary(DP)

    Quasi Binary
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011 — are quasibinary and numbers 2, 12, 900 are not.

    You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.

    Input

    The first line contains a single integer n (1 ≤ n ≤ 106).

    Output

    In the first line print a single integer k — the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.

    In the second line print k numbers — the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.

    Examples
    input
    9
    output
    9
    1 1 1 1 1 1 1 1 1
    input
    32
    output
    3
    10 11 11
    【题意】给出一种定义数,这种数每一位不是0就是1,给你一个n,问你最少可以由多少定义数组成,分别列出这些定义数。
    【分析】简单DP,先预处理出所有可能的数,然后背包。
    #include <bits/stdc++.h>
    #define pb push_back
    #define mp make_pair
    #define vi vector<int>
    #define inf 0x3f3f3f3f
    #define met(a,b) memset(a,b,sizeof a)
    using namespace std;
    typedef long long LL;
    const int N = 1e6+50;
    int n;
    int dp[N],vis[N],pre[N];
    vector<int>vec,ans;
    void init(){
        queue<int>q;
        q.push(1);
        vis[1]=1;
        while(!q.empty()){
            int u=q.front();
            q.pop();
            vec.pb(u);
            int k = u*10+0;
            if(k<=1000000&&!vis[k]){
                q.push(k);
                vis[k]=1;
            }
            k = u*10+1;
            if(k<=1000000&&!vis[k]){
                q.push(k);
                vis[k]=1;
            }
        }
    }
    int main(){
        scanf("%d",&n);
        if(!n){
            printf("1
    0
    ");
            return 0;
        }
        init();
        met(dp,inf);
        dp[0]=0;
        for(int i=1;i<=n;i++){
            for(int x : vec){
                if(i-x>=0&&dp[i-x]+1<dp[i]){
                    dp[i]=dp[i-x]+1;
                    pre[i]=i-x;
                }
            }
        }
        int k=n;
        while(k){
            int s=k-pre[k];
            k=pre[k];
            ans.pb(s);
        }
        printf("%d
    ",dp[n]);
        for(auto x:ans){
            printf("%d ",x);
        }printf("
    ");
        return 0;
    }
  • 相关阅读:
    WebMatrix简介与预览
    使用NuGet增加常见包引用
    用Jquery实现的一个Table的帮助js
    使用AspNetPager进行存储过程分页
    Android之旅AppWidget
    SQL积累
    【问题记录】Asp.net WebApplication和WebSite中用户控件的使用区别
    ActionScript 3.0工厂模式实例
    ActionScript 3.0 实现单态模式
    装饰器模式小结
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/7226099.html
Copyright © 2011-2022 走看看