Codeforces Beta Round #96 (Div. 1) D. Constants in the language of Shakespeare 贪心
D. Constants in the language of Shakespeare
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/132/problem/D
Description
Shakespeare is a widely known esoteric programming language in which programs look like plays by Shakespeare, and numbers are given by combinations of ornate epithets. In this problem we will have a closer look at the way the numbers are described in Shakespeare.
Each constant in Shakespeare is created from non-negative powers of 2 using arithmetic operations. For simplicity we'll allow only addition and subtraction and will look for a representation of the given number which requires a minimal number of operations.
You are given an integer n. You have to represent it as n = a1 + a2 + ... + am, where each of ai is a non-negative power of 2, possibly multiplied by -1. Find a representation which minimizes the value of m.
Input
Output
Output the required minimal m. After it output m lines. Each line has to be formatted as "+2^x" or "-2^x", where x is the power coefficient of the corresponding term. The order of the lines doesn't matter.
Sample Input
1111
Sample Output
3
HINT
题意
给你一个2进制的数,然后要求你由+2^x和-2^x来构成这个数
使得需求的数最少
题解:
感觉好像是DP的样子,但是我DP灰常鶸,那就贪心咯
对于每一段连续的1,我们可以一个一个的点,也可以点开头然后灭掉结尾,很显然,当长度大于等于2的时候,第二种策略更加优秀
但是这儿有一个坑点,11101111,这个数据,答案是3
所以我们还要合并一次就行了~
代码:
//qscqesze #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <bitset> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define maxn 1100000 #define mod 1001 #define eps 1e-9 #define pi 3.1415926 int Num; //const int inf=0x7fffffff; const ll inf=999999999; inline ll read() {ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f; } //*************************************************************************************string s; int flag[maxn]; int main() {cin>>s;int n = s.size();for(int i=0;i<n;i++)if(s[i]=='1')flag[n-1-i]=1;for(int i=0;i<n;i++){if(flag[i]==0)continue;int j = i;while(flag[j])j++;if(j-i>=2){flag[i]=-1;for(int k=i+1;k<=j;k++)flag[k]=0;flag[j]=1;}i=j-1;}int tot=0;for(int i=0;i<=n;i++)if(flag[i]==1||flag[i]==-1)tot++;printf("%d\n",tot);for(int i=0;i<=n;i++){if(flag[i]==0)continue;if(flag[i]==1)printf("+2^%d\n",i);elseprintf("-2^%d\n",i);} }
Codeforces Beta Round #96 (Div. 1) D. Constants in the language of Shakespeare 贪心相关推荐
- Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树。单点更新
http://codeforces.com/problemset/problem/91/B 题意: 给你n个数,求得i 到n中小于a[i]的最右边的a[j],然后求a[i]到a[j]之间包含了多少个数 ...
- Codeforces Beta Round #22 (Div. 2 Only) E. Scheme(DFS+强连通)
题目大意 给了 n(2<=n<=105) 个点,从每个点 u 出发连向了一个点 v(共 n 条边) 现在要求添加最少的边使得整个图是一个强连通图 做法分析 这道题千万不要一般化:先求强连通 ...
- Codeforces Beta Round #4 (Div. 2 Only)
Codeforces Beta Round #4 (Div. 2 Only) A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 ...
- Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力
A. Prime Permutation 题目连接: http://www.codeforces.com/contest/123/problem/A Description You are given ...
- Codeforces Beta Round #9 (Div. 2 Only) D. How many trees? dp
D. How many trees? 题目连接: http://www.codeforces.com/contest/9/problem/D Description In one very old t ...
- Codeforces Beta Round #14 (Div. 2) B. Young Photographer 水题
B. Young Photographer 题目连接: http://codeforces.com/contest/14/problem/B Description Among other thing ...
- Codeforces Beta Round #9 (Div. 2 Only) C. Hexadecimal's Numbers dfs
C. Hexadecimal's Numbers 题目连接: http://www.codeforces.com/contest/9/problem/C Description One beautif ...
- Codeforces Beta Round #16 (Div. 2 Only)【未完结】
2022.3.9 题目地址:https://codeforces.com/contest/16 目录 A. Flag[模拟] B. Burglar and Matches[贪心] C. Monitor ...
- Codeforces Beta Round #14 (Div. 2)【未完结】
2022.3.8 题单地址:https://codeforces.com/contest/14 目录 A. Letter B. Young Photographer[差分] C. Four Segme ...
最新文章
- R语言绘制waffle chart
- 创建3层的服务模板 (1)--- 概述
- oracle数据库简单操作
- 视图解析自定义视图和自定义视图解析器——未完待续
- java 动态规划视频_157-动态规划算法解决背包问题1
- python基础之---03基本语法
- 关于业务用例抽象问题对网友的回复
- c语言中 文件的字符串输入函数是6,【C语言】文件操作及输入输出格式(文件常用的库函数)...
- 《软件工程》课之-调查问卷的心得体会
- jsp导出数据时离开页面_您应该在要离开的公司开始使用数据
- 第三次小组实践作业小组每日进度汇报:2017-12-2
- python语言适用于哪些领域_Python用于哪些领域
- 基于 Multiple Teacher Single Student 框架的多领域对话模型
- 虚拟主机隐藏index.html,Nginx如何隐藏index.html
- VMware 14 版本激活许可证
- 中彩3d组合王 霏凡软件站
- Python参考文献
- 魔兽争霸3冰封王座十大经典战役全集
- Photoshop学习(一)Ps基础
- 计算机三级网络技术资料分享
热门文章
- Android平台开源项目
- WinForm 应用程序中开启新的进程及控制
- 【更新指南】Aspose.CAD for Java更新至v19.5版本,实施导出的“警告”通知机制!...
- 每周一个 Python 模块 | time
- 深入理解PHP之isset和array_key_exists对比
- 英语发音规则---N字母
- Centos6 破解系统密码
- greenplum安装札记(待完善)
- 中国移动物联网客户规模突破2600家 联网接入终端达2300万台
- 使用python抓取百度漂流瓶妹纸照片