B. Lucky Numbers
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

Lucky number is super lucky if it's decimal representation contains equal amount of digits 4 and 7. For example, numbers 47, 7744, 474477 are super lucky and 4, 744, 467 are not.

One day Petya came across a positive integer n. Help him to find the least super lucky number which is not less than n.

Input

The only line contains a positive integer n (1 ≤ n ≤ 10^100000). This number doesn't have leading zeroes.

Output

Output the least super lucky number that is more than or equal to n.

Examples
input

output

input

output

题意:给你一个数字(位数<=10^100000)求不小于他且只由4,7并且4跟7的个数相等的最小的数字

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
#define MM(a,b) memset(a,b,sizeof(a));
char s[100000+10],s2[100000+10]; int main()
{
string s;
while(cin>>s)
{
int l=s.size();
if(l%2!=0||s>string(l/2,'7')+string(l/2,'4'))
{cout<<string(l/2+1,'4')<<string(l/2+1,'7')<<"\n";continue;}
int n4=l/2,n7=l/2; int flag=0;
string ans;
for(int i=0;i<s.size();i++)
{
int ok=0;
if(n4>0)
{
if(flag||s[i]<'4') ok=1;
else if(s[i]=='4') for(int j=i+1;j<=l;j++)
{
if(j==l) {ok=1;break;}
char ch=j-i<=n7?'7':'4';
if(ch>s[j]) ok=1;
if(ch!=s[j]) break;
}
}
if(ok) {ans+="4";n4--;}
else {ans+="7";n7--;}
if(ans[i]>s[i]) flag=1;
}
cout<<ans<<"\n";
}
return 0;
}

  分析:比赛时主要是比如4500<7744这样需要构造的数字没想出来。其实还是比较简单的,

因为要让数字足够小,所以从左往右贪心的尽可能的用4,如果当前数字<4,那么肯定就用4了,但是如果等于4呢,那就无法通过当前位判断出来了,需要假设当前位放4,那么为了>=数据4500,接下来的一个位数需要尽可能大,那么就用7,比如47,当到达第三位的时候7>0,说明当前位放4虽然跟数据的数值一样,但是是可以通过后面的数字构造出一个比数据大的,所以当前位放4,否则放7

注意string放元素是a+="w";

TTTTTTTTTTTTTT CF 95 B 构造4,7幸运数字 贪心 构造 string的更多相关文章

  1. [洛谷P2567] SCOI2010 幸运数字

    问题描述 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的"幸运号码"是十进制表示中只包含数字6和8的那些号码,比如68,666,888都是&quo ...

  2. 洛谷P3292 [SCOI2016] 幸运数字 [线性基,倍增]

    题目传送门 幸运数字 题目描述 A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以纪念碑的形式矗立在这座城市的正中心,作为城市的 ...

  3. 「SCOI2010」幸运数字

    传送门 Luogu 解题思路 首先构造出所有的幸运数字. 然后考虑一个幸运数字会产生多少贡献. 对于一个数 \(x\),它在区间 \([l,r]\) 内的倍数的个数为 \(\lfloor \frac{ ...

  4. 贪心/构造/DP 杂题选做

    本博客将会收录一些贪心/构造的我认为较有价值的题目,这样可以有效的避免日后碰到 P7115 或者 P7915 这样的题就束手无策进而垫底的情况/dk 某些题目虽然跟贪心关系不大,但是在 CF 上有个 ...

  5. 贪心/构造/DP 杂题选做Ⅱ

    由于换了台电脑,而我的贪心 & 构造能力依然很拉跨,所以决定再开一个坑( 前传: 贪心/构造/DP 杂题选做 u1s1 我预感还有Ⅲ(欸,这不是我在多项式Ⅱ中说过的原话吗) 24. P5912 ...

  6. BZOJ 1853 【Scoi2010】 幸运数字

    Description 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认 为,于是他定义自己的"幸运号码"是十进制表示中只包含数字6和8的那些号码,比如68,666,8 ...

  7. BZOJ 4568 幸运数字

    题目传送门 4568: [Scoi2016]幸运数字 Time Limit: 60 Sec Memory Limit: 256 MB Description A 国共有 n 座城市,这些城市由 n-1 ...

  8. BZOJ 1853: [Scoi2010]幸运数字

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 2117  Solved: 779[Submit][Status] ...

  9. 【BZOJ-4568】幸运数字 树链剖分 + 线性基合并

    4568: [Scoi2016]幸运数字 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 238  Solved: 113[Submit][Status ...

随机推荐

  1. python中全局global和局部nonlocal命名空间

    python中全局global和局部nonlocal命名空间 局部名称空间对全局名称空间的变量可以引用,但是无法改变. count = 1 def func1(): count = 2 print(c ...

  2. Djangon简介

    目录 Djangon简介 MVC与MTV模型 MVC MTV python三大主流web框架 Django的下载与基本命令 基于Django实现的一个简单示例 Django静态文件配置 Django ...

  3. Redis客户端相关

    1.redis是什么 redis是一个开源的.使用C语言编写的.支持网络交互的.可基于内存也可持久化的Key-Value数据库.redis的官网地址,非常好记,是redis.io.目前,Vmware在 ...

  4. MySQL5.7主从-GTID-mysqldump,xtrabackup搭建

    1.两个空库,都是row+gtid,版本为MySQL5.7.22mydb1执行:(dba_user@localhost) [(none)]> show master status;+------ ...

  5. paramiko-ssh-sftp实例

    import paramiko transport = paramiko.Transport(('192.168.71.136', 22)) transport.connect(username='r ...

  6. Active Learning 主动学习

    Active Learning 主动学习 2015年09月30日 14:49:29 qrlhl 阅读数 21374 文章标签: 算法机器学习 更多 分类专栏: 机器学习   版权声明:本文为博主原创文 ...

  7. 区间问题 codeforces 422c+hiho区间求差问

    先给出一个经典的区间处理方法 对每个区间 我们对其起点用绿色标识  终点用蓝色标识 然后把所有的点离散在一个坐标轴上 如下图 这样做有什么意义呢.由于我们的区间可以离散的放在一条轴上面那么我们在枚举区 ...

  8. c语言中gets()的详细用法

    gets从标准输入设备读字符串函数.可以无限读取,不会判断上限,以回车结束读取,所以程序员应该确保buffer的空间足够大,以便在执行读操作时不发生溢出. 从stdin流中读取字符串,直至接受到换行符 ...

  9. C#面向对象17 23种设计模式

    1.简单工厂模式 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  10. Bootstrap页面响应式设计

    关键词:viewport.栅格布局.媒体查询(Media Queries) 一.关于栅格布局的说明: 1.基本图解 extra small devices phones  超小型设备手机small d ...