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的更多相关文章

  1. [LeetCode] Reconstruct Original Digits from English 从英文中重建数字

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  2. [LeetCode] Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  3. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  4. [LeetCode] Add Digits 加数字

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  5. LeetCode 258. Add Digits

    Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...

  6. ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】

     FZU 2105  Digits Count Time Limit:10000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  7. Revolving Digits[EXKMP]

    Revolving Digits Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. 【LeetCode】Add Digits

    Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  9. 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 ...

随机推荐

  1. Task 小记

    1. 注意 Task 执行的顺序. 如下代码: IQueryable<T> querySet; if (preConditionExpression == null) { querySet ...

  2. 互联网基础知识------OSI七层网络模型梗概

    OSI七层模型 七层网络结构: 应用层.表示层.会话层.传输层.网络层.数据链路层.物理层 一般也作五层 应用层.传输层.网络层.数据链路层.物理层(实体层) 引申问题:TCP/IP协议与http协议 ...

  3. 【05】Kubernets:资源清单(控制器 - ReplicaSet)

    写在前面的话 从上一章节开始,我们一直在学习关于自主式 Pod 的资源清单编写, 但是自主式 Pod 存在着一个问题,就是和我们直接 docker run 运行 docker 容器一样.如果我们想以集 ...

  4. 使用Net Mail发送邮件

    最近用到了发送邮件这个功能,简单记录一下案例.代码如下: using System; using System.Collections.Generic; using System.Linq; usin ...

  5. [世预赛] 中国vs菲律宾,第一场考验,国足力争三连胜,比分预测:3:0, 2:0,4:1

    7比0大胜关岛,中国国家男子足球队在冲击2022卡塔尔世界杯的征途上收获两连胜,北京时间10月15日20点,中国男足将奔赴巴科洛德,客场挑战菲律宾男足,本场比赛也是中国男足在本次世预赛四十强赛阶段的第 ...

  6. 2019 蓝鲸人java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.蓝鲸人等公司offer,岗位是Java后端开发,因为发展原因最终选择去了蓝鲸人,入职一年时间了,也成为了面试官 ...

  7. python--线程和进程的初识

    一.进程与线程之间的关系 1.线程是属于进程的,线程运行在进程空间内,同一进程所产生的线程共享同一内存空间,当进程退出时该进程所产生的线程都会被强制退出并清除. 2.线程可与属于同一进程的其它线程共享 ...

  8. Python基础13

    <玩1>中关于病假.事假的问题,说得不完全正确. 实际为哑变量. 有关看待问题的维度.出发点(即屁股在哪里) 转哑变量后可以提高模型精度. 机器学习不怕字段过多. 转哑变量是在增维.

  9. Bootstrap初始化过程源码分析--netty客户端的启动

    Bootstrap初始化过程 netty的客户端引导类是Bootstrap,我们看一下spark的rpc中客户端部分对Bootstrap的初始化过程 TransportClientFactory.cr ...

  10. AHB总线协议(二)

    下图是8拍回环字突发传输:地址将在 32 字节边界处回环因此地址 0x3C 之后的地址是 0x20. 下图是8增量半字突发传输,所以地址每次增加 2 个字节并且突发在递增因此地址连续增加通过了 16 ...