結果

問題 No.3114 0→1
ユーザー umezo
提出日時 2025-04-19 01:16:05
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 928 bytes
コンパイル時間 3,390 ms
コンパイル使用メモリ 275,368 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-04-19 01:16:10
合計ジャッジ時間 3,837 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /usr/include/c++/13/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:51,
                 from main.cpp:1:
In function ‘constexpr const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = int]’,
    inlined from ‘int main()’ at main.cpp:40:19:
/usr/include/c++/13/bits/stl_algobase.h:238:15: warning: iteration 3 invokes undefined behavior [-Waggressive-loop-optimizations]
  238 |       if (__b < __a)
      |           ~~~~^~~~~
main.cpp: In function ‘int main()’:
main.cpp:4:31: note: within this loop
    4 | #define rep(i,n) for(int i=0;i<(int)(n);i++)
      |                               ^
main.cpp:40:3: note: in expansion of macro ‘rep’
   40 |   rep(j,4) ans=min(ans,dp[n-1][j]);
      |   ^~~

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
template <class T> using V=vector<T>;
template <class T> using VV=V<V<T>>;

int dp[200200][3];
const int INF=1e9;
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int n;
  string s;
  cin>>n>>s;
  rep(i,200200) rep(j,3) dp[i][j]=INF;
  if(s[0]=='0'){
    dp[0][2]=1;
    dp[0][0]=0;
  }
  else dp[0][2]=0;
  for(int i=1;i<n;i++){
    if(s[i]=='1'){
      dp[i][1]=min(dp[i][1],dp[i-1][0]);
      dp[i][2]=min(dp[i][2],dp[i-1][1]);
      dp[i][2]=min(dp[i][2],dp[i-1][2]);
    }
    else{
      dp[i][0]=min(dp[i][0],dp[i-1][2]);
      dp[i][1]=min(dp[i][1],dp[i-1][0]+1);
      dp[i][2]=min(dp[i][2],dp[i-1][1]+1);
      dp[i][2]=min(dp[i][2],dp[i-1][2]+1);
    }
  }
  int ans=INF;
  rep(j,4) ans=min(ans,dp[n-1][j]);
  cout<<ans<<endl;
    
  return 0;
}
0