题:

  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整体值减一)

  1. bool dfs(int id)
  2. {
  3. if(id==len+1) return true;
  4. bool flag;
  5. int pi,now=s[id];
  6. //1
  7. if(cnt[now])
  8. cnt[now]--,ned--,ans[id]=now;
  9. else
  10. cnt[now]++,ned++,ans[id]=now;
  11. if(ned<=len-id+1)
  12. {
  13. flag=dfs(id+1);
  14. if(flag) return true;
  15. }
  16. if(cnt[now])
  17. cnt[now]--,ned--,ans[id]=-1;
  18. else
  19. cnt[now]++,ned++,ans[id]=-1;
  20. //2
  21. while(now-1>=0)
  22. {
  23. now--;
  24. if(cnt[now])
  25. cnt[now]--,ned--,ans[id]=now;
  26. else
  27. cnt[now]++,ned++,ans[id]=now;
  28. if(ned<=len-id+1)
  29. return true;
  30. if(cnt[now])
  31. cnt[now]--,ned--,ans[id]=-1;
  32. else
  33. cnt[now]++,ned++,ans[id]=-1;
  34. }
  35. return false;
  36. }

好♂用的数据

  1. 7
  2. 89
  3. 88
  4. 1000
  5. 28923845
  6. 340011
  7. 1001
  8.  
  9. 88
  10. 77
  11. 99
  12. 28923839
  13. 339933
  14. 99

  

全部代码:

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <cstdio>
  6.  
  7. using namespace std;
  8.  
  9. const int M=2e5+44;
  10. const int N=14;
  11.  
  12. char str[M];
  13. int s[M],len;
  14. int cnt[N],ned;
  15. int ans[M];
  16.  
  17. void fillAns()
  18. {
  19. int pi=len;
  20. for(int i=0;i<=9;i++)
  21. while(cnt[i]--)
  22. ans[pi--]=i;
  23. while(ans[pi]==-1)
  24. ans[pi]=9,pi--;
  25. }
  26.  
  27. bool dfs(int id)
  28. {
  29. if(id==len+1) return true;
  30. bool flag;
  31. int pi,now=s[id];
  32. //1
  33. if(cnt[now])
  34. cnt[now]--,ned--,ans[id]=now;
  35. else
  36. cnt[now]++,ned++,ans[id]=now;
  37. if(ned<=len-id+1)
  38. {
  39. flag=dfs(id+1);
  40. if(flag) return true;
  41. }
  42. if(cnt[now])
  43. cnt[now]--,ned--,ans[id]=-1;
  44. else
  45. cnt[now]++,ned++,ans[id]=-1;
  46. //2
  47. while(now-1>=0)
  48. {
  49. now--;
  50. if(cnt[now])
  51. cnt[now]--,ned--,ans[id]=now;
  52. else
  53. cnt[now]++,ned++,ans[id]=now;
  54. if(ned<=len-id+1)
  55. return true;
  56. if(cnt[now])
  57. cnt[now]--,ned--,ans[id]=-1;
  58. else
  59. cnt[now]++,ned++,ans[id]=-1;
  60. }
  61. return false;
  62. }
  63.  
  64. void solve()
  65. {
  66. memset(cnt,0,sizeof(cnt)); ned=0;
  67. memset(ans,-1,sizeof(int)*(len+22));
  68. dfs(1);
  69. fillAns();
  70. for(int i=1;i<=len;i++)
  71. printf("%d",ans[i]);
  72. puts("");
  73. }
  74.  
  75. int main()
  76. {
  77. int ex0,ex1;
  78. int cas;
  79. scanf("%d",&cas);
  80. for(int icas=1;icas<=cas;icas++)
  81. {
  82. scanf("%s",str+1);
  83. len=strlen(str+1);
  84. for(int i=1;i<=len;i++)
  85. s[i]=str[i]-'0';
  86. int pi=len;
  87. s[pi]--;
  88. while(s[pi]<0)
  89. {
  90. s[pi]+=10;
  91. pi--; s[pi]--;
  92. }
  93. ex0=ex1=0;
  94. for(int i=1;i<=len;i++)
  95. if(s[i]==0) ex0++;
  96. else if(s[i]==1) ex1++;
  97. if(s[1]==0 || (ex0+ex1==len && ex1==1))
  98. {
  99. for(int i=1;i<=len-2;i++)
  100. printf("9");
  101. puts("");
  102. }
  103. else if(len&1)
  104. {
  105. for(int i=1;i<=len-1;i++)
  106. printf("9");
  107. puts("");
  108. }
  109. else
  110. solve();
  111. }
  112. return 0;
  113. }
  114.  
  115. /*
  116.  
  117. 7
  118. 89
  119. 88
  120. 1000
  121. 28923845
  122. 340011
  123. 1001
  124.  
  125. 88
  126. 77
  127. 99
  128. 28923839
  129. 339933
  130. 99
  131.  
  132. */

  

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. Yii错误异常处理

    目录 背景 web错误处理 console错误处理 背景 当程序中出现不可预期的错误,比如说除0异常,yii会给我们扔出这个异常信息,由于现在都是读写分离,客户端调你的api,都是协商好的数据格式,如 ...

  2. MySQL 新建用户,为用户授权,指定用户访问数据库

    1.登录MySQL mysql -u root -p 2.添加新用户(允许所有ip访问) create user 'test'@'*' identified by '123456';(test:用户名 ...

  3. 【学习笔记】RMQ-Range Minimum/Maximum Query (区间最小/最大值)

    RMQ是一类询问区间最小/最大值的问题. 这类问题一般分成两类:静态区间(无修改),动态区间(带修改). 对于动态区间查询最大/最小,我们显然可以用线段树来解决…… 那么对于静态区间查询最大/最小的问 ...

  4. js实现——鼠标移动时跟随着一连的小图片

    首先放置一连的image <body> <div><img src="yezi.png" alt="tu"></div ...

  5. # VsCode 配置C++调试运行

    VsCode 配置C++调试运行 打开命令面板快捷键为F1,软件上写的Ctrl+Shift+P似乎没用 先安装插件使得可以运行 先自行在vsc扩展中搜索C++安装C/C++插件 再参考知乎专栏中安装c ...

  6. 搭建apache2.4+php7+mysql+phpmyadmin

    apache2.2不支持php7,会报错 cannot load php7apache2_4.dll into server 前排提示:保证安装文件夹和我的一致可以省事很多哦 下载地址 下载apach ...

  7. 使用寄存器点亮LED(第1节)—GPIO功能框图讲解

    GPIO简介 GPIO 是通用输入输出端口的简称,简单来说就是 STM32 可控制的引脚, STM32 芯片的 GPIO 引脚与外部设备连接起来,从而实现与外部通讯.控制以及数据采集的功能.STM32 ...

  8. Eclipse一些技巧

    1:测试某个测试溢出,修改堆内存大小 // 模拟内存溢出 -Xms10m -Xmx10m private static void mockOOM() { List list = new ArrayLi ...

  9. 设计Qt风格的C++API

    在奇趣(Trolltech),为了改进Qt的开发体验,我们做了大量的研究.这篇文章里,我打算分享一些我们的发现,以及一些我们在设计Qt4时用到的原则,并且展示如何把这些原则应用到你的代码里. 优秀AP ...

  10. vim编辑器中的替换(转)

    转1:https://www.cnblogs.com/david-wei0810/p/6385988.html 转2:https://blog.csdn.net/doubleface999/artic ...