题意:
给你n个小于101810^{18}1018的大数,问在可以再不改变序列位置,之改变数值中某数位的‘9’变为‘6’或将‘6’变为‘9’,求的最终序列由小到大,且字典序最小。
题目:
链接:https://ac.nowcoder.com/acm/contest/16741/H
来源:牛客网
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 difffferent sequence. But then it hits you: if you flflip 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 fifive hours. Try to implement this sorting algorithm before then!
输入描述:
The input consists of:
• A line with an integer n (2 ≤ n ≤ 10 000), 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.
输出描述:
If the sequence cannot be sorted non-decreasingly by flipping some number 6 or 9 in input 1, output “not possible”. 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.
示例1
输入
4
9
7
7
9
输出
possible
6
7
7
9
示例2
输入
4
97
96
66
160
输出
possible
67
69
69
160
示例3
输入
3
80
97
79
输出
impossible
示例4
输入
2
197
166
输出
possible
167
169
分析:
是一道比较容易的题,但比赛时考虑太多,wa了十几遍。。。下来看题解,有点想吐血
废话不多说了,下面看分析:
- 贪心构造,使每个串在大于等于前一个串的前提下尽可能小。注意字符串之间的大小比较与数字之间大小比较的不同(以字符串形式输入数字)。
依次遍历n个串,分情况构造:- 当前串长度 > 前一个串长度。那直接把当前串的所有9变成6就可以了,变完之后当前串肯定还是大于前一个串。
- 当前串长度 < 前一个串长度。就算把当前串的所有6变成9也无力回天了,直接impossible结束。
- 当前串长度 = 前一个串长度。先还是把当前串的所有6变成9,如果还是比前一个串小,直接impossible结束;否则,从高位到低位,依次尝试把9变回6,这个时候要注意,如果变了之后比前一个串小了,那就说明不能变,一定要还原回去!
AC代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int M=1e4+10;
string a[M];
int n;
bool solve(string f,int ii){if(f.size()>a[ii-1].size()){for(int j=0;j<f.size();j++)if(f[j]=='9')f[j]='6';a[ii]=f;}else{if(f.size()<a[ii-1].size())return 0;for(int i=0;i<f.size();i++)if(f[i]=='6')f[i]='9';if(f<a[ii-1])return 0;for(int i=0;i<f.size();i++){if(f[i]=='9'){f[i]='6';if(f<a[ii-1])f[i]='9';}}a[ii]=f;}return 1;
}
int main(){cin>>n;int vis=0;a[0]="0";for(int i=1;i<=n;i++){cin>>a[i];if(vis==1)continue;if(!solve(a[i],i))vis=1;}if(vis==1) cout<<"impossible"<<endl;else{cout<<"possible"<<endl;for(int i=1;i<=n;i++)cout<<a[i]<<endl;}return 0;
}
想了想还是抛上我比赛时地错误代码:
分析:
- 因为我没有分长度判断,所以开了一个函数计算长度为101810^{18}1018的数值,
也是够够的,比赛时的确注意到直接比较字符串的字典序,由于长度的问题,会有问题,但是随即的解决方案,搞了这么个吃力不讨好的。 - 暴力莽,想着对于所有6和9的出现位置可以先全部更为6,然后进制枚举一下可能性,将6变为9,那么有2182^{18}218种排列,数据量218∗1042^{18}*10^4218∗104,妥妥的超时…
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<map>
#include<iostream>
using namespace std;
typedef long long ll;
const int M=1e4+10;
string a[M];
int b[M];
int n,tot;
ll dfs(string f){ll ans=0;int p=f.size();for(int i=0;i<p;i++){ans=ans*10+f[i]-'0';}return ans;
}
bool solve(string f,int ii){int ans=1<<tot;for(int i=0;i<ans;i++){string w=f;int k=i;int kk=0;while(k>0){if(k%2==1){w[b[tot-1-kk]]='9';}k/=2;kk++;}if(dfs(w)>=dfs(a[ii-1])){a[ii]=w;return true;}}return false;
}
int main(){//while(1){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin>>n;int vis=0;a[0]="0";for(int i=1;i<=n;i++){cin>>a[i];tot=0;if(vis==1)continue;string s=a[i];int l=s.size();for(int j=0;j<l;j++){if(s[j]=='6')b[tot++]=j;if(s[j]=='9'){b[tot++]=j;s[j]='6';}}if(!solve(s,i))vis=1;}if(vis==1) cout<<"impossible"<<endl;else{cout<<"possible"<<endl;for(int i=1;i<=n;i++)cout<<a[i]<<endl;}//}return 0;
}