提出 #577768


ソースコード 拡げる

#include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;

typedef long long ll;

string to_string1(ll i){
  stringstream ss;
  ss << i;
  return ss.str();
}

string toString(ll v,int keta) {
  stringstream ss;
  ss << v;
  string ret;
  ss >> ret;
  if( (int)ret.size() < keta ) {
    string tmp = "";
    rep(i,keta-(int)ret.size()) tmp += "0";
    tmp += ret;
    ret = tmp;
  }
  return ret;
}

void setter(string &s,int prefix){
  string pres = to_string1(prefix);
  rep(i,(int)pres.size()) s[i] = pres[i];
}

bool used2[11];
bool isValid(string s){
  memset(used2,false,sizeof(used2));
  rep(i,(int)s.size()) {
    if( used2[s[i]-'0'] ) return false;
    used2[s[i]-'0'] = true;
  }
  return true;
}


bool used[11];
int N;
ll pow10_;
string compute(string s){
  string ans = "";
  memset(used,false,sizeof(used));
  rep(i,(int)s.size()) {
    if( !used[s[i]-'0'] ) {
      used[s[i]-'0'] = true;
      ans += string(1,s[i]);
    } else {
      int prefix = (atoi)(s.substr(0,i+1).c_str());
      int L = prefix, R = prefix;

      int which = -1;
      while(1) {
	string tmp = s;
	--L;
	if( L < 0 ) {
	  L = pow10_ - 1;
	}
	setter(tmp,L); 
	if( isValid(to_string1(L)) ) {
	  which = 1;
	  ans = to_string1(L);
	  ans = string(i-(int)ans.size()+1,'0') + ans;
	  memset(used,false,sizeof(used));
	  rep(j,(int)ans.size()) used[ans[j]-'0'] = true;
	  break;
	}
	

	++R;	
	if( R >= pow10_ ) {
	  R = 0;
	}
	tmp = s;
	setter(tmp,R);
	if( isValid(to_string1(R)) ) {
	  which = 0;
	  ans = to_string1(R);
	  ans = string(i-(int)ans.size()+1,'0') + ans;
	  memset(used,false,sizeof(used));
	  rep(j,(int)ans.size()) used[ans[j]-'0'] = true;
	  break;
	}

      }
      assert( which != -1 );
      if( which == 1 ) { // max
	int cur = 9;
	while( (int)ans.size() < N ) {
	  if( used[cur] ) { --cur; continue; }
	  used[cur] = true;
	  ans += string(1,(char)(cur+'0'));
	  --cur;
	}
      } else { // min
      	int cur = 0;
	while( (int)ans.size() < N ) {
	  if( used[cur] ) { ++cur; continue; }
	  used[cur] = true;
	  ans += string(1,(char)(cur+'0'));
	  ++cur;
	}
      }
      break;
    }
  }
  return ans;
}


ll getDiffer(string olds,string news) {
  ll o = (atoll)(olds.c_str());
  ll n = (atoll)(news.c_str());
  return min(llabs(o-n),pow10_-llabs(o-n));
}


bool is(int i,int keta){
  memset(used,false,sizeof(used));
  string s = to_string1(i);
  s = string(keta-(int)s.size(),'0') + s;
  rep(i,(int)s.size()) {
    if( used[s[i]-'0'] ) return false;
    used[s[i]-'0'] = true;
  }
  return true;
}

string bf(string s){
 ll answ = -1;
  ll maxi = -1; 
  rep(i,(int)pow10_){
    if(!is(i,(int)s.size())) continue;
    ll v = getDiffer(s,to_string1(i));
    //cout << i <<" -> " << v << endl;
    if( maxi  == -1) {
      maxi = v;
      answ = (ll)i;
    } else if( maxi <= v ) {
      if( maxi == v ) {
	if( answ > i ) answ= i;
      } else {
	answ = i;
      }
      maxi = v;
    }
  }
  return to_string1(answ);
}

int main(){
  



  string s;
  cin >> s;


  /*

  ll answ = -1;
  ll maxi = -1; 
  rep(i,10000){
    if( !is(i)) continue;
    ll v = getDiff(s,to_string(i));
    //cout << v << endl;
    if( maxi  == -1) {
      maxi = v;
      answ = v;
    } else if( maxi <= v ) {
      maxi = v;
      cout << "!" <<i << endl;
      if( answ > i ) answ = i;
    }
  }
  cout << answ << endl;
  return 0;
*/
  string o = s;
  ll v = (atoll)(s.c_str());
  N = (int)s.size() ;
  pow10_ = 1;
  rep(i,N) pow10_ *= 10LL;
  ll five = 1;
  rep(i,N) five *= 10LL;
  five /= 2LL;
  v += five;
  v %= pow10_;
  
  //cout <<"bf =" << bf(s) << endl;
  //cout << "v = " << v << endl;
  string sv = toString(v,N);
  string  ans = "";
  ll diff = getDiffer(o,sv);

  string tmp = compute(sv);
  ll diff_tmp = getDiffer(o,sv);
  if( diff <= diff_tmp ) {
    if( ans.empty() ) {
      ans = tmp;
    } else {
      if( diff == diff_tmp ) {
	if( (atoll)(ans.c_str()) > (atoll)(tmp.c_str()) ) {
	  ans = tmp;
	}
      } else {
	ans = tmp;
      }
    }

    diff = diff_tmp;
    //cout << "diff = " << diff_tmp << endl;
  }

    //cout << string(1,'z') << endl;
  cout << ans << endl;
  return 0;
}

