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 ...
随机推荐
- Docker File
- What are regsvr32, regasm and gacutil using for?(转载)
What are regsvr32, regasm and gacutil using for? Regasm.exe is used to create COM Callable Wrapper ( ...
- <compilation debug="true" targetFramework="4.5.2"> 报错解决方案
有的时候新建项目,默认会选择比较高的 .net framework 版本如 4.5.2 有的时候发布项目就会遇到这个问题 解决的话 改成4.0就行了! 看你发布在哪里,如果在本地或者服务器,只要去下载 ...
- sql server 中取前n行字段最大值问题
例子 取前三行最大ID ID from Students)AS A 这样写得到的却是整个表的最大ID值,并不是我们需要的值 要在句中加入order by ID ID from Students ord ...
- poj3045 Cow Acrobats (思维,贪心)
题目: poj3045 Cow Acrobats 解析: 贪心题,类似于国王游戏 考虑两个相邻的牛\(i\),\(j\) 设他们上面的牛的重量一共为\(sum\) 把\(i\)放在上面,危险值分别为\ ...
- Loadsh 常用方法总结以及在vue中使用Loadsh
Loadsh 常用方法总结以及在vue中使用Loadsh Lodash 是一个一致性.模块化.高性能的 JavaScript 实用工具库.处理复杂数组,对比等可以直接采用该库,也方便快捷. 官方网站 ...
- JAVA I/O系统 Thinking in Java 之 File类
File类的文件具有一定的误导性,我们可能会认为它指代的是文件,实际上并非如此.它技能代表一个特定文件的名称,又能代表一个目录下的一组文件的名称.如果它指的是一个文件集,我们就可以对此集合调用list ...
- 如何做一个跨平台的游戏App?
如何做一个跨平台的游戏App? iOS和安卓系统上的应用程序,根据提供的内容不同,按照开发方式和用户体验不同,可区分为app和游戏: 首先从开发方式不同来说明,app开发一般是用操作系统官方提供的开发 ...
- 记一次删除ocr与dbfile的恢复记录
自己造成的一个案例: 场景:ocr磁盘组被我dd掉了,dbfile磁盘组也被我dd掉了.Rac起不来.之前ocr的DATA磁盘组被替换到了ABC磁盘.所幸的是有备份. 重新加载OCR磁盘 [root@ ...
- MS Sql 优化步骤及优化not in一例
今天接到客户投诉说系统卡死了,经过一翻努力,终于解决了.现将解决步骤记录一下,以便下次参考: 因为客户系统集中在阿里云上面,使用的是ms sql2008数据库,上面有N个客户,一下子无法知道是哪个客户 ...