题:

  OvO http://codeforces.com/contest/946/problem/E

  CF 946E 

解:

  记读入串为 s ,答案串为 ans,记读入串长度为 len,下标从 1 开始。

  分三种情况讨论

  1.数字位数为奇数,设数字位数为 len,则输出 ( len-1 ) 个 9 即可

  2.数字位数为偶数,其中最高位为,最低位为 或者,其他位为 ,(即 100001,100 这些形式),记数字位数为 len,则输出 ( len-2 ) 个 9 即可

  3.数字位数为偶数,而并非是第二种情况,那么进行一次 dfs 即可

    dfs 中,记处理到第 id 位,进行如下两种情况的匹配

    1.ans[id] = s[id],此时要继续进行为 id+1 位的dfs。

    2.ans[id] < s[id],此时在区间 [0,s[id]-1] 中从大到小寻找合适值,如果找到,那么答案串的 1~id 位已经确定, 而余下的 id+1~len 位可以从前面推导得到,所以不用继续进行 dfs。

dfs部分代码如下(预处理中我已经将读入串s整体值减一)

bool dfs(int id)
{
if(id==len+1) return true;
bool flag;
int pi,now=s[id];
//1
if(cnt[now])
cnt[now]--,ned--,ans[id]=now;
else
cnt[now]++,ned++,ans[id]=now;
if(ned<=len-id+1)
{
flag=dfs(id+1);
if(flag) return true;
}
if(cnt[now])
cnt[now]--,ned--,ans[id]=-1;
else
cnt[now]++,ned++,ans[id]=-1;
//2
while(now-1>=0)
{
now--;
if(cnt[now])
cnt[now]--,ned--,ans[id]=now;
else
cnt[now]++,ned++,ans[id]=now;
if(ned<=len-id+1)
return true;
if(cnt[now])
cnt[now]--,ned--,ans[id]=-1;
else
cnt[now]++,ned++,ans[id]=-1;
}
return false;
}

好♂用的数据

7
89
88
1000
28923845
340011
1001 88
77
99
28923839
339933
99

  

全部代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio> using namespace std; const int M=2e5+44;
const int N=14; char str[M];
int s[M],len;
int cnt[N],ned;
int ans[M]; void fillAns()
{
int pi=len;
for(int i=0;i<=9;i++)
while(cnt[i]--)
ans[pi--]=i;
while(ans[pi]==-1)
ans[pi]=9,pi--;
} bool dfs(int id)
{
if(id==len+1) return true;
bool flag;
int pi,now=s[id];
//1
if(cnt[now])
cnt[now]--,ned--,ans[id]=now;
else
cnt[now]++,ned++,ans[id]=now;
if(ned<=len-id+1)
{
flag=dfs(id+1);
if(flag) return true;
}
if(cnt[now])
cnt[now]--,ned--,ans[id]=-1;
else
cnt[now]++,ned++,ans[id]=-1;
//2
while(now-1>=0)
{
now--;
if(cnt[now])
cnt[now]--,ned--,ans[id]=now;
else
cnt[now]++,ned++,ans[id]=now;
if(ned<=len-id+1)
return true;
if(cnt[now])
cnt[now]--,ned--,ans[id]=-1;
else
cnt[now]++,ned++,ans[id]=-1;
}
return false;
} void solve()
{
memset(cnt,0,sizeof(cnt)); ned=0;
memset(ans,-1,sizeof(int)*(len+22));
dfs(1);
fillAns();
for(int i=1;i<=len;i++)
printf("%d",ans[i]);
puts("");
} int main()
{
int ex0,ex1;
int cas;
scanf("%d",&cas);
for(int icas=1;icas<=cas;icas++)
{
scanf("%s",str+1);
len=strlen(str+1);
for(int i=1;i<=len;i++)
s[i]=str[i]-'0';
int pi=len;
s[pi]--;
while(s[pi]<0)
{
s[pi]+=10;
pi--; s[pi]--;
}
ex0=ex1=0;
for(int i=1;i<=len;i++)
if(s[i]==0) ex0++;
else if(s[i]==1) ex1++;
if(s[1]==0 || (ex0+ex1==len && ex1==1))
{
for(int i=1;i<=len-2;i++)
printf("9");
puts("");
}
else if(len&1)
{
for(int i=1;i<=len-1;i++)
printf("9");
puts("");
}
else
solve();
}
return 0;
} /* 7
89
88
1000
28923845
340011
1001 88
77
99
28923839
339933
99 */

  

