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 ...
随机推荐
- Hive简介
实验简介 我们本节课程主要介绍 Hive 的相关知识,将会涉及以下内容: Hive 的定义 Hive 的体系结构 Hive 与关系数据库的区别 Hive 的应用场景 Hive 的存储 一.什么是 Hi ...
- Java面试题-Java中的锁
1. 如何实现乐观锁(CAS)?如何避免ABA问题? 答:1)读取内存值的方式实现了乐观锁(比如:SVN系统),方法:第一,比较内存值和期望值:第二,替换内存值为要替换值. 2)带参数版 ...
- 深入理解Android内存管理原理(六)
一般来说,程序使用内存的方式遵循先向操作系统申请一块内存,使用内存,使用完毕之后释放内存归还给操作系统.然而在传统的C/C++等要求显式释放内存的编程语言中,记得在合适的时候释放内存是一个很有难度的工 ...
- 比较const ,readonly, stitac readonly
比较const ,readonly, stitac readonly: const和readonly的值一旦初始化则都不再可以改写: const必须在声明时初始化:readonly既可以在声明时初始化 ...
- C#中父窗口和子窗口之间实现控件互操作
很多人都苦恼于如何在子窗体中操作主窗体上的控件,或者在主窗体中操作子窗体上的控件.相比较而言,后面稍微简单一些,只要在主窗体中创建子窗体的时候,保留所创建子窗体对象即可. 下面重点介绍前一种,目前常见 ...
- [LOJ 1030] Discovering Gold
B - Discovering Gold Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- 使用hibernate tools插件生成POJO
很多时候我们已经设计好了数据库,需要使用hibernate来做数据持久化,因此需要根据数据库中的表结构生成相应的POJO. 本例使用hibernatetools来自动创建pojo. 测试环境:ecli ...
- [Mac][$PATH]如何修改$PATH变量
从 stackoverflow 找到的方法 http://stackoverflow.com/questions/7703041/editing-path-variable-on-mac 首先打开终端 ...
- 刑事案件的构成要素 zt
论刑事案件的构成要素 马忠红 2013-03-22 14:05:33 来源:<中国人民公安大学学报:社会科学版>(京)2012年5期 [内容提要]刑事案件是由诸多要素构成的一个系 统. ...
- TPL
namespace TPLTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } pr ...