题目链接: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,就是答案了。

  

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. #ifndef ONLINE_JUDGE
  11. freopen("in.txt", "r", stdin);
  12. #endif
  13. int m, s;
  14. while (scanf("%d%d", &m, &s) != EOF)
  15. {
  16. if (m == && s == )
  17. printf("0 0\n");
  18. else if (s == || *m < s)
  19. printf("-1 -1\n");
  20. else
  21. {
  22. string s1, s2;
  23. for (int i = ; i < m; i++)
  24. {
  25. int x = min(, s);
  26. s -= x;
  27. s2 += char(x + '');
  28. }
  29. s1 = s2;
  30. reverse(s1.begin(), s1.end());
  31. bool flag = false;
  32. for (int i = ; i < m && !flag; i++)
  33. {
  34. if (s1[i] == '')
  35. {
  36. for (int j = i+; j < m && !flag; j++)
  37. {
  38. if (s1[j] != '')
  39. {
  40. s1[j]--;
  41. s1[i]++;
  42. flag = true;
  43. break;
  44. }
  45. }
  46. }
  47. }
  48. cout << s1 << " " << s2 << endl;
  49. }
  50. }
  51. return ;
  52. }

 

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. Python 之我见

    读音 Python(KK 英语发音:/ˈpaɪθən/) 序言 其实早前就已经接触了python这个功能强大的脚本语言,但是那时只是基于兴趣而学习,目的性并不是很强,所以学习的并不是很深入.最近由于闲 ...

  2. codeforces 719A:Vitya in the Countryside

    Description Every summer Vitya comes to visit his grandmother in the countryside. This summer, he go ...

  3. hihocoder1187 Divisors

    传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given an integer n, for all integers not larger than n, f ...

  4. 类,抽象基类,接口类三者间的区别与联系(C++)

    结构上的区别: 普通类:数据+方法+实现 抽象类:数据+方法(一定包含虚方法n>=1)+部分方法的实现 接口类:方法(纯虚方法) http://www.cnblogs.com/Tris-wu/p ...

  5. javascript判断文件大小

    <input type="file" id="fileName" name ="fileName" onchange="Ge ...

  6. web漏洞总结

    目录: 1.sql注入获取数据库信息2.sql注入绕过管理后台登录3.反射型xss4.存储型xss5.csrf6.文件上传7.暴力破解8.目录遍历9.权限跨越10.文件包含11.未知漏洞 web漏洞演 ...

  7. motto5

    No matter what others say,I won't forsake my priciples.

  8. HDU4870_Rating_双号从零单排_高斯消元求期望

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...

  9. js实现剪切、复制、粘贴——clipBoard.js

    摘要: 最近项目上要实现一个点击按钮复制链接的功能,刚开始查找了一些资料,找了几款插件,ZeroClipboard是通过flash实现的复制功能,随着越来越多的提议废除flash,于是就想能不能通过j ...

  10. There are no interfaces on which a capture can be done.

    There are no interfaces on which a capture can be done. 今天启动了Wireshark 但是提示→There are no interfaces ...