Educational Codeforces Round 39 (Rated for Div. 2) 946E E. Largest Beautiful Number的更多相关文章

  1. Educational Codeforces Round 39 (Rated for Div. 2) G

    Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...

  2. #分组背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable

    2018-03-11 http://codeforces.com/contest/946/problem/D D. Timetable time limit per test 2 seconds me ...

  3. Educational Codeforces Round 39 (Rated for Div. 2) B. Weird Subtraction Process[数论/欧几里得算法]

    https://zh.wikipedia.org/wiki/%E8%BC%BE%E8%BD%89%E7%9B%B8%E9%99%A4%E6%B3%95 取模也是一样的,就当多减几次. 在欧几里得最初的 ...

  4. codeforces Educational Codeforces Round 39 (Rated for Div. 2) D

    D. Timetable time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  6. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  7. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  8. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  9. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

随机推荐

  1. plsql中文乱码 显示问号

    输入sql语句select * from V$NLS_PARAMETERS查看字符集,查看第一行value值是否为简体中文 解决方案: 新增环境变量 变量名: NLS_LANG 变量值: SIMPLI ...

  2. 分布式缓存 - hash环/一致性hash

    一 引言 当前memcached,redis这类分布式kv缓存已经非常普遍.我们知道memcached的分布式其实是一种"伪分布式",也就是它的服务器节点之间其实是无关联的,之间没 ...

  3. 【转】spring基础:@ResponseBody,PrintWriter用法

    理解:很多情况我们需要在controller接收请求然后返回一些message. 1.在springmvc中当返回值是String时,如果不加@ResponseBody的话,返回的字符串就会找这个St ...

  4. const关键字的使用——C语言

    一.常规用法 关键字const用来定义只读变量,被const定义的变量它的值是不允许改变的,即不允许给它重新赋值,即使是赋相同的值也不可以.所以说它定义的是只读变量,这也就意味着必须在定义的时候就给它 ...

  5. Python开发【第三章】:编码转换

    一.字符编码与转码 1.bytes和str 之前有学过关于bytes和str之间的转换,详细资料->bytes和str(第四字符串) 2.为什么要进行编码和转码 由于每个国家电脑的字符编码格式不 ...

  6. UOJ208 UOIP十合一(提交答案)

    首先对每张图都去掉自环. 1:给出的就是DAG.答案即为2m. 2.5:显然每个SCC之间互相独立.这两个点都满足SCC中的点很少.于是对每个SCC暴力枚举边集判环,而SCC之间的边显然选不选没有影响 ...

  7. asp.net类似于js中的setTimeOut()的函数作用?

    asp.net类似于js中的setTimeOut()的函数作用? 插入这行即可,定时2秒,再运行下一步: System.Threading.Thread.Sleep(); 加个随机数 Random r ...

  8. 50道高级sql练习题;大大提高自己的sql能力(附具体的sql)

    问题及描述:--1.学生表Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别--2.课程表Course(CID ...

  9. Nature Biotechnology:人类基因研究走近平民 数据是基础解读更重要

    Nature Biotechnology:人类基因研究走近平民 数据是基础解读更重要 5万美元可以做什么?最近,美国斯坦福大学教授斯蒂芬·夸克在国际著名学术期刊<自然·生物技术>发表论文宣 ...

  10. Asp.Net Core 轻松学系列-2从安装环境开始

    Asp.Net Core 介绍     Asp.Net Core是微软新一代的跨平台开发框架,基于 C# 语言进行开发,该框架的推出,意味着微软从系统层面正式进击 Linux 服务器平台:从更新速度开 ...