結果

問題 No.3408 1215 Segments
コンテスト
ユーザー TKTYI
提出日時 2025-12-15 02:39:30
言語 C++23
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 3 ms / 2,500 ms
コード長 4,424 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,916 ms
コンパイル使用メモリ 335,100 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-12-15 02:39:38
合計ジャッジ時間 7,621 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
typedef long long int ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<vvvl> vvvvl;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
typedef vector<vvvb> vvvvb;
typedef pair<ll,ll> pl;
typedef pair<ll,pl> ppl;
typedef pair<ll,ppl> pppl;
typedef pair<ll,pppl> pppppl;
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) begin(a),end(a)
#define sz(a) (int)(a).size()
#define F first
#define S second
#define bs(A,x) binary_search(all(A),x)
#define lb(A,x) (ll)(lower_bound(all(A),x)-A.begin())
#define ub(A,x) (ll)(upper_bound(all(A),x)-A.begin())
#define cou(A,x) (ll)(upper_bound(all(A),x)-lower_bound(all(A),x))
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}
//*
using mint=modint998244353;
const ll mod=998244353;
//*/
/*
using mint=modint1000000007;
const ll mod=1000000007;
//*/
//using mint=modint;
//*
typedef vector<mint> vm;
typedef vector<vm> vvm;
typedef vector<vvm> vvvm;
typedef vector<vvvm> vvvvm;
ostream&operator<<(ostream&os,mint a){os<<a.val();return os;}
istream&operator>>(istream&is,mint&a){int x;is>>x;a=mint(x);return is;}
//*/
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>p){os<<p.F<<" "<<p.S;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}
template<typename T>ostream&operator<<(ostream&os,vector<T>v){rep(i,0,sz(v))os<<v[i]<<(i+1!=sz(v)?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
int main(){
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  ll N;cin>>N;
  string S=to_string(N);
  //vvl A={
  //	{ 0,-1, 0, 0, 0, 0},
  //	{ 0, 1, 0, 1, 1, 1},
  //	{ 0, 0, 1, 0, 0, 1},
  //	{ 1, 0, 0, 1, 0, 0},
  //	{-1, 0, 0, 0, 0,-1},
  //	{-1, 0, 0,-1, 0, 0},
  //	{ 1, 1, 1, 0, 0, 1},
  //	{ 0, 0,-1,-1, 0, 0},
  //	{ 0, 0,-1, 0, 0, 0},
  //	{ 1, 1, 0, 1, 1, 1},
  //	{-1,-1, 0,-1,-1, 0},
  //	{ 0,-1, 0,-1,-1, 0},
  //	{ 0, 0, 0, 0, 0, 0},
  //	{ 0, 0, 0,-1, 0, 0},
  //	{ 0, 0, 0,-1,-1, 0}
  //};
  ll ans=1111111113333333338;
  vl A(15);
  for(A[5]=0;2*A[5]<=19;A[5]++){
  	for(A[7]=0;2*A[5]+3*A[7]<=19;A[7]++){
  		for(A[13]=0;2*A[5]+3*A[7]+2*A[13]<=19;A[13]++){
  			A[3]=A[5]+A[7]+A[13];
  			for(A[8]=0;A[3]+A[5]+2*A[7]+2*A[8]+A[13]<=19;A[8]++){
  				for(A[10]=0;A[3]+A[5]+2*A[7]+2*A[8]+2*A[10]+A[13]<=19;A[10]++){
  					for(A[11]=0;A[3]+A[5]+2*A[7]+2*A[8]+2*A[10]+2*A[11]+A[13]<=19;A[11]++){
  						for(A[14]=0;A[3]+A[5]+2*A[7]+2*A[8]+2*A[10]+2*A[11]+A[13]+2*A[14]<=19;A[14]++){
  							A[4]=A[7]+A[8]+A[10]+A[11]+A[14];
  							for(A[9]=0;A[9]<=A[10]+A[11]+A[14]&&A[3]+A[4]+A[5]+A[7]+A[8]+A[9]+A[10]+A[11]+A[13]+A[14]+(A[10]+A[11]+A[14]-A[9])<=19;A[9]++){
  								A[1]=A[10]+A[11]+A[14]-A[9];
  								A[2]=A[7]+A[9]+A[13]-2*A[10]-A[11]-A[14];
  								A[6]=A[8]+2*A[10]+A[11]+A[14]-A[9]-A[13];
  								A[0]=A[6]+A[14];
  								if(A[2]<0||A[0]<0||A[6]<0||A[0]+A[1]+A[2]+A[3]+A[4]+A[5]+A[6]+A[7]+A[8]+A[9]+A[10]+A[11]+A[13]+A[14]>19)continue;
  								vl T={A[0]+A[1],A[2]+A[3],A[4],A[5],A[6],A[7],A[8]+A[9],A[10]+A[11],0,A[13]+A[14]};
  								ll s=0;
  								rep(i,0,10)s+=T[i];
  								for(;T[8]+s<=19;T[8]++){
  									if(s+T[8]<sz(S))continue;
  									if(s+T[8]>sz(S)){
  										ll t=1;
  										while(t<10&&!T[t])t++;
  										if(t==10)continue;
  										ll n=t;
  										rep(i,0,10)rep(_,0,T[i]-(i==t))n=10*n+i;
  										chmin(ans,n);
  									}
  									else{
  										vl TT=T;
  										ll nn=0;
  										rep(i,0,sz(S)){
  											ll t=S[i]-'0'+1,n=nn;
  											while(t<10&&!TT[t])t++;
  											if(t<10){
  												ll n=10*nn+t;
  												rep(i,0,10)rep(_,0,TT[i]-(i==t))n=10*n+i;
  												chmin(ans,n);
  											}
  											if(!TT[S[i]-'0'])break;
  											TT[S[i]-'0']--;
  											nn=10*nn+S[i]-'0';
  											if(i==sz(S)-1)chmin(ans,nn);
  										}
  									}
  								}
  							}
  						}
  					}
  				}
  			}
  		}
  	}
  }
  cout<<ans<<endl;
  return 0;
}
0