結果

問題 No.3658 Darumaka Number 2
コンテスト
ユーザー rinrion
提出日時 2026-08-30 22:23:01
言語 C++14
(gcc 15.3.0 + boost 1.92.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,605 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,500 ms
コンパイル使用メモリ 250,108 KB
実行使用メモリ 9,808 KB
最終ジャッジ日時 2026-08-30 22:23:11
合計ジャッジ時間 4,829 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39 WA * 6
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vs = vector<string>;
using vpi = vector<pair<int, int>>;
using vpl = vector<pair<ll, ll>>;
#define rep(i, s, n) for (int i = (s); i < (int)(n); ++i)
#define repr(i, s, n) for (int i = (s); i >= (int)(n); --i)
#define sz(x) ((int)(x).size())
template<typename T> bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;}
template<typename T> bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;}
auto _ = []{ios::sync_with_stdio(false); cin.tie(nullptr); return 0;}();
const int INFI = 1 << 30;
const ll INFL = 1LL << 62;

int main() {
	string s;
	cin >> s;
	int n=sz(s);
	int bf=-1; // 最後に5が出現した位置
	string ans="";
	rep (i, 0, n){
		if(s[i]=='5'){
			bf=i;
			ans.push_back ('5');
		}
		else if(s[i]=='4')ans.push_back ('4');
		else if(s[i]>'5'){ // 6以上
			// i以降を5にする
			rep (j, i, n){
				ans.push_back ('5');
			}
			break;
		}
		else {
			// 3以下
			if(bf==-1){ // 1度も5が出現していない
				ans="";
				rep (j, 0, n-1){
					ans.push_back ('5'); // 桁数-1個の5
				}
				break;
			}
			else{
				ans[bf]='4'; // 最後に出現した5を4に
				int siz=sz(ans);
				rep (j, bf+1, i){ // ansのbf以降を5に
					s[j]='5';
				}
				rep (j, i, n){
					ans.push_back ('5'); // n桁になるまで5を追加
				}
				break;
			}
		}
	}
	cout << ans << '\n';
	
	return 0;
}
0