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. jQuert DOM操作

    DOM操作 1 内部插入 append(content|fn) 向每个匹配的元素内部追加内容 appendTo(content) 把所有匹配的元素追加到另一个指定的元素元素集合中 prepend(co ...

  2. Elastic Search中Query String常见语法

    1 搜索所有数据timeout参数:是超时时长定义.代表每个节点上的每个shard执行搜索时最多耗时多久.不会影响响应的正常返回.只会影响返回响应中的数据数量.如:索引a中,有10亿数据.存储在5个s ...

  3. 输出单项链表中倒数第k个结点——牛客刷题

    题目描述: 输入一个单向链表,输出该链表中倒数第k个结点 输入.输出描述: 输入说明:1.链表结点个数 2.链表结点的值3.输入k的值 输出说明:第k个结点指针 题目分析: 假设链表长度为n,倒数第k ...

  4. javascript框架(库)

    javascript框架(库)高级JavaScript编程,尤其是复杂的浏览器差异处理,通常是困难和耗时的.为了响应这些调整,出现了许多javascript(helper)库.这些JavaScript ...

  5. OSCP-FristiLeaks

    环境搭建 靶机下载: https://www.vulnhub.com/entry/fristileaks-13,133/ 安装:直接用virtualbox打开 网络桥接 找到靶机IP 虚拟机启动就显示 ...

  6. python视频学习笔记6(名片管理系统开发)

    cards_main.py主函数 cards_tools.py -------------------------------------------------------------------- ...

  7. OGG学习笔记03

    OGG学习笔记03-单向复制简单故障处理 环境:参考:OGG学习笔记02-单向复制配置实例实验目的:了解OGG简单故障的基本处理思路. 1. 故障现象故障现象:启动OGG源端的extract进程,da ...

  8. c#传入类名插入多条数据

    public int Insert<T>(IReadOnlyCollection<T> models) where T : class, new() { int sucess ...

  9. Java学习笔记【五、字符串】

    String类 11种构造,不一一列举 常用方法 s.length() 返回字符串长度 s1.contact(s2) 连接s1.s2 String.format("aaa %f bbb %d ...

  10. 经典i++和++i问题(附带运算符优先级问题)

    转自 https://blog.csdn.net/mustard1020/article/details/79617865 1.i++和++i的区别     (1)i++简单来说就是先用i的值来参加表 ...