提出 #45735908


ソースコード 拡げる

#include <bits/stdc++.h>
#pragma GCC optimize(3)
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define F first
#define S second
#define endl '\n'
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 1e2 + 5;

int n, m;
vector<string> a(N);

struct Dinic{
    static constexpr ll Inf = 1e18;
    int n;
    struct Edge{
        int to; ll c;
        Edge(int to, ll c) : to(to), c(c) {}
    };
    vector<Edge> e;
    vector<vector<int>> g;
    vector<int> cur, h;
    Dinic(int n) : n(n), g(n+1) {}
 
    void addEdge(int x, int y, ll w){
        g[x].push_back(e.size());
        e.push_back({y, w});
        g[y].push_back(e.size());
        e.push_back({x, 0});
    }
 
    bool bfs(int s, int t){
        h.assign(n+1, -1);
        queue<int> q;
        h[s] = 0;
        q.push(s);
        while(!q.empty()){
            int p = q.front(); q.pop();
            for(auto id : g[p]){
                auto [j, c] = e[id];
                if(c > 0 && h[j] == -1){
                    h[j] = h[p] + 1;
                    if(j == t) return true;
                    q.push(j);
                }
            }
        }
        return false;
    }
 
    ll dfs(int i, int t, ll f){
        if(i == t) return f;
        ll r = f;
        for(int &ci = cur[i]; ci<g[i].size(); ci++){
            int id = g[i][ci];
            auto [j, c] = e[id];
            if(c > 0 && h[j] == h[i] + 1){
                ll tmp = dfs(j, t, min(r, c));
                e[id].c -= tmp;
                e[id ^ 1].c += tmp;
                r -= tmp;
                if(r == 0) return f;
            }
        }
        return f - r;
    }
 
    ll maxFlow(int s, int t){
        ll ans = 0;
        while(bfs(s, t)){
            cur.assign(n+1, 0);
            ans += dfs(s, t, Inf);
        }
        return ans;
    }
};


void solve(){
    cin >> n >> m;
    for(int i=1; i<=n; i++) cin >> a[i];
    
    int ans = inf;
    vector<vector<int>> b(n+1);

    auto check = [&](int x) -> bool {
        // 1 ~ n => n + 1 + t, 
        vector<int> t;
        for(int i=1; i<=n; i++) for(auto j : b[i]){
            if(j <= x) t.push_back(j);
            else break;
        }

        sort(t.begin(), t.end());
        t.erase(unique(t.begin(), t.end()), t.end());
        int cnt = t.size();

        Dinic flow(n + cnt + 5);
        int S = n + cnt + 1;
        int T = S + 1;

        for(int i=1; i<=n; i++) flow.addEdge(S, i, 1);
        for(int i=1; i<=n; i++){
            for(auto j : b[i]){
                if(j > x) break;
                auto it = lower_bound(t.begin(), t.end(), j) - t.begin() + 1;
                flow.addEdge(i, n + it, 1);
            }
        }
        for(int i=n+1; i<=n+cnt; i++) flow.addEdge(i, T, 1);
        auto mf = flow.maxFlow(S, T);
        return mf == n;
    };

    for(char c='0'; c<='9'; c++){
        for(int i=1; i<=n; i++) b[i].clear();
        bool ok = 1;
        for(int i=1; i<=n; i++){
            for(int j=0; j<m; j++){
                if(a[i][j] == c){
                    b[i].push_back(j);
                }
            }
            if(b[i].empty()){
                ok = 0;
                break;
            }
        }
        if(!ok) continue;

        for(int i=1; i<=n; i++){
            int cur = 0;
            while(b[i].size() < n){
                b[i].push_back(b[i][cur] + m);
                cur++;
            }
        }

        int ma = 0, mi = inf;
        for(int i=1; i<=n; i++){
            ma = max(ma, b[i].back());
            mi = min(mi, b[i][0]);
        }

        // if(c == '8'){
        //     for(int i=1; i<=n; i++){
        //         for(auto j : b[i]) cout << j << " ";
        //         cout << endl;
        //     }
        //     cout << "mi=" << mi << " ma=" << ma << endl;

        // }
        
        int l = mi, r = ma;
        while(l <= r){
            int mid = (l+r) >> 1;
            auto ok = check(mid);
            if(ok) r = mid - 1;
            else l = mid + 1;
        }

        ans = min(ans, l);
    }

    cout << (ans == inf ? -1 : ans) << endl;

}  


int main(){
    ios::sync_with_stdio(0); cin.tie(0);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    
    int t =1;
    //cin >> t;
    while(t--)
    solve();
    return 0;
}

提出情報

提出日時
問題 G - Slot Strategy 2 (Hard)
ユーザ diobrando97
言語 C++ 20 (gcc 12.2)
得点 0
コード長 4533 Byte
結果 TLE
実行時間 2216 ms
メモリ 381592 KiB

コンパイルエラー

Main.cpp: In member function ‘long long int Dinic::dfs(int, int, long long int)’:
Main.cpp:58:33: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   58 |         for(int &ci = cur[i]; ci<g[i].size(); ci++){
      |                               ~~^~~~~~~~~~~~
Main.cpp: In function ‘void solve()’:
Main.cpp:137:31: warning: comparison of integer expressions of different signedness: ‘std::vector<int>::size_type’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]
  137 |             while(b[i].size() < n){
      |                   ~~~~~~~~~~~~^~~

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 0 / 600
結果
AC × 4
AC × 25
TLE × 2
セット名 テストケース
Sample sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
All random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
ケース名 結果 実行時間 メモリ
random_01.txt AC 1645 ms 73572 KiB
random_02.txt AC 212 ms 14068 KiB
random_03.txt AC 696 ms 29024 KiB
random_04.txt AC 90 ms 8988 KiB
random_05.txt AC 1654 ms 73504 KiB
random_06.txt AC 1053 ms 47840 KiB
random_07.txt AC 606 ms 25832 KiB
random_08.txt AC 702 ms 28748 KiB
random_09.txt AC 1121 ms 381592 KiB
random_10.txt AC 17 ms 10784 KiB
random_11.txt AC 5 ms 4100 KiB
random_12.txt AC 1 ms 3616 KiB
random_13.txt AC 13 ms 13956 KiB
random_14.txt AC 33 ms 15176 KiB
random_15.txt AC 13 ms 14104 KiB
random_16.txt AC 32 ms 14292 KiB
random_17.txt AC 18 ms 14424 KiB
random_18.txt AC 36 ms 14492 KiB
random_19.txt AC 15 ms 13576 KiB
random_20.txt AC 36 ms 14316 KiB
random_21.txt TLE 2216 ms 132584 KiB
random_22.txt TLE 2212 ms 134768 KiB
random_23.txt AC 1 ms 3432 KiB
sample_01.txt AC 1 ms 3544 KiB
sample_02.txt AC 1 ms 3464 KiB
sample_03.txt AC 1 ms 3488 KiB
sample_04.txt AC 1 ms 3544 KiB