結果

問題 No.2162 Copy and Paste 2
ユーザー 👑 p-adicp-adic
提出日時 2024-04-26 07:19:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,800 ms / 7,000 ms
コード長 9,806 bytes
コンパイル時間 2,854 ms
コンパイル使用メモリ 220,524 KB
実行使用メモリ 36,252 KB
最終ジャッジ日時 2024-04-26 07:20:18
合計ジャッジ時間 26,916 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
12,888 KB
testcase_01 AC 4 ms
12,816 KB
testcase_02 AC 4 ms
12,800 KB
testcase_03 AC 4 ms
12,828 KB
testcase_04 AC 5 ms
12,876 KB
testcase_05 AC 4 ms
13,208 KB
testcase_06 AC 192 ms
26,704 KB
testcase_07 AC 258 ms
30,544 KB
testcase_08 AC 686 ms
35,920 KB
testcase_09 AC 398 ms
26,068 KB
testcase_10 AC 453 ms
25,832 KB
testcase_11 AC 699 ms
33,132 KB
testcase_12 AC 722 ms
30,288 KB
testcase_13 AC 1,129 ms
35,852 KB
testcase_14 AC 531 ms
26,832 KB
testcase_15 AC 549 ms
26,816 KB
testcase_16 AC 561 ms
26,704 KB
testcase_17 AC 1,557 ms
36,252 KB
testcase_18 AC 1,772 ms
36,176 KB
testcase_19 AC 1,653 ms
36,072 KB
testcase_20 AC 1,577 ms
36,172 KB
testcase_21 AC 883 ms
36,168 KB
testcase_22 AC 846 ms
36,048 KB
testcase_23 AC 269 ms
36,048 KB
testcase_24 AC 1,766 ms
36,060 KB
testcase_25 AC 1,800 ms
36,044 KB
testcase_26 AC 1,695 ms
36,052 KB
testcase_27 AC 1,757 ms
36,204 KB
testcase_28 AC 1,421 ms
26,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define _GLIBCXX_DEBUG 
#pragma GCC optimize ( "O3" )
//#pragma GCC target ( "avx" )
#include <bits/stdc++.h>
using namespace std;

using uint = unsigned int;
using ll = long long;

#define TYPE_OF( VAR ) remove_const<remove_reference<decltype( VAR )>::type >::type
#define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr ) 
#define CEXPR( LL , BOUND , VALUE ) constexpr const LL BOUND = VALUE 
#define CIN( LL , A ) LL A; cin >> A 
#define ASSERT( A , MIN , MAX ) assert( MIN <= A && A <= MAX ) 
#define CIN_ASSERT( A , MIN , MAX ) CIN( TYPE_OF( MAX ) , A ); ASSERT( A , MIN , MAX ) 
#define GETLINE( A ) string A; getline( cin , A ) 
#define GETLINE_SEPARATE( A , SEPARATOR ) string A; getline( cin , A , SEPARATOR ) 
#define FOR( VAR , INITIAL , FINAL_PLUS_ONE ) for( TYPE_OF( FINAL_PLUS_ONE ) VAR = INITIAL ; VAR < FINAL_PLUS_ONE ; VAR ++ ) 
#define FOREQ( VAR , INITIAL , FINAL ) for( TYPE_OF( FINAL ) VAR = INITIAL ; VAR <= FINAL ; VAR ++ ) 
#define FOREQINV( VAR , INITIAL , FINAL ) for( TYPE_OF( INITIAL ) VAR = INITIAL ; VAR >= FINAL ; VAR -- ) 
#define FOR_ITR( ARRAY , ITR , END ) for( auto ITR = ARRAY .begin() , END = ARRAY .end() ; ITR != END ; ITR ++ ) 
#define REPEAT( HOW_MANY_TIMES ) FOR( VARIABLE_FOR_REPEAT , 0 , HOW_MANY_TIMES ) 
#define QUIT return 0 
#define COUT( ANSWER ) cout << ( ANSWER ) << "\n"; 
#define RETURN( ANSWER ) COUT( ANSWER ); QUIT 
#define DOUBLE( PRECISION , ANSWER ) cout << fixed << setprecision( PRECISION ) << ( ANSWER ) << "\n"; QUIT 

#define POWER( ANSWER , ARGUMENT , EXPONENT )				\
  TYPE_OF( ARGUMENT ) ANSWER{ 1 };					\
  {									\
    TYPE_OF( ARGUMENT ) ARGUMENT_FOR_SQUARE_FOR_POWER = ( ARGUMENT );	\
    TYPE_OF( EXPONENT ) EXPONENT_FOR_SQUARE_FOR_POWER = ( EXPONENT );	\
    while( EXPONENT_FOR_SQUARE_FOR_POWER != 0 ){			\
      if( EXPONENT_FOR_SQUARE_FOR_POWER % 2 == 1 ){			\
	ANSWER *= ARGUMENT_FOR_SQUARE_FOR_POWER;			\
      }									\
      ARGUMENT_FOR_SQUARE_FOR_POWER *= ARGUMENT_FOR_SQUARE_FOR_POWER;	\
      EXPONENT_FOR_SQUARE_FOR_POWER /= 2;				\
    }									\
  }									\


