CF852A Digits

隔壁yijian大佬写出了正解。那我就写一个随机化大法吧?

我们先考虑一种错误的贪心,每个数字分成一位,使其分割后数字和最小。虽然这样是错的,但是我们发现错误的概率很小,所以我们可以每次随机一个数字一位或者两个数字一位。后者的概率调整在百分之一左右。我们用这样的方法做出第一次分割,剩余的两次分割都每个数字一位即可。最后判断一下是否满足条件,如果不满足就重新跑一遍随机化。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<time.h>
#include<string>
#include<stdlib.h>
#define MAX 100
using namespace std;
char s[200500];
int ans[200500],cnt=0,n,nums;
int data[3][200500],add[3];
string output;
int rd() {
int num=rand()%MAX;
if(num==0)return 2;
else return 1;
}
bool generate() {
cnt=0,nums=0;output.clear();
for(int i=0;i<n;i++) {
if(i!=0)output.push_back('+');
if(i!=n-1&&rd()==2) {
ans[++cnt]=(s[i]-'0')*10+s[i+1]-'0';
output.push_back(s[i]);output.push_back(s[i+1]);
i++;
}
else {
ans[++cnt]=s[i]-'0';
output.push_back(s[i]);
}
}
return 1;
}
void out() {
cout<<output;
printf("\n");
for (int i = add[1]; i >=1; i--) {
printf("%d", data[1][i]);
if (i != 1)printf("+");
}
printf("\n");
for (int i = add[2]; i >=1; i--) {
printf("%d", data[2][i]);
if (i != 1)printf("+");
}
return;
}
int main() {
srand((unsigned)(time(NULL)));
scanf("%d%s",&n,s);
while(generate()) {
long long tot=0,trans;add[0]=add[1]=add[2]=0;
for(int i=1;i<=cnt;i++)tot+=ans[i];trans=tot;tot=0;
while(trans){tot+=trans%10;data[1][++add[1]]=trans%10;trans/=10;}trans=tot;tot=0;
while(trans){tot+=trans%10;data[2][++add[2]]=trans%10;trans/=10;}
if(tot<=9){out();return 0;}
else continue;
}
}

最新文章

  1. struts2 框架处理流程
  2. 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch
  3. [Head First设计模式]策略模式
  4. Centos下安装和配置SVN
  5. 使用liunx部署的心得
  6. [转]Neural Networks, Manifolds, and Topology
  7. Docker Centos安装Mysql5.6
  8. css 中的position z-index em rem zoom 的基本用法
  9. 【Android】源码external/目录中在编译过程中生成的文件列表
  10. NSLog中的%@
  11. SQL语句 &amp; 查询表结构
  12. 如何从硬盘安装fedora 19 (How to install fedora 19 from hard drive, Fedora-19-i386-DVD.iso)
  13. PHPの页面跳转-常见方法
  14. 面向对象程序设计-C++ Type conversion (Static) &amp; Inheritance &amp; Composition【第十二次上课笔记】
  15. JAVA学习第三十六课(经常使用对象API)— Set集合:HashSet集合演示
  16. 将Gradle项目发布到Jcenter和Maven Central
  17. MySQL事务的的介绍及使用
  18. Jenkins高级用法 - Jenkinsfile 介绍及实战经验
  19. 新浪IP库地址
  20. 【sqli-labs】Less17

热门文章

  1. 【转】.Net程序员学习Linux最简单的方法
  2. PIE SDK Alpha通道数据渲染
  3. 关于在linux上部署scrapy的爬虫
  4. Mac切换root用户
  5. 一个很简单的SpringCloud项目,集成Feign、Hystrix
  6. SpringBoot 整合websocket
  7. 【Linux】yum 安装 JDK
  8. Java--8--新特性--串并行流与ForkJoin框架
  9. 随笔记录--RegExp类型
  10. django项目中的ajax分页和条件查询。