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

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

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

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

  4. CF1060B:Maximum Sum of Digits

    我对贪心的理解:https://www.cnblogs.com/AKMer/p/9776293.html 题目传送门:http://codeforces.com/problemset/problem/ ...

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

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

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

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

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

随机推荐

  1. [Unity3D]Unity3D游戏开发之连续滚动背景

    在诸如天天跑酷等2D游戏中.因为游戏须要表现出运动的感觉.通常都会使游戏背景连续循环滚动以增强视觉效果,那么今天.博主就来带领大家一起来实现连续滚动背景吧. 首先来讲述一下原理.准备两张连续的图片(博 ...

  2. mysql 转换编码方式

    进入mysql 的安装文件夹找到 “ my.ini” 文件  (mysql配置文件) 一.编辑MySql的配置文件 vim /etc/my.cnf 在 [mysqld] 标签下加上三行 default ...

  3. [MAC] Load Crypto.Cipher.ARC4 Failed, Use Pure Python Instead.

    MAC启动GoAgent,出现Load Crypto.Cipher.ARC4 Failed, Use Pure Python Instead. 解决方法:为Python安装pycrypto,可通过Py ...

  4. android编程取消标题栏方法(appcompat_v7、Theme.NoTitleBar)

    方式一:编码方式 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstance ...

  5. bzoj2743 [HEOI2012]采花——树状数组+离线

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2743 和 HH的项链 那道题很像,也是类似的做法: 但不同的是这里的点只有有前驱时才起作用: ...

  6. 修复win7的启动引导

    自己的电脑上本身是win7,为了试验openstack,决定装个ubuntu server,过程也不太懂,一直卡在分区不敢贸然前进,反复了几次,终于导致我进不了原来的win7,原因不明! 当时就慌了, ...

  7. 使用FFMPEG从MP4封装中提取视频流到.264文件 (转载)

    命令行: ffmpeg -i 20130312_133313.mp4 -codec copy -bsf: h264_mp4toannexb -f h264 20130312_133313.264 说明 ...

  8. MySQL之不得不说的keepsync和trysync

    此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 开宗明义,keepsync和trysync是网易MySQL分支版本InnoSQL的两个参数,非常重要的两个参 ...

  9. 重启fpm

    ps aux | grep php-fpm cat /etc/php-fpm.conf kill -USR2 `cat /run/php-fpm/php-fpm.pid

  10. input type="file"文件上传到后台读取

    html页面(表单采用bootStrap) js部分: //更换头像时把上传的图片post方式到控制器 <script type="text/javascript"> ...