Educational Codeforces Round 39 (Rated for Div. 2) 946E E. Largest Beautiful Number
题:
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的更多相关文章
- 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 < ...
- #分组背包 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 ...
- 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 取模也是一样的,就当多减几次. 在欧几里得最初的 ...
- 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 ...
- 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 ...
- 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 ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- 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 ...
随机推荐
- dubbo探究
一 占位 待整理.. 二 问题汇总 1 谈谈dubbo的超时重试 dubbo 启动时默认有重试机制和超时机制.如果在一定的时间内,provider没有返回,则认为本次调用失败.重试机制出现在调用失败时 ...
- java学习基础知识入门
基础入门知识(一) 一.java技术的分类 java按照技术标准和应用场景的不同分为三类,分别是JAVASE.JAVAEE.JAVAME JAVASE : 平台标准版,用于开发部署桌面,服务器以及嵌入 ...
- java虚拟机栈(关于java虚拟机内存的那些事)
<深入理解 java 虚拟机> 读书扩展 作者:淮左白衣 写于 2018年4月13日16:26:51 目录 文章目录 java虚拟机栈是什么 特点 栈帧 局部变量表 什么时候抛出 `Sta ...
- C++操作文件行(读取,删除,修改指定行)
/******************************************************** Copyright (C), 2016-2018, FileName: main A ...
- CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败
今天在使用 C# 操作 Excel 时,一直在报错误: 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下 ...
- POJ1065(Wooden Sticks)--贪心
木棍 时间限制: 1000MS 内存限制: 10000K 提交总数: 27336 接受: 11857 描述 有一堆木棍.每根杆的长度和重量是预先已知的.这些木棍将由木工机器逐一加工.它需要一些 ...
- MySQL5.7.28免安装版配置
下载 安装配置 问题及解决方案 一.下载 找到你要的版本点击“looking for the latest GA version?”切换,我这个安装的是5.7.28,然后往下拉: 找到免安装的压缩包: ...
- Java 之 HashMap 集合
一.HashMap 概述 java.util.HashMap<k,v> 集合 implements Map<k,v> 接口 HashMap 集合的特点: 1.HashMap 集 ...
- ASE19团队项目beta阶段Backend组 scrum2 记录
本次会议于12月5日,19:00在微软北京西二号楼sky garden召开,持续10分钟. 与会人员:Zhikai Chen, Lihao Ran, Xin Kang 请假人员:Hao Wang 每个 ...
- 【Struts2】Json插件使用
一.使用步骤 1.1 引入依赖 1.2 在struts.xml文件中配置 一.使用步骤 1.1 引入依赖 <!-- https://mvnrepository.com/artifact/org. ...