提出情報

提出日時
問題 B - Change a Password
ユーザ konoha
言語 C++ (GCC 4.4.7)
得点 0
コード長 4382 Byte
結果 WA
実行時間 5033 ms
メモリ 928 KiB

ジャッジ結果

セット名 All
得点 / 配点 0 / 100
結果
AC × 48
WA × 11
TLE × 2
セット名 テストケース
All 00_test_00, 00_test_01, 00_test_02, 00_test_03, 01_rand_00, 01_rand_01, 01_rand_02, 01_rand_03, 01_rand_04, 01_rand_05, 01_rand_06, 01_rand_07, 01_rand_08, 01_rand_09, 01_rand_10, 01_rand_11, 01_rand_12, 01_rand_13, 01_rand_14, 01_rand_15, 01_rand_16, 01_rand_17, 01_rand_18, 01_rand_19, 01_rand_20, 01_rand_21, 01_rand_22, 01_rand_23, 01_rand_24, 01_rand_25, 01_rand_26, 01_rand_27, 01_rand_28, 01_rand_29, 01_rand_30, 01_rand_31, 01_rand_32, 01_rand_33, 01_rand_34, 01_rand_35, 01_rand_36, 01_rand_37, 01_rand_38, 01_rand_39, 01_rand_40, 01_rand_41, 01_rand_42, 01_rand_43, 01_rand_44, 01_rand_45, 01_rand_46, 01_rand_47, 01_rand_48, 01_rand_49, 99_handmake_00, 99_handmake_01, 99_handmake_02, 99_handmake_03, 99_handmake_04, 99_handmake_05, 99_handmake_06
ケース名 結果 実行時間 メモリ
00_test_00 AC 24 ms 920 KiB
00_test_01 AC 23 ms 924 KiB
00_test_02 AC 24 ms 800 KiB
00_test_03 AC 23 ms 924 KiB
01_rand_00 AC 24 ms 676 KiB
01_rand_01 WA 23 ms 928 KiB
01_rand_02 AC 24 ms 928 KiB
01_rand_03 AC 23 ms 800 KiB
01_rand_04 WA 25 ms 700 KiB
01_rand_05 AC 24 ms 924 KiB
01_rand_06 AC 23 ms 928 KiB
01_rand_07 AC 22 ms 800 KiB
01_rand_08 AC 24 ms 676 KiB
01_rand_09 AC 24 ms 796 KiB
01_rand_10 AC 24 ms 804 KiB
01_rand_11 AC 22 ms 800 KiB
01_rand_12 AC 27 ms 920 KiB
01_rand_13 WA 24 ms 796 KiB
01_rand_14 AC 24 ms 796 KiB
01_rand_15 WA 22 ms 792 KiB
01_rand_16 AC 24 ms 804 KiB
01_rand_17 AC 24 ms 928 KiB
01_rand_18 AC 24 ms 924 KiB
01_rand_19 AC 22 ms 800 KiB
01_rand_20 AC 24 ms 796 KiB
01_rand_21 AC 24 ms 924 KiB
01_rand_22 AC 23 ms 928 KiB
01_rand_23 WA 23 ms 800 KiB
01_rand_24 AC 24 ms 924 KiB
01_rand_25 WA 24 ms 920 KiB
01_rand_26 AC 24 ms 928 KiB
01_rand_27 AC 27 ms 928 KiB
01_rand_28 AC 25 ms 920 KiB
01_rand_29 WA 25 ms 924 KiB
01_rand_30 AC 25 ms 800 KiB
01_rand_31 WA 25 ms 924 KiB
01_rand_32 AC 24 ms 804 KiB
01_rand_33 AC 24 ms 800 KiB
01_rand_34 AC 24 ms 916 KiB
01_rand_35 AC 25 ms 804 KiB
01_rand_36 AC 25 ms 928 KiB
01_rand_37 AC 24 ms 672 KiB
01_rand_38 AC 24 ms 924 KiB
01_rand_39 AC 23 ms 924 KiB
01_rand_40 AC 22 ms 672 KiB
01_rand_41 AC 23 ms 924 KiB
01_rand_42 AC 24 ms 924 KiB
01_rand_43 WA 24 ms 796 KiB
01_rand_44 AC 24 ms 804 KiB
01_rand_45 AC 25 ms 796 KiB
01_rand_46 WA 23 ms 676 KiB
01_rand_47 AC 22 ms 928 KiB
01_rand_48 AC 25 ms 672 KiB
01_rand_49 AC 25 ms 804 KiB
99_handmake_00 AC 25 ms 924 KiB
99_handmake_01 AC 24 ms 796 KiB
99_handmake_02 AC 23 ms 928 KiB
99_handmake_03 AC 24 ms 800 KiB
99_handmake_04 WA 24 ms 800 KiB
99_handmake_05 TLE 5032 ms 804 KiB
99_handmake_06 TLE 5033 ms 868 KiB