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 ...
随机推荐
- mysql08---优化01
Mysql数据库的优化技术 对mysql优化时一个综合性的技术,主要包括 a: 表的设计合理化(符合3NF) b: 添加适当索引(index) [四种: 普通索引(什么都不写).主键索引(有一个主键 ...
- YTU 2891: E--围栏
2891: E--围栏 时间限制: 1 Sec 内存限制: 128 MB 提交: 91 解决: 24 题目描述 一串连续字符被称作围栏当且仅当它由间隔的'|'和'-'组成.比如"|-|- ...
- UltraEdit mac破解版
2018-01-17 增加18.00.0.19破解 去官网下载原载,先运行一次,再在终端里执行下面代码就可以破解完成! printf '\x31\xC0\xFF\xC0\xC3\x90' | dd s ...
- C# Stopwatch
问题一:前几天写并行计算的实际应用——通讯录的时候,用到了stopwatch来计时,发现这个计时是真正的计时. Stopwatch stopwatch = new Stopwatch(); TimeS ...
- spring-boot快速搭建解析
创建方式: 1.在File菜单里面选择 New > Project,然后选择Spring Initializr: 2.使用maven直接构建,添加依赖. 1 2 3 4 pom.xml:Mave ...
- Watir: 很久以前,对Watir开始学习时候做的笔记
1). buttons Xpath 1)Button properties browser.button(:xpath,"//input[@id='b2']/").name bro ...
- hdu 1719
Friend Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- selenium中同名的class如何区分
例子是使用了selenium3.7.0 , python 3.6.2 , 火狐57版本 以下是例子的源码: <!DOCTYPE html><!-- <html> --& ...
- hdu 1071 The area【定积分】
用顶点式\( a(x-h)^2+k=y \)解方程,转化为\(ax^2+bx+c=y \)的形式,然后对二次函数求定积分\( \frac{ax^3}{3}+\frac{bx^2}{2}+cx+C \) ...
- [Swift通天遁地]一、超级工具-(2)制作美观大方的环形进度条
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...