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. SpringBoot 通过jjwt快速实现token授权

    A 10分钟了解JSON Web令牌(JWT)https://baijiahao.baidu.com/s?id=1608021814182894637&wfr=spider&for=p ...

  2. LearnOpenGL笔记(2)三角形

    这是学习LearnOpenGL CN教程的笔记,包括我遇到的问题和我的烂笔头.文章名与网站小节对应. ------------------------------------分割线---------- ...

  3. tkinter的单选Radiobutto

    from tkinter import * def printSelection(): num = var.get() if num == 1: lab.config(text="你是男生& ...

  4. 安装Ubuntu linux

    (1)下载Ubuntu http://www.ubuntu.com/download/desktop (2)制作启动U盘 1. 启动Rufus: 2. 插入U盘: 3. Rufus会提示更新,以自动选 ...

  5. 如何做一个跨平台的游戏App?

    如何做一个跨平台的游戏App? iOS和安卓系统上的应用程序,根据提供的内容不同,按照开发方式和用户体验不同,可区分为app和游戏: 首先从开发方式不同来说明,app开发一般是用操作系统官方提供的开发 ...

  6. 5种处理Vue异常的方法

    原文: Handling Errors in Vue.js 译者: Fundebug 本文采用意译,版权归原作者所有 去年一整年,我都在使用最爱的-Vue.js- 来做项目.最近突然意识到,我竟然从来 ...

  7. oracle查看表空间的真实使用情况

    --查看表空间的真实使用情况 set linesize 500 pagesize 500 col tablespace_name format a25 col TP_REAL_GB format a1 ...

  8. MySQL基础:DCL语句总结

    SQL语言大致分为DCL.DDL.DML三种,本文主要介绍MySQL 5.7版本DCL语句. 概述 DCL(Data Control Language)语句:数据控制语句,用于控制不同数据段直接的许可 ...

  9. uwsgi no python application found错误的解决(python3+centos6)

    近期在努力把自己的项目从python2转到python3上,因为生产环境无法抛弃centos7,所以只好在centos7上安装了python3.装好了python3,将python命令软连接改成pyt ...

  10. (原)堆叠hourglass网络

    转载请注明出处: https://www.cnblogs.com/darkknightzh/p/11486185.html 论文: https://arxiv.org/abs/1603.06937 官 ...