题目链接: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... 解题报告的更多相关文章

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

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

  3. Codeforces 489C Given Length and Sum of Digits...

    m位长度,S为各位的和 利用贪心的思想逐位判断过去即可 详细的注释已经在代码里啦~ //#pragma comment(linker, "/STACK:16777216") //f ...

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

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

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

  7. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  8. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  9. 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)

    [LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

随机推荐

  1. Informatica 错误:Cannot convert from SQL type 93 to C type 4

    经验和积累蛮重要!向大神学习! ---------------------------------------------------------------------- Mapping: 在sou ...

  2. POJ-2352 Stars 树状数组

    Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39186 Accepted: 17027 Description A ...

  3. Bzoj3893 [Usaco2014 Dec]Cow Jog

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 302  Solved: 157 Description The cows are out exerci ...

  4. [NOIP2009] 普及组

    多项式输出 模拟 /*by SilverN*/ #include<algorithm> #include<iostream> #include<cstring> # ...

  5. HDU 5714

    Problem Description 小明在旅游的路上看到了一条美丽的河,河上有许多船只,有的船只向左航行,有的船只向右航行.小明希望拍下这一美丽的风景,并且把尽可能多的船只都完整地拍到一张照片中. ...

  6. 中国天气网-天气预报接口api

    中国天气网地址:http://www.weather.com.cn 请求服务 : 查询实时天气信息 http://www.weather.com.cn/data/sk/101110101.html 在 ...

  7. Linux创建用户命令

    创建用户.设置密码.修改用户.删除用户: useradd testuser   创建用户testuser passwd testuser   给已创建的用户testuser设置密码 说明:新创建的用户 ...

  8. php验证是否是中文

    header("content-type:text/html;charset=utf-8");$str = "编";if (preg_match("/ ...

  9. WinForm中动态添加控件 出现事件混乱,解决办法记录。

    还是在抢票软件中出的问题,我没点击一个联系人,要生成一排控件,其中有席别combobox这样的下拉框控件,会出现如下图所示的问题:问题描述:在代码中动态创建的控件,事件混乱了,一个控件触发了所有同类型 ...

  10. ping 和 traceroute 命令

    ping 和 traceroute 命令 ping 程序 就是发送一个ICMP查询报文给某服务器,以测试该服务器是否可达. 当返回ICMP回显应答时,要打印出序列号.TTL,和往返时间:   [roo ...