codeforces 489C.Given Length and Sum of Digits... 解题报告
题目链接:http://codeforces.com/problemset/problem/489/C
题目意思:给出 m 和 s,需要构造最大和最小的数。满足长度都为 m,每一位的数字之和等于 s。如果构造不出来,输出 -1 -1。否则输出最小和最大且符合条件的数。
想了两个多小时,发现想错了方向......
/******************************************
首先把不能得到最小数和最大数的情况揪出来。
第二组测试数据 3 0 有提示,s = 0 且 m > 1,
还有一种无解的情况就是 9 m < s(怎么填全部位的数字之和都凑不够 s),其实这种情况是有最小解的,前提是s >= 1,10000000....0 就是了,但是都归为 -1 -1 行列(感觉这个不是太过严谨)。
至于 s = 0,m = 1是 输出 0 0。然后就开始重头戏了........
我是从最小数开始构造的(感觉这样做就呵呵了),容易知道最小数的构造是,从个位开始填数,尽量填最大的数字9,然后剩下的数remain = s - 9,接着填十位、百位、......直到填完 第 m 位。我们要尽可能把最大的数字往低位填,那么才能保证最终得到的数是最小的。但是要考虑到填的过程中,有可能这个remain 不足9,然后尝试8,再不行的话,填7,直到0,不过前提是保证最高位最小为 1,保证没有前导 0 嘛。
但实现起来很复杂,循环是从 m-1 ——> 0 的,于是要考虑 i == 1 时,不能把remain完全填光,只能填remain + 1。对于第一组数据 2 15,ans:69 96;好像又要额外讨论......所以总体来讲,不好写,即使写出来也很烦琐。
******************************************/
看了别人的,一下子顿悟了!
从最大数开始构造就简单多了。循环变成 0 ~ m-1,然后死命填大数字9,不行的话填剩下最大的,之后的位就是填0了。
构造最小数更加方便,把最大数复制到最小数里,然后颠倒过来,如果首位是 0,就找到后面位中第一次遇到的非0数字,从它那里拿1,这位数就减1,就是答案了。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int m, s;
while (scanf("%d%d", &m, &s) != EOF)
{
if (m == && s == )
printf("0 0\n");
else if (s == || *m < s)
printf("-1 -1\n");
else
{
string s1, s2;
for (int i = ; i < m; i++)
{
int x = min(, s);
s -= x;
s2 += char(x + '');
}
s1 = s2;
reverse(s1.begin(), s1.end());
bool flag = false;
for (int i = ; i < m && !flag; i++)
{
if (s1[i] == '')
{
for (int j = i+; j < m && !flag; j++)
{
if (s1[j] != '')
{
s1[j]--;
s1[i]++;
flag = true;
break;
}
}
}
}
cout << s1 << " " << s2 << endl;
}
}
return ;
}
codeforces 489C.Given Length and Sum of Digits... 解题报告的更多相关文章
- 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 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 489C Given Length and Sum of Digits...
m位长度,S为各位的和 利用贪心的思想逐位判断过去即可 详细的注释已经在代码里啦~ //#pragma comment(linker, "/STACK:16777216") //f ...
- 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 ...
- 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 ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)
[LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
随机推荐
- php两种导出excel的方法
所需要的:jquery库,phpexcel插件,页面导出excel效果测试文件explode.php,excel导出功能实现文件exp.php和explode_excel.php,文件相关内容在此文下 ...
- 工匠若水 Android应用开发编译框架流程与IDE及Gradle概要
http://blog.csdn.net/yanbober/article/details/45306483 http://blog.csdn.net/yanbober/article/details ...
- Centos是什么
Linux是GNU/Linux的缩写,通常指各种Linux发行版的通称. 常见的Linux厂家主要有Redhat/Novell等. Redhat有两大Linux产品系列,其一是免费的Fedora Co ...
- easyUI框架之学习2--添加左侧导航栏
<head> function addTab(title, url) { if ($('#tableContainer').tabs('exists', title)) { $('#tab ...
- boost解析json(2)
"{ "A":1, "B":{ "C":2, "D":3 }, "E":[ {" ...
- 分子量 (Molar Mass,ACM/ICPC Seoul 2007,UVa 1586)
解题思路: 1.将分子量用double 数组记录下来 2.将字符串存储在字符数组中,从头向后扫描,一直记住“字母”,对下一个字符进行判断,是否是数字,如果是数字:用一个整数记录,本代码中用的sum,同 ...
- 用css3制作旋转加载动画的几种方法
以WebKit为核心的浏览器,例如Safari和Chrome,对html5有着很好的支持,在移动平台中这两个浏览器对应的就是IOS和Android.最近在开发一个移动平台的web app,那么就有机会 ...
- 将对象转为数组方法:延伸array_map函数在PHP类中调用内部方法
public static function objectToArray($d) { if (is_object($d)) { $d = get_object_vars($d); } if (is_a ...
- svn代码回滚命令
代码回滚提交: 比如要把73回滚到68 svn merge -r 73:68 http://my.repository.com/my/project/trunk 然后commit就行了 svn com ...
- HTTP协议详解(一直在用可是这篇太好了转一下)
引言 HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1. ...