結果

問題 No.3015 右に寄せろ!
ユーザー applejam
提出日時 2025-01-25 13:25:03
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,160 bytes
コンパイル時間 2,555 ms
コンパイル使用メモリ 136,132 KB
実行使用メモリ 5,996 KB
最終ジャッジ日時 2025-01-25 22:45:50
合計ジャッジ時間 3,962 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using mint = modint998244353;
#define rep(i, n) for (int i = 0; i< (int)(n); i++) 

int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    string s; cin >> s;
    int len = s.length();
    int tmp = 0;
    ll t = 0;
    ll ans = 0;
    stack<int> st;
    int state = 0;
    for(int i = len -1 ; i >= 0; i--){
        if(s[i] == '0'){
            if(state == 0){
                tmp++;
            }else{
                tmp = 1;
                state = 0;
            }
        }else{
            if(state == 0){
                if(tmp > 0){
                    st.push(tmp);
                    t += tmp;
                }
                state = 1;
                tmp = 0;
            }else{
                ans += t;
                if(st.size() > 0){
                    t -= st.top();
                    tmp = st.top();
                    st.pop();
                }
                state = 0;
            }
        }
    }
    cout << ans << endl;
    return 0;
}
0