Codeforces_B.Maximum Sum of Digits
http://codeforces.com/contest/1060/problem/B
题意:将n拆为a和b,让a+b=n且S(a)+S(b)最大,求最大的S(a)+S(b)。
思路:考虑任意一个数,例如156,将其分为 d1:(1,155)或 d2:(6,150),其实S(a)+S(b)都是;但是当分为 d3:(7,149)或 d4:(9,147)时(此时,由d2变为d3,b的个位由0变为9,跨越0),S(a)+S(b)变为了;将其分为 d5:(57,99)或 d6:(68,88)时,S(a)+S(b)变为。S(a)+S(b)是如何变大的?考虑b任意一个数位,当其从0变为9,S(a)增大1,S(B)增大8;当是其它情况时(数位变化不跨越0),相当于S(a)+x, S(b)-x,S(a)+S(b)不变。总结起来,划分(a,b)时让尽量多的数位变化能够跨越0,等于让b的每一位尽量分最多出来。得到贪心策略,每一数位都分9出来。例如,对4394826划分为(999999,3394827)
#include<cstdio>
#include<iostream>
#include<cstring> using namespace std; #define LL long long int digitsum(LL x)
{
int res=;
while(x>)
{
res+=x%;
x/=;
}
return res;
} int main()
{
LL n;
while(scanf("%I64d",&n)!=EOF)
{
int cnt9=;
LL tmp9=;
while(tmp9<=n)
{
tmp9=tmp9*+;
cnt9++;
}
tmp9/=;
cnt9--;
int res=cnt9*+digitsum(n-tmp9);
printf("%d\n",res);
}
return ;
}
Codeforces_B.Maximum Sum of Digits的更多相关文章
- CodeForces 1060 B Maximum Sum of Digits
Maximum Sum of Digits You are given a positive integer n. Let S(x)S(x) be sum of digits in base 10 r ...
- cf#513 B. Maximum Sum of Digits
B. Maximum Sum of Digits time limit per test 2 seconds memory limit per test 512 megabytes input sta ...
- Maximum Sum of Digits(CodeForces 1060B)
Description You are given a positive integer nn. Let S(x) be sum of digits in base 10 representation ...
- CF1060B:Maximum Sum of Digits
我对贪心的理解:https://www.cnblogs.com/AKMer/p/9776293.html 题目传送门:http://codeforces.com/problemset/problem/ ...
- CodeForces 489C Given Length and Sum of Digits... (贪心)
Given Length and Sum of Digits... 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/F Descr ...
- Codeforces Round #277.5 (Div. 2)C——Given Length and Sum of Digits...
C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...
- codeforces#277.5 C. Given Length and Sum of Digits
C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...
- CodeForces 489C Given Length and Sum of Digits... (dfs)
C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...
- Codeforces Round #277.5 (Div. 2)-C. Given Length and Sum of Digits...
http://codeforces.com/problemset/problem/489/C C. Given Length and Sum of Digits... time limit per t ...
随机推荐
- jdk 版本不一致导致的错误
平时做项目时难免会从git,svn下载代码或者把别人的项目文件导入到自己的MyEclipse中进行操作,因此会遇到很多问题,常见的有一种是使用的jdk版本不一致造成的报错, 错误案例: 错误提 ...
- B2C
B2C是Business-to-Customer的缩写,而其中文简称为"商对客"."商对客"是电子商务的一种模式,也就是通常说的直接面向消费者销售产品和服务商业 ...
- caioj1272&&codeforces 148D: 概率期望值3:抓老鼠
这道真的是好题,不卡精度,不卡细节,但是思考的方式很巧妙! 一开始大家跟我想的应该差不多,用f[i][j]表示有i只白老鼠,j只黑老鼠的胜率,然后跑DP,然后我就发现,这样怎么做?还有一种不胜不负的平 ...
- bzoj2683(要改一点代码)&&bzoj1176: [Balkan2007]Mokia
仍然是一道cdq模版.. 那么对于一个询问,容斥一下分成四个,变成问(1,1)~(x,y),那么对于x,y,修改只有x'<=x&&y'<=y,才对询问有影响,那么加上读入顺 ...
- casperjs userAgent的一些问题
casperjs 的options内的userAgent若设置为非正常浏览器的字符串,可能导致form无法正确提交. 表现为,this.click()失效,或evaluate(function(){$ ...
- YTU 2918: Shape系列-5
2919: Shape系列-5 时间限制: 1 Sec 内存限制: 128 MB 提交: 251 解决: 199 题目描述 JC和Kitty听说小亮和小华有了Rectangle和Circle并用R ...
- idea新建springmvc+spring+mybaties项目2
1,项目创建完成后,src-main下建立java目录后,是无法在该目录下创建新的包和java类等文件的.在idea中需要对目录进行标注 Sources 一般用于标注类似 src 这种可编译目录.有时 ...
- 一个tomcat部署多个应用实例总结
项目组有好几个项目需要运行,之前项目少,一个tomcat对应一个项目还能应付,但现在项目多了,要是再一个tomcat对应一个项目的话,一方面看起来很业余,一方面也加大服务器的维护难度.所以现在需要对t ...
- tools:context=".MainActivity的作用 (转载)
转自:http://blog.csdn.net/caiwenfeng_for_23/article/details/8373569 <TextView android:layout_width= ...
- 在selenium中一些相对常用的JavaScript事件
输入框输入: 1.找到输入框的id,然后进行输入操作 ordinal :输入框的id parameter :需要输入的内容 browser.execute_script("document. ...