码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #300 B

时间:2015-05-02 18:14:42      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:贪心   算法   

B. Quasi Binary

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.
Sample test(s)
Input

9

Output

9
1 1 1 1 1 1 1 1 1

Input

32

Output

3
10 11 11

好题,很好的题~
题意就是在只有01组成的数字中选取最小数量的数组成n。并且输出这些数。
常规的思路很容易看到,不大于1000000的01数才64个,打个表是不是就能过呢?但是问题关键不是问是否能构成n,而是要求输出最少的构成n的数。看似像个背包,但是又不太好背。。因为还要记录最优的中间路程。
但是这个题一个很好地思路就是利用01,把n好好看一下,每一位当做一个数,那么其中最大的数就是结果。敲好把每一位都取一个1,不够的就是0,那么组成的这res个数不刚好就是符合要求的么。OK,答案就是酱紫的。
其实,我觉得解题报告最好不要附带吗。千篇一律的附代码造成的结果就是大家的懒惰。
可是我还是附了。。(^o^)/~ 哈哈

#include<functional>
#include <cstdio>
#include <cstring>
#include<string>
#include <cctype>
#include <cstdlib>
#include <ctime>
#include <climits>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <algorithm>
#define read freopen("q.in","r",stdin)
#define write freopen("q.out","w",stdout)
#define LL long long
#define maxn 1000005
#define mod 9999999999
using namespace std;
int a[10];
int cnt;
void get(int n)
{
    cnt=0;
    while(n)
    {
        a[cnt++]=n%10;
        n/=10;
    }
}

int main()
{
   int n;
   while(~scanf("%d",&n))
   {
       int i,j;
       get(n);
       int res=0;
       for(i=0;i<cnt;i++)
       {
           res=max(res,a[i]);
       }
       cout<<res<<endl;
       for(j=0;j<res;j++)
       {
           i=cnt-1;
           while(a[i]<=0)i--;
           for(;i>=0;i--)
           {
             if(a[i]>0)cout<<"1";
             else cout<<"0";
             if(a[i])
             a[i]--;
            }
           cout<<" ";
       }

       cout<<endl;

   }



}

Codeforces Round #300 B

标签:贪心   算法   

原文地址:http://blog.csdn.net/u010582475/article/details/45440769

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!