#define POWER_MOD( ANSWER , ARGUMENT , EXPONENT , MODULO )		\
  TYPE_OF( ARGUMENT ) ANSWER{ 1 };					\
  {									\
    TYPE_OF( ARGUMENT ) ARGUMENT_FOR_SQUARE_FOR_POWER = ( MODULO + ( ARGUMENT ) % MODULO ) % MODULO; \
    TYPE_OF( EXPONENT ) EXPONENT_FOR_SQUARE_FOR_POWER = ( EXPONENT );	\
    while( EXPONENT_FOR_SQUARE_FOR_POWER != 0 ){			\
      if( EXPONENT_FOR_SQUARE_FOR_POWER % 2 == 1 ){			\
	ANSWER = ( ANSWER * ARGUMENT_FOR_SQUARE_FOR_POWER ) % MODULO;	\
      }									\
      ARGUMENT_FOR_SQUARE_FOR_POWER = ( ARGUMENT_FOR_SQUARE_FOR_POWER * ARGUMENT_FOR_SQUARE_FOR_POWER ) % MODULO; \
      EXPONENT_FOR_SQUARE_FOR_POWER /= 2;				\
    }									\
  }									\


#define FACTORIAL_MOD( ANSWER , ANSWER_INV , MAX_I , LENGTH , MODULO )	\
  ll ANSWER[LENGTH];							\
  ll ANSWER_INV[LENGTH];						\
  {									\
    ll VARIABLE_FOR_PRODUCT_FOR_FACTORIAL = 1;				\
    ANSWER[0] = VARIABLE_FOR_PRODUCT_FOR_FACTORIAL;			\
    FOREQ( i , 1 , MAX_I ){						\
      ANSWER[i] = ( VARIABLE_FOR_PRODUCT_FOR_FACTORIAL *= i ) %= MODULO; \
    }									\
    POWER_MOD( FACTORIAL_MAX_INV , ANSWER[MAX_I] , MODULO - 2 , MODULO ); \
    ANSWER_INV[MAX_I] = FACTORIAL_MAX_INV;				\
    FOREQINV( i , MAX_I - 1 , 0 ){					\
      ANSWER_INV[i] = ( FACTORIAL_MAX_INV *= i + 1 ) %= MODULO;		\
    }									\
  }									\
									\

// 通常の二分探索
#define BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET )		\
  ll ANSWER = MAXIMUM;							\
  {									\
    ll VARIABLE_FOR_BINARY_SEARCH_L = MINIMUM;				\
    ll VARIABLE_FOR_BINARY_SEARCH_U = ANSWER;				\
    ll VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( TARGET ) - ( EXPRESSION ); \
    if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH == 0 ){		\
      VARIABLE_FOR_BINARY_SEARCH_L = ANSWER;				\
    } else {								\
      ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \
    }									\
    while( VARIABLE_FOR_BINARY_SEARCH_L != ANSWER ){			\
      VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( TARGET ) - ( EXPRESSION ); \
      if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH == 0 ){		\
	VARIABLE_FOR_BINARY_SEARCH_L = ANSWER;				\
	break;								\
      } else {								\
	if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH > 0 ){		\
	  VARIABLE_FOR_BINARY_SEARCH_L = ANSWER;			\
	} else {							\
	  VARIABLE_FOR_BINARY_SEARCH_U = ANSWER;			\
	}								\
	ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \
      }									\
    }									\
  }									\
									\


// 二進法の二分探索
#define BS2( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET )		\
  ll ANSWER = MINIMUM;							\
  {									\
    ll VARIABLE_FOR_POWER_FOR_BINARY_SEARCH_2 = 1;			\
    ll VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( MAXIMUM ) - ANSWER; \
    while( VARIABLE_FOR_POWER_FOR_BINARY_SEARCH_2 <= VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH ){ \
      VARIABLE_FOR_POWER_FOR_BINARY_SEARCH_2 *= 2;			\
    }									\
    VARIABLE_FOR_POWER_FOR_BINARY_SEARCH_2 /= 2;			\
    ll VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH_2 = ANSWER;		\
    while( VARIABLE_FOR_POWER_FOR_BINARY_SEARCH_2 != 0 ){		\
      ANSWER = VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH_2 + VARIABLE_FOR_POWER_FOR_BINARY_SEARCH_2; \
      VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( TARGET ) - ( EXPRESSION ); \
      if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH == 0 ){		\
	VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH_2 = ANSWER;		\
	break;								\
      } else if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH > 0 ){	\
	VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH_2 = ANSWER;		\
      }									\
      VARIABLE_FOR_POWER_FOR_BINARY_SEARCH_2 /= 2;			\
    }									\
    ANSWER = VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH_2;			\
  }									\
									\


