#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll n;
cin>>n;
getchar();
string s[100001];
for(int i=1;i<=n;i++)
{
cin>>s[i];
ll l=s[i].size();
for(int j = 0; j < l; j++) {
if(s[i][j] == '9') s[i][j] = '6';
}
if(i>1)
{
if(s[i].size()>s[i-1].size())continue;
if(s[i]>=s[i-1]) continue;
if(s[i]<s[i-1])//此数比上一个数字小
{
if(l<s[i-1].size())
{
cout<<"impossible";
return 0;
}
for(int j=0;j<l;j++)
{
if(s[i][j]=='6')
s[i][j]='9';
}
if(s[i]<s[i-1])
{
cout<<"impossible";
return 0;
}
else
{
for(int k = 0; k < l; k++) {
if(s[i][k] == '9') {
s[i][k] = '6';
if(s[i] < s[i-1]) {
s[i][k] = '9';
}
}
}
}
}
}
}
cout << "possible" << endl;
for(int i = 1; i <= n; i++) {
cout << s[i] << endl;
}
}
I . In-place Sorting [ 问题 8048 ] [ 讨论 ]
Description
Woe is you – for your algorithms class you have to write a sorting algorithm, but you missed the relevant lecture! The subject was in-place sorting algorithms, which you deduce must be algorithms that leave each input number in its place and yet somehow also sort the sequence.
Of course you cannot change any of the numbers either, then the result would just be a different sequence. But then it hits you: if you flip a 6 upside-down, it becomes a 9, and vice versa! Certainly no one can complain about this since you changed none of the digits! The deadline to hand in the exercise is in five hours. Try to implement this sorting algorithm before then!
Input
The input consists of:
A line with an integer n (2≤n≤10000), the number of integers in the input sequence.
n lines, the ith of which contains a positive integer xi (1≤xi≤1018), the ith number of the sequence.
Output
If the sequence cannot be sorted in non-decreasing order by flipping some of the digits 6 or 9 in the input(Flipping any of the digits of n is not allowed.), output “impossible”. Otherwise, output “possible” followed by the sorted sequence – each number on its own line.
If there is more than one valid solution, please output the smallest sequence.
Samples
Input 复制
4
9
7
7
9
Output
possible
6
7
7
9
Input 复制
4
97
96
66
160
Output
possible
67
69
69
160
Input 复制
3
80
97
79
Output
impossible
Input 复制
2
197
166
Output
possible
167
169
Source
BAPC 2020 Preliminaries