Codeforces Round #277.5 (Div. 2)C. Given Length and Sum of Digits...(贪心)

传送门

Description

You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base without leading zeroes.

Input

The single line of the input contains a pair of integers ms (1 ≤ m ≤ 100, 0 ≤ s ≤ 900) — the length and the sum of the digits of the required numbers.

Output

In the output print the pair of the required non-negative integer numbers — first the minimum possible number, then — the maximum possible number. If no numbers satisfying conditions required exist, print the pair of numbers "-1 -1" (without the quotes).

Sample Input

2 15

0 0

Sample Output

69 96

-1 -1

思路

题意:

求由1-9组成的长度为m且各位数之和为s的最大数和最小数。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 105;
int a[maxn];

bool OK(int m, int s)
{
    return s >= 0 && s <= m * 9;
}

int main()
{
    int m, s;
    scanf("%d%d", &m, &s);
    if (!OK(m, s) || (m > 1 && s == 0))  printf("-1 -1\n");
    else
    {
        int cnt = 0, sum = s;
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                if (((i > 0 || j > 0) || (m == 1 && j == 0)) && OK(m - i - 1, sum - j))
                {
                    sum -= j;
                    a[cnt++] = j;
                    break;
                }
            }
        }

        for (int i = 0; i < cnt; i++) printf("%d", a[i]);
        printf(" ");
        cnt = 0, sum = s;
        for (int i = 0; i < m; i++)
        {
            for (int j = 9; j >= 0; j--)
            {
                if (((i > 0 || j > 0) || (m == 1 && j == 0)) && OK(m - i - 1, sum - j))
                {
                    sum -= j;
                    a[cnt++] = j;
                    break;
                }
            }
        }
        for (int i = 0; i < cnt; i++) printf("%d", a[i]);
        puts("\40");
    }
    return 0;
}

  

时间: 2024-08-08 09:38:48

Codeforces Round #277.5 (Div. 2)C. Given Length and Sum of Digits...(贪心)的相关文章

Codeforces Round #277.5 (Div. 2) C Given Length and Sum of Digits...

大大的传送门:就是这里 这一道卡在了特判的  1   0 本来输出 0  0 输出  -1    -1了,就错了,, 这道题就是一个恨好的贪心,往大了贪 下面是代码 #include <cstdio> #include <cstring> #include <vector> using namespace std; int n,m; int ans[110]; int main(){ scanf("%d%d",&n,&m); mems

Codeforces Round #277.5 (Div. 2) JAVA版题解

Codeforces Round #277.5 (Div. 2) A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output In this problem your goal is to sort an array consisting of n integers in at most n swaps. For t

Codeforces Round #277.5 (Div. 2) b

/**  * @brief Codeforces Round #277.5 (Div. 2) b  * @file b.c  * @author 面码  * @created 2014/11/18 17:22  * @edited  2014/11/18 17:22  * @type greedy  *  */ #include <stdio.h> #define MAXN 110 #define max(a, b)  ((a) > (b) ? (a) : (b)) #define mi

Codeforces Round #277.5 (Div. 2)D Unbearable Controversy of Being (暴力)

这道题我临场想到了枚举菱形的起点和终点,然后每次枚举起点指向的点,每个指向的点再枚举它指向的点看有没有能到终点的,有一条就把起点到终点的路径个数加1,最后ans+=C(路径总数,2).每两个点都这么弄.但是我考虑时间复杂度n2前面的系数过大会超时,再想别的方法也没想出来.. 其实思路就是这样的,只不过时间上可以优化一下,就是用常用的两种做法不变而优化时间复杂度的方法:1.以空间换时间2.分步降维 #include <cstdio> #include <vector> using n

Codeforces Round #277.5 (Div. 2)——C贪心—— Given Length and Sum of Digits

You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base with

Codeforces Round #277.5 (Div. 2)

A 题意:给定n个数,最多交换n次获得一个不减的序列,求一个合法的交换序列. 题解:每次找从i开始的最小值换到第i个位置就可以了. #include <cstdio> #include <algorithm> #include <cstring> #include <iostream> #include <cmath> using namespace std; int a[5000],n,max1,maxx; int main() { scanf

Codeforces Round #277.5 (Div. 2)-D

题意:求该死的菱形数目.直接枚举两端的点.平均意义每一个点连接20条边,用邻接表暴力计算中间节点数目,那么中间节点任选两个与两端可组成的菱形数目有r*(r-1)/2. 代码: #include<iostream> #include<cstdio> #include<cmath> #include<map> #include<cstring> #include<algorithm> #define rep(i,a,b) for(int

Codeforces Round #277.5 (Div. 2)-C

简单细节题: #include<iostream> #include<cstdio> #include<cmath> #include<map> #include<cstring> #include<algorithm> #define rep(i,a,b) for(int i=(a);i<(b);i++) #define rev(i,a,b) for(int i=(a);i>=(b);i--) #define clr(a

Codeforces Round #277.5 (Div. 2) 解题报告

还是只会4道..sad... A:SwapSort 用一个数组存储排好序之后.然后从头开始依次将需要交换的与本来应该在这个位置的交换,最多交换n-1次. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h>