Codeforces 489C 简单构造

本文介绍了一个编程问题的解决方法,该问题要求根据指定的位数和各位数字之和来构造最大和最小的合法非负整数。文章详细阐述了验证输入的有效性、构造最小与最大整数的具体步骤,并提供了实现这一逻辑的C++代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门:


http://codeforces.com/contest/489/problem/C

Given Length and Sum of Digits...

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You have a positive integer m and a non-negative integers. Your task is to find the smallest and the largest of the numbers that have lengthm 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 m, s (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 test(s)
Input
2 15
Output
69 96
Input
3 0
Output
-1 -1

题意:给一个数的位数和每一位的和,问这个数最大是多少,最小是多少。不存在输出-1

思路:先检查合法性,是否输出-1 -1;

构造最小数:从尾部开始构造,直接填9(或者剩下的数的和-1),最后剩下的数应该大于等于1,这个填到首位。其他地方填0.

构造最大数:从首位开始构造,直接填9(或者剩下的数的和),其他的地方直接填0.

代码:

#include<cstdio>
int main()
{
    int n,m;
    while(~scanf("%d %d",&n,&m))
    {
        if(m==0&&n==1)
        {
            printf("0 0\n");
            continue;
        }
        if(m<1||n*9<m)
        {
            printf("-1 -1\n");
            continue;
        }
        char s[200]={0};
        for(int i=1;i<=n;i++)
        {
            int tmp=m;
            if(tmp>=9)
            {
                s[i]=9+48;
                m-=9;
            }
            else
            {
                s[i]=tmp+48;
                m-=tmp;
            }
        }
        char temp[105];
        sprintf(temp,"%s",s+1);
        if(s[n]=='0')
        {
            s[n]++;
            for(int i=n-1;i>=1;i--)
            if(s[i]>48)
            {
                s[i]--;
                break;
            }
        }
        for(int i=n;i>=1;i--) printf("%c",s[i]);
        printf(" ");
         printf("%s\n",temp);
    }
    return 0;
}

### Codeforces Round 1027 题目与题解 Codeforces Round 1027 是一场面向不同水平选手的比赛,包含多道编程问题,涉及算法、数据结构以及数学知识。以下是部分题目及其对应的题解分析。 #### A. 示例题目(假设为“A. Simple Arithmetic”) **题目描述** 给定一个整数 \( x \),判断是否可以通过某种方式将 \( x \) 分解为若干个数字的和,并满足特定条件。 **题解** 此问题可以通过简单模拟来解决。通过检查 \( x \) 的每一位数字是否符合要求,逐步缩小范围直至找到答案[^1]。以下是一个可能的实现代码: ```cpp #include <bits/stdc++.h> using namespace std; void solve() { long long x; cin >> x; while (x) { if (x > 0 && x < 10 || x % 10 == 9) { cout << "NO\n"; return; } x /= 10; x -= 1; } cout << "YES\n"; } ``` 上述代码通过逐位检查 \( x \) 的值是否满足条件,并在不满足时直接返回“NO”。 --- #### B. 中位数调整问题(假设为“B. Median Adjustment”) **题目描述** 给定一个数组,通过删除某些元素使得中位数向左或向右移动,最终形成一段连续的合法区间。 **题解** 为了实现中位数的调整,需要枚举每一段可能的区间并判断其合法性。如果目标是左移,则优先删除右侧端点;如果是右移,则删除左侧端点[^4]。代码示例如下: ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (auto &x : a) cin >> x; // 枚举所有可能的区间 int result = 0; for (int l = 0; l < n; ++l) { vector<int> temp; for (int r = l; r < n; ++r) { temp.push_back(a[r]); // 判断当前区间是否合法 if (is_valid(temp)) result += 1; } } cout << result; } ``` 在此代码中,`is_valid` 函数用于验证当前区间的合法性[^4]。 --- #### C. 序列构造问题(假设为“C. Sequence Construction”) **题目描述** 给定两个整数 \( n \) 和 \( k \),构造一个大小不超过 25 的非负整数序列 \( a \),使得: 1. 存在一个子序列的和为任意 \( v \neq k \) 且 \( 1 \leq v \leq n \)。 2. 不存在任何子序列的和等于 \( k \)。 **题解** 此类问题的核心在于构造一个满足条件的序列。一种常见方法是通过贪心策略逐步添加元素,同时避免生成和为 \( k \) 的子序列[^3]。以下是一个可能的实现: ```cpp #include <bits/stdc++.h> using namespace std; vector<int> construct_sequence(int n, int k) { vector<int> res; for (int i = 1; i <= n && res.size() < 25; ++i) { if (i != k) res.push_back(i); // 检查是否存在和为 k 的子序列 if (has_subsequence_sum(res, k)) return {}; } return res; } bool has_subsequence_sum(const vector<int> &a, int k) { // 使用动态规划检查是否存在子序列和为 k vector<bool> dp(k + 1, false); dp[0] = true; for (auto x : a) { for (int j = k; j >= x; --j) { dp[j] |= dp[j - x]; } } return dp[k]; } ``` 上述代码通过动态规划验证是否存在和为 \( k \) 的子序列[^3]。 --- #### D. 路径众数问题(假设为“D. Path Majority”) **题目描述** 给定一棵树,判断是否存在一条路径,使得该路径上的众数(出现次数最多的节点值)超过路径总长度的一半。 **题解** 此问题可以通过深度优先搜索(DFS)结合众数统计的方法解决。关键在于观察到如果路径上存在众数,则必然会出现两个相同的节点值相邻或间隔一个节点的情况[^2]。因此可以直接对所有节点进行简单判断即可。 ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; vector<int> adj[MAXN]; int values[MAXN]; bool dfs(int u, int parent, int target) { int count = 0; if (values[u] == target) count += 1; for (auto v : adj[u]) { if (v != parent) { count += dfs(v, u, target); } } return count > (adj[u].size() + 1) / 2; } bool has_majority_path(int n) { for (int i = 1; i <= n; ++i) { if (dfs(i, -1, values[i])) return true; } return false; } ``` 上述代码通过递归检查每个节点是否可能成为路径上的众数[^2]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值