This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h> //Andrei Alexandru a.k.a Sho10
#define ll long long
#define double long double
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define all(a) (a).begin(), (a).end()
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pi pair
#define rc(s) return cout<<s,0
#define endl '\n'
#define mod 1000007
#define PI 3.14159265359
#define MAXN 100005
#define INF 1000000005
#define LINF 1000000000000000005ll
#define CODE_START ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll t,k,cnt[10],sz;
map<ll,ll>viz;
string ans="";
string s="";
string mn="";
void solve(int nr,int pos){
s+=(nr+'0');
cnt[nr]--;
if(pos==sz){
string mx="";
for(int i=1;i<=k;i++)
{
mx+='1';
}
for(int i=0;i<s.size()-k+1;i++)
{
string x=s.substr(i,k);
mx=max(mx,x);
}
if(mx<mn){
ans=s;
mn=mx;
}
cnt[nr]++;
s.pop_back();
return;
}
for(int i=1;i<=4;i++)
{
if(cnt[i]){
solve(i,pos+1);
}
}
cnt[nr]++;
s.pop_back();
return;
}
int32_t main(){
CODE_START;
cin>>t;
while(t--){
cin>>k;
for(ll i=1;i<=9;i++){
cin>>cnt[i];
}
ll s1=0;
for(ll i=3;i<=9;i++)
{
if(cnt[i]){
s1=1;
}
}
if(s1==0){
if(cnt[2]<k){
for(ll i=1;i<=2;i++)
{
for(ll j=1;j<=cnt[i];j++)
{
cout<<i;
}
}
cout<<endl;
continue;
}
string ans="";
for(ll i=1;i<k;i++)
{
ans+=(2+'0');
}
cnt[2]-=k-1;
sz=cnt[1]+cnt[2];
ll nr=(cnt[2]+cnt[1]-1)/cnt[1];
viz.clear();
for(ll i=0;i<=sz;i++)
{
viz[sz-i]=2-((i*cnt[1]+cnt[1]-1)%(cnt[1]+cnt[2])<cnt[1]);
}
for(ll i=sz;i>=1;i--)
{
ans+=(viz[i]+'0');
}
reverse(ans.begin(),ans.end());
cout<<ans<<endl;
}else if(k==2){
ll mx=0;
for(ll i=1;i<=9;i++){
if(cnt[i]>0){mx=i;}
}
string ans="";
ans.pb('0'+mx);
cnt[mx]--;
mx=0;
for(ll i=9; i>=1; i--){
if(cnt[i]>0){
mx=i;
break;
}
}
ll sum=0;
for(ll i=1;i<mx;i++){
if(sum>=cnt[mx]){
break;
}
sum+=cnt[i];
}
if(sum>=cnt[mx]){
for(ll i=1;(i<=mx)&&(cnt[mx]>0);i++){
while((cnt[i]>0)&&(cnt[mx]>0)){
cout<<mx;
cnt[mx]--;
if(cnt[i]>0)
{cout<<i;
cnt[i]--;
}
}
}
}
for(ll i=9;i>=1;i--){
for(ll j=1;j<=cnt[i];j++){
cout<<i;
}
}
cout<<ans<<endl;
}else {
mn="";
for(int i=1;i<=k;i++)
{
mn+='9';
}
sz=0;
for(int i=1;i<=9;i++)
{
cin>>cnt[i];
sz+=cnt[i];
}
ll pos=4;
ll sum=0;
for(ll i=4;i>=1;i--)
{
sum+=cnt[i];
if(sum>=k){
solve(i,1);
break;
}
}
cout<<ans<<endl;
}
}
}
Compilation message (stderr)
devil.cpp: In function 'void solve(int, int)':
devil.cpp:35:14: warning: comparison of integer expressions of different signedness: 'int' and 'long long unsigned int' [-Wsign-compare]
35 | for(int i=0;i<s.size()-k+1;i++)
| ~^~~~~~~~~~~~~
devil.cpp: In function 'int32_t main()':
devil.cpp:92:8: warning: unused variable 'nr' [-Wunused-variable]
92 | ll nr=(cnt[2]+cnt[1]-1)/cnt[1];
| ^~
devil.cpp:158:8: warning: unused variable 'pos' [-Wunused-variable]
158 | ll pos=4;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |