結果

問題 No.3558 Dominoes, Black and White
コンテスト
ユーザー GOTKAKO
提出日時 2026-05-29 21:26:01
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 1,257 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,465 ms
コンパイル使用メモリ 220,084 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-05-29 21:26:09
合計ジャッジ時間 5,067 ms
ジャッジサーバーID
(参考情報)
judge4_1 / judge1_0
純コード判定待ち
このコードへのチャレンジ
(要ログイン)
サブタスク 配点 結果
部分点 10 % AC * 30
満点 90 % AC * 89
合計 100 点
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int N; cin >> N;
    stack<pair<long long,long long>> st;
    long long answer = 0;
    for(int x=0; x<N; x++){
        string s; cin >> s;
        long long white = 0,black = 2*N;
        for(int i=0; i<2*N; i++) if(s.at(i) == '.') answer += i-white,white++;
        black -= white;
        if(black > N){
            long long b = black-N; 
            answer += b*(b-1)/2;
            while(st.size() && st.top().second > 0 && b){
                auto &[x2,w] = st.top();
                long long use = min(b,w);
                answer += use*(x-x2+1);
                w -= use,b -= use;
                if(w == 0) st.pop();
            }
            if(b) st.push({x,-b});
        }
        else if(white > N){
            long long w = white-N;
            answer += w*(w-1)/2;
            while(st.size() && st.top().second < 0 && w){
                auto &[x2,b] = st.top();
                long long use = min(-b,w);
                answer += use*(x-x2+1);
                w -= use,b += use;
                if(b == 0) st.pop(); 
            }
            if(w) st.push({x,w});
        }
    }
    cout << answer << endl;
}
0