NC223546. ArrayofDiscord
描述
输入描述
The first line of input contains n (2 ≤ n ≤ 100), the length of Zeus’ answer. The second line contains n integers(
), Zeus’ answer
输出描述
If Eris can make the list not be sorted by changing a single digit of one of the numbers, then output n integers b1, . . . , bn, the resulting list of numbers after making the change. Otherwise, output “impossible”. If there are many valid solutions, any one will be accepted.
示例1
输入:
3 2020 2020 2020
输出:
2021 2020 2020
示例2
输入:
2 1 9999999
输出:
impossible
示例3
输入:
4 1 42 4711 9876
输出:
1 42 4711 3876
C++ 解法, 执行用时: 4ms, 内存消耗: 524K, 提交时间: 2021-08-12 14:51:48
#include<bits/stdc++.h> using namespace std; #define int long long int n,a[123],flag; bool judge(int &x,int &y) { string sx=to_string(x); for(auto &c:sx) { char d=c; c='9'; if(stoll(sx)>y) { x=stoll(sx); return 1; } c=d; } string sy=to_string(y); for(int i=0;sy[i];i++) { char c=sy[i]; sy[i]=i||!sy[1]?'0':'1'; if(x>stoll(sy)) { y=stoll(sy); return 1; } sy[i]=c; } return 0; } signed main() { cin>>n; for(int i=0;i<n;i++) { cin>>a[i]; if(i&&!flag) flag|=judge(a[i-1],a[i]); } if(flag) for(int i=0;i<n;i++) cout<<a[i]<<" \n"[i==n-1]; else cout<<"impossible"; }
Python3 解法, 执行用时: 53ms, 内存消耗: 7040K, 提交时间: 2021-08-03 15:11:40
n = int(input()) a = input().split() for i in range(n-1): if len(a[i]) == len(a[i+1]): b1 = '9' + a[i][1:] b2 = '1' + a[i+1][1:] if len(a[i]) > 1 else '0' if int(b1) > int(a[i+1]): a[i] = b1 print(' '.join(a)) break if int(a[i]) > int(b2): a[i+1] = b2 print(' '.join(a)) break else: print('impossible')