CF852A Digits
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;
}
}

CF852A Digits的更多相关文章
- [LeetCode] Reconstruct Original Digits from English 从英文中重建数字
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...
- [LeetCode] Remove K Digits 去掉K位数字
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Add Digits 加数字
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- LeetCode 258. Add Digits
Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...
- ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】
FZU 2105 Digits Count Time Limit:10000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- Revolving Digits[EXKMP]
Revolving Digits Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 【LeetCode】Add Digits
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference
最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...
随机推荐
- C#中的System.Type和System.RuntimeType之间的区别
string str = string.Empty; Type strType = str.GetType(); Type strTypeType = strType.GetType(); strTy ...
- Lambda表达式---Day27
函数式编程思想概述 在数学中,函数就是有输入量.输出量的一套计算方案,也就是“拿什么东西做什么事情”.相对而言,面向对象过 分强调“必须通过对象的形式来做事情”,而函数式思想则尽量忽略面向对象的复杂语 ...
- Linux操作:使用grep排除搜索的目录
使用grep时,当一个目录下有一个包含很多文件的目录,但也不想搜索它,怎么办? 使用 --exclude-dir 选项. 单个目录示例: grep -rni 'http' --exclude-dir= ...
- 【转载】C#使用typeof运算符获取对象变量的具体类型Type
在C#的实际类型操作过程中,有时候需要通过typeof方法获取对象的类型对应的Type变量即具体类型,例如在获取DataTable中某一列的具体数据类型的时候,我们就会用到typeof方法来获取具体的 ...
- C# DataTable 转实体对象
DataTable 转实体对象 /// <summary> /// DataTable通过反射获取单个对象 /// </summary> public static T ToS ...
- Java中assert(断言)的使用
Java中assert(断言)的使用 1.Eclipse中默认assert(断言)是关闭,开启方式如下: 简单来说:就是设置一下jvm的参数,参数是-enableassertions或者-ea(推荐) ...
- 利用shell脚本将Oracle服务器中数据定时增量刷新到ftp服务器中
现有需求:将oracle数据库中的数据准实时同步至某ftp服务器中,以便前端应用能定时从ftp服务器目录中取增量数据 方法:将加工脚本写为存储过程,然后利用shell脚本执行该存储过程并将增量数据导出 ...
- Beyond Compare 4解除日期限制,激活版本
修改注册表 1 在搜索栏中输入 regedit ,打开注册表 2 删除项目:计算机\HKEY_CURRENT_USER\Software\Scooter Software\Beyond Compare ...
- django_restframework项目之python虚拟环境配置(一)
虚拟环境的搭建 优点 1.使不同应用开发环境相互独立 2.环境升级不影响其他应用,也不会影响全局的python环境 3.防止出现包管理混乱及包版本冲突 windows 安装 # 建议使用pip3安装到 ...
- linux系统启动报错:[contains a file system with errors, check forced]的解决方法参考
1.解决参考一Press enter for maintenance(or type Control-D to continue):/dev/sda3 contains a file system w ...