template <typename T> inline T Absolute( const T& a ){ return a > 0 ? a : - a; }
template <typename T> inline T Residue( const T& a , const T& p ){ return a >= 0 ? a % p : p - ( - a - 1 ) % p - 1; }


// 区間最大値取得可能なBITを頑張って実装したのですが1点更新しかできないパターンだった、
// というのを2回繰り返してとても疲弊したので、遅延セグ木の実装は今回諦めてペタリします。。
#include <atcoder/lazysegtree>
using namespace atcoder;

inline CEXPR( int , bound , 200000 );
// laze_segtree用のデータ
inline int op( int m , int n ) { return m < n ? n : m;}
// 0は負数も考える設定だとunitでないことに注意
inline int unit() { return -bound-1; }

int main()
{
  UNTIE;
  CIN( string , S );
  int size = S.size();
  ASSERT( size , 1 , bound );
  // Sの長さi+jの始切片の長さjの終切片がSの長さjの始切片と一致するようなj<=iの最大値
  static int j_update[bound + 1];
  // 再帰の都合まずj<=iという条件を無視して計算する
  j_update[0] = size;
  int j_curr = 0;
  int j_lim , j_sub , j_update_j_sub;
  FOREQ( i , 1 , size ){
    int& j_update_i = j_update[i];
    j_lim = size - i;
    j_update_i = j_lim;
    while( j_curr < j_lim ){
      if( S.substr( j_curr , 1 ) != S.substr( i + j_curr , 1 ) ){
	j_update_i = j_curr;
	break;
      }
      j_curr++;
    }
    if( j_curr != 0 ){
      j_sub = 1;
      j_update_j_sub = j_update[j_sub];
      while( j_sub + j_update_j_sub < j_curr ){
	j_update[++i] = j_update_j_sub;
	j_sub++;
	j_update_j_sub = j_update[j_sub];
      }
      j_curr -= j_sub;
    }
  }
  // 最大値を記録したjごとにiを格納
  static set<int> i_update[bound + 1] = {};
  // 最大値を記録したjを渡らせてiを格納
  set<int> i_update_total{};
  // 条件j<=iを反映
  FOREQ( i , 0 , size ){
    int& j_update_i = j_update[i];
    if( j_update_i > i ){
      j_update_i = i;
    }
    if( j_update_i > 0 ){
      i_update[j_update_i].insert( i );
      i_update_total.insert( i );
    }
  }
  // i >= j に対し
  // a(i,j) := (TがSの始切片かつ|T|=iかつ|U|=jとなる時の最小操作数)
  // i > jかつUがTの終切片 -> a(i,j) = min( a(i-1,j) + 1 , a(i-j,j) + 1 )
  // i > jかつUがTの終切片でない -> a(i,j) = a(i-1,j) + 1
  // a(i,i) = min( int j = 0 ; j < i ; j++ ) a(i,j) + 1
  // dp(i,j) := i - a(i,j)
  // i > jかつ(Sの長さiの始切片の長さjの終切片がSの長さjの始切片) -> dp(i,j) = max( dp(i-1,j) , dp(i-j,j)+(j-1) )
  // i > jかつ(Sの長さiの始切片の長さjの終切片がSの長さjの始切片) -> dp(i,j) = dp(i-1,j)
  // dp(i,i) = max( int j = 0 ; j < i ; j++ ) dp(i,j) - 1
  // max_{j'<=j} dp(i,j')の値を(j,i)に関する辞書式順序で計算してopt[i]に格納
  lazy_segtree<int,op,unit,int,op,op,unit> opt( size + 1 );
  // a(i,0)=iよりd(i,0) = 0
  // unitが入ってしまっているので0で初期化が必要
  opt.apply( 0 , size + 1 , 0 );
  int current_update , i;
  set<int>::iterator itr_i , end_i;
  bool updating;
  // 最後が操作Cであることはないのでsize未満までで良い
  FOR( j , 1 , size ){
    // 右辺はmax_{j'<j} dp(j,j') - 1 = dp(j,j)
    current_update = opt.get( j ) - 1;
    i = j;
    end_i = i_update_total.end();
    updating = true;
    while( updating ){
      current_update += j - 1;
      itr_i = i_update_total.lower_bound( i );
      if( updating = ( itr_i != end_i ) ){
  	i = *itr_i + j;
  	if( updating = ( i <= size ) ){
  	  opt.apply( i , size + 1 , current_update );
  	}
      }
    }
    set<int>& i_update_j = i_update[j];
    FOR_ITR( i_update_j , itr_j , end_j ){
      // dp(i,j+1)以降はSの長さi+jの始切片の長さjの終切片がSの長さjの始切片と一致することの寄与がない
      i_update_total.erase( *itr_j );
    }
  }
  // size - ( max_{j'<=size-1} dp(size,j') ) = min_{j'<size} a(size,j');
  RETURN( size - opt.get( size ) );
}
0