Codeforces Round #300 Quasi Binary(DP)
2 seconds
256 megabytes
standard input
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.
The first line contains a single integer n (1 ≤ n ≤ 106).
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.
- 9
- 9
1 1 1 1 1 1 1 1 1
- 32
- 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+;
- int n;
- int dp[N],vis[N],pre[N];
- vector<int>vec,ans;
- void init(){
- queue<int>q;
- q.push();
- vis[]=;
- while(!q.empty()){
- int u=q.front();
- q.pop();
- vec.pb(u);
- int k = u*+;
- if(k<=&&!vis[k]){
- q.push(k);
- vis[k]=;
- }
- k = u*+;
- if(k<=&&!vis[k]){
- q.push(k);
- vis[k]=;
- }
- }
- }
- int main(){
- scanf("%d",&n);
- if(!n){
- printf("1\n0\n");
- return ;
- }
- init();
- met(dp,inf);
- dp[]=;
- for(int i=;i<=n;i++){
- for(int x : vec){
- if(i-x>=&&dp[i-x]+<dp[i]){
- dp[i]=dp[i-x]+;
- pre[i]=i-x;
- }
- }
- }
- int k=n;
- while(k){
- int s=k-pre[k];
- k=pre[k];
- ans.pb(s);
- }
- printf("%d\n",dp[n]);
- for(auto x:ans){
- printf("%d ",x);
- }printf("\n");
- return ;
- }
Codeforces Round #300 Quasi Binary(DP)的更多相关文章
- Educational Codeforces Round 51 D. Bicolorings(dp)
https://codeforces.com/contest/1051/problem/D 题意 一个2*n的矩阵,你可以用黑白格子去填充他,求联通块数目等于k的方案数,答案%998244353. 思 ...
- Codeforces Round #219 (Div. 1)(完全)
戳我看题目 A:给你n个数,要求尽可能多的找出匹配,如果两个数匹配,则ai*2 <= aj 排序,从中间切断,分成相等的两半后,对于较大的那一半,从大到小遍历,对于每个数在左边那组找到最大的满足 ...
- Codeforces Round #624 (Div. 3)(题解)
Codeforces Round #624 (Div.3) 题目地址:https://codeforces.ml/contest/1311 B题:WeirdSort 题意:给出含有n个元素的数组a,和 ...
- CodeForces - 710E Generate a String (dp)
题意:构造一个由a组成的串,如果插入或删除一个a,花费时间x,如果使当前串长度加倍,花费时间y,问要构造一个长度为n的串,最少花费多长时间. 分析:dp[i]---构造长度为i的串需要花费的最短时间. ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
- Codeforces 536D - Tavas in Kansas(dp)
Codeforces 题目传送门 & 洛谷题目传送门 其实这题本该 2019 年 12 月就 AC 的(详情请见 ycx 发此题题解的时间),然鹅鸽到了现在-- 首先以 \(s,t\) 分别为 ...
- Codeforces Round #249 (Div. 2) (模拟)
C. Cardiogram time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces 295D - Greg and Caves(dp)
题意: 给出一个 \(n \times m\) 的矩阵,需对其进行黑白染色,使得以下条件成立: 存在区间 \([l,r]\)(\(1\leq l\leq r\leq n\)),使得第 \(l,l+1, ...
- Codeforces 467C George and Job(DP)
题目 Source http://codeforces.com/contest/467/problem/C Description The new ITone 6 has been released ...
随机推荐
- HDU 5950Recursive sequence ICPC沈阳站
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- mysql 并发测试
针对上一节做一些针对公司业务的测试. 我们来做一些压力测试. 服务器配置: 操作系统: centos 5.6-64 CPU: 8核 内存: 8G 硬盘:sas 文件系统:linux MySQL:5.6 ...
- 【BZOJ】1609: [Usaco2008 Feb]Eating Together麻烦的聚餐
[算法]动态规划 [题解]DP有个特点(递推的特点),就是记录所有可能状态然后按顺序转移. 最优化问题中DP往往占据重要地位. f[i][j]表示前i头奶牛,第i头改为号码j的最小改动数字,这样每头奶 ...
- Spring Session加Redis(山东数漫江湖)
session是一个非常常见的概念.session的作用是为了辅助http协议,因为http是本身是一个无状态协议.为了记录用户的状态,session机制就应运而生了.同时session也是一个非常老 ...
- Edgware Feign hystrix-dashboard
相关依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring ...
- html meta标签作用
1.概要 标签提供关于HTML文档的元数据.元数据不会显示在页面上,但是对于机器是可读的.它可用于浏览器(如何显示内容或重新加载页面),搜索引擎(关键词),或其他web服务. 必要属性: conten ...
- python基础===Excel处理库openpyxl
openpyxl是一个第三方库,可以处理xlsx格式的Excel文件. 安装: pip install openpyxl 对如下excel进行读取操作,如图: from openpyxl import ...
- C#数据没初始化,使用会报错,可以初始化null
protected void Page_Load(object sender, EventArgs e) { string[] A; if (B== 0) { A = new string[] {1, ...
- 105.Construct Binary Tree from Preorder and Inorder Traversal---《剑指offer》面试6
题目链接 题目大意:根据先序遍历和中序遍历构造二叉树. 法一:DFS.根据模拟步骤,直接从先序和中序数组中找值然后加入二叉树中,即先从先序数组中确定根结点,然后再去中序数组中确定左子树和右子树的长度, ...
- 设计模式之笔记--组合模式(Composite)
组合模式(Composite) 定义 组合模式(Composite),将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有一致性. 组合模式有 ...