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=;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************* string s;
int flag[maxn];
int main()
{
cin>>s;
int n = s.size();
for(int i=;i<n;i++)
if(s[i]=='')flag[n--i]=;
for(int i=;i<n;i++)
{
if(flag[i]==)
continue;
int j = i;
while(flag[j])j++;
if(j-i>=)
{
flag[i]=-;
for(int k=i+;k<=j;k++)
flag[k]=;
flag[j]=;
}
i=j-;
}
int tot=;
for(int i=;i<=n;i++)
if(flag[i]==||flag[i]==-)
tot++;
printf("%d\n",tot);
for(int i=;i<=n;i++)
{
if(flag[i]==)
continue;
if(flag[i]==)
printf("+2^%d\n",i);
else
printf("-2^%d\n",i);
}
}
Codeforces Beta Round #96 (Div. 1) D. Constants in the language of Shakespeare 贪心的更多相关文章
- Codeforces Beta Round #96 (Div. 2) E. Logo Turtle dp
http://codeforces.com/contest/133/problem/E 题目就是给定一段序列,要求那个乌龟要走完整段序列,其中T就是掉头,F就是向前一步,然后开始在原点,起始方向随意, ...
- Codeforces Beta Round #96 (Div. 1) C. Logo Turtle —— DP
题目链接:http://codeforces.com/contest/132/problem/C C. Logo Turtle time limit per test 2 seconds memory ...
- Codeforces Beta Round #96 (Div. 2) (A-E)
写份DIV2的完整题解 A 判断下HQ9有没有出现过 #include <iostream> #include<cstdio> #include<cstring> ...
- Codeforces Beta Round #96 (Div. 1) C. Logo Turtle DP
C. Logo Turtle A lot of people associate Logo programming language with turtle graphics. In this c ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
随机推荐
- I.MX6 Android iperf3 porting failed
/***************************************************************************** * I.MX6 Android iperf ...
- win8.1 64 安装用友T3+sql2005-64步骤
1. 环境:win8.1 64 专业版 4G内存 .net framwork 3.5 2.初始过程及所需软件 安装sql2008数据库,安装完T3发现并不支持此数据库,运行T3老是出现连接数据时的 ...
- html的视频插件 (转)
1)jMedia Element是一个基于jQuery/jQuery UI实现的HTML5音频/视频开发工具包.提供非常多的功能来控制页面中的音频和视频内容.当旧的浏览器不兼容HTML5时, ...
- svc 报“由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。“的HTTP 错误 404.3 – Not Found
原因:系统没有默认为IIS注册WCF服务的svc文件的MIME映射. 解决方法:管理员身份运行C:\Windows\Microsoft.NET\Framework\v3.0\Windows Commu ...
- DataTable 分页
#region DataTable 分页 /// <summary> /// Datatable 分页 /// </summary> /// <param name=&q ...
- 【Python】使用python的tornado配合html页面示例
背景:java写的非标加密算法,测试时执行java工程进行解密测试,很不方便. 目的:想写个web页面,使得任何测试人员都可以在输入加密串时得到解密后字段,方便日志查询及字段核对.(额,算法部分就不写 ...
- 清理vs工程文件(python2.7)
本文记录了两种方法,用于对vs目录的清理工作,这两种方法都是用python2.7实现的,一个是基于文件的扩展名,一个是基于文件的大小: 基于文件大小的清理脚本: #-*- coding:utf-8 - ...
- Ext.useShims=true
Extjs的panel中嵌套ActiveX的插件,如PDF,但是Ext控件被遮罩 eg.在panel的tbar中加入下拉框,结果其下拉值看不到,原因就是被PDF给遮住了, 此时只需设置Ext.useS ...
- 【原创】linux命令bc使用详解
最近经常要在linux下做一些进制转换,看到了可以使用bc命令,如下: echo "obase=10;ibase=16;CFFF" | bc 用完以后就对bc进行了进一步的了解, ...
- openstack学习线路指导
原文链接: http://www.aboutyun.com/thread-7225-1-1.html 网上很多hadoop资料,openstack资料相对较少,这里整理一下,帮助初学者尽快入门. 首先 ...