The meaning of problems: Given n, m, solving the lexicographically smallest inverse number of 1-n consisting of m number of columns of
Method One: violence recursive solution
#include<iostream> #include<algorithm> using namespace std; int n,m; int a[(int)5e4+5]; void dfs(int l,int r,int ans) { if(l==r){ cout<<l<<' '; return ; } if(l>=r)return ; if(a[r-l+1]>=ans) { cout<<l<<' '; dfs (s + 1 , r, years); } Else { for ( int j = l + 1 ; j <= r j ++ ) if (a [jl + 1 ] + a [r-j + 1 ] + jl> = years) { years - = (jl ) ; dfs (l + 1 , j, ans- = min (years, [r-j + 1 ])); cout << l << ' ' ; dfs (j + 1 , r, min (years, [r-j + 1 ])); break; } } } int main () { a[1]=0,a[2]=0; for(int i=3;i<=5e4;i++) a[i]=a[i-1]+i-2; cin>>n>>m; dfs(1,n,m); return 0; }
Higher temporal complexity; reverse if the maximum number of digits constituting the n-1 is greater than 1 m will directly into a first, recursive sequence
If this is a digital number in reverse order into the remaining digits of the configuration of this position can not be greater than the number m of the back side of the enumeration, to find the nearest location to satisfy this condition and then divided into two recursive
The second method:
#include<iostream> using namespace std; typedef long long ll; int a[(int )1e5+5]; int main () { ll n,m; cin>>n>>m; ll l=1,r=n; for(int i=1;i<=n;i++) { int t=(n-i)*(n-i-1)/2; if(t>=m) a[l++]=i; else a[r--]=i,m-=(r-l+1); } for(int i=1;i<=n;i++) cout<<a[i]<<' '; return 0; }
The first solution by manual simulation: when sorting from the minimum value, or to place the order or to put backwards.
Another proof: the number of reverse could constitute fewer, smaller lexicographic, so on the last one, make the minimum number of lexicographical front.