Codeforces Beta Round #97 (Div. 2)

本文介绍了一个算法问题,即如何通过一次元素替换并排序后得到数组每个位置上的最小可能值。输入包括数组长度及元素,输出为替换排序后的最小值数组。

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

C. Replacement
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all.

After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.

<p< p=""><p< p="">
Input
<p< p="">

The first line contains a single integer n (1 ≤ n ≤ 105), which represents how many numbers the array has. The next line contains n space-separated integers — the array's description. All elements of the array lie in the range from 1 to 109, inclusive.

<p< p=""><p< p="">
Output
<p< p="">

Print n space-separated integers — the minimum possible values of each array element after one replacement and the sorting are performed.

<p< p=""><p< p="">
Sample test(s)
<p< p=""><p< p="">
input
5
1 2 3 4 5
output
1 1 2 3 4
input
5
2 3 4 5 6
output
1 2 3 4 5
input
3
2 2 2
output
1 2 2


#include<cstdio>
#include<algorithm>
using std::sort;
using std::min;

int main()
{
    int n, m, mm = 0;
    int array[199999];
    scanf("%d",&n);
    for ( int i = 1; i <= n; i++ )
    {
        scanf("%d",&array[i]);
        if ( array[i] > mm ) mm = array[i], m = i;
    }
    array[m] = ( mm == 1 ? 2 : 1 );
    sort(array+1,array+1+n);
    for ( int i = 1; i <= n; i++ )
        printf("%d ",array[i]);
    printf("\n");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值