QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#309363#8131. Filesystemucup-team1055#WA 1ms3760kbC++204.4kb2024-01-20 16:54:082024-01-20 16:54:09

Judging History

你现在查看的是最新测评结果

  • [2024-01-20 16:54:09]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3760kb
  • [2024-01-20 16:54:08]
  • 提交

answer

#include <bits/stdc++.h>

#define rep(i,s,n) for(int i = int(s); i < int(n); i++)
#define rrep(i,s,n) for(int i = int(n) - 1; i >= int(s); i--)
#define all(v) (v).begin(), (v).end()

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<class T>
bool chmin(T &a, T b) {
    if(a <= b) return false;
    a = b;
    return true;
}

template<class T>
bool chmax(T &a, T b) {
    if(a >= b) return false;
    a = b;
    return true;
}

using namespace std;

template <class Cap> struct mf_graph {
    struct edge{
        int from, to;
        Cap cap, flow;
    };

    struct nedge {
        int to, rev;
        Cap cap;
    };

    int nn;
    vector<pair<int,int>> pos;
    vector<vector<nedge>> g;

    mf_graph(): nn(0){}
    explicit mf_graph(int n):nn(n), g(n){}
    
    int add_edge(int from, int to, Cap cap){
        int m = pos.size();
        pos.push_back({from, int(g[from].size())});
        int frid = int(g[from].size());
        int toid = int(g[to].size());
        if (from==to) toid++;
        g[from].push_back(nedge{to,toid,cap});
        g[to].push_back(nedge{from,frid,0});
        return m; 
    }
    Cap flow(int s, int t){
        return flow(s, t, numeric_limits<Cap>::max());
    }
    Cap flow(int s, int t, Cap flow_limit){
        vector<int> lv(nn), iter(nn);
        queue<int> q;
        auto bfs = [&](){
            fill(all(lv), -1);
            lv[s] = 0;
            queue<int>().swap(q);
            q.push(s);
            while(!q.empty()){
                int v = q.front();
                q.pop();
                for (auto e:g[v]){
                    if (e.cap == 0 || lv[e.to] >= 0) continue;
                    lv[e.to] = lv[v] + 1;
                    if (e.to == t) return;
                    q.push(e.to);
                }
            }
        };
        auto dfs = [&](auto self, int v, Cap up){
            if (v == s) return up;
            Cap res = 0;
            int lvvv = lv[v];
            for (int& i = iter[v]; i < int(g[v].size()); i++){
                nedge& e = g[v][i];
                if (lvvv <= lv[e.to] || g[e.to][e.rev].cap == 0) continue;
                Cap d = self(self, e.to, min(up-res, g[e.to][e.rev].cap));
                if (d <= 0) continue;
                g[v][i].cap += d;
                g[e.to][e.rev].cap -= d;
                res += d;
                if (res==up) return res;
            }
            lv[v] = nn;
            return res;
        };
        Cap flow = 0;
        while(flow < flow_limit){
            bfs();
            if (lv[t] == -1) break;
            fill(all(iter), 0);
            Cap f = dfs(dfs,t,flow_limit-flow);
            if (!f) break;
            flow += f;
        }
        return flow;
    }

};

void solve(){
    int n, k; cin >> n >> k;
    vector<int> u(k);
    rep(i,0,k){
        cin >> u[i];
        u[i]--;
    }
    
    vector<bool> targ(n);
    rep(i,0,k){
        targ[u[i]] = 1;
    }
    
    vector<int> p(n);
    rep(i,0,n){
        cin >> p[i];
        p[i]--;
    }

    vector<int> q(n);
    rep(i,0,n){
        q[p[i]] = i;
    }

    mf_graph<int> mf(n + 2);
    rep(i,0,n){
        if (!targ[i]) continue;
        if (i==0){
            mf.add_edge(n, i, 1);
        }else{
            if (!targ[i-1]){
                mf.add_edge(n, i, 1);
            }else{
                mf.add_edge(i, i-1, 1);
            }
        }

        if (i==n-1){
            mf.add_edge(n, i, 1);
        }else{
            if (!targ[i+1]){
                mf.add_edge(n, i, 1);
            }else{
                mf.add_edge(i, i+1, 1);
            }
        }
    }
    rep(i,0,n){
        if (!targ[q[i]]) continue;

        if (i==0){
            mf.add_edge(q[i], n+1, 1);
        }else{
            if (!targ[q[i-1]]){
                mf.add_edge(q[i], n+1, 1);
            }else{
                mf.add_edge(q[i], q[i-1], 1);
            }
        }
        
        if (i==n-1){
            mf.add_edge(q[i], n+1, 1);
        }else{
            if (!targ[q[i+1]]){
                mf.add_edge(q[i], n+1, 1);
            }else{
                mf.add_edge(q[i], q[i+1], 1);
            }
        }
    }

    cout << mf.flow(n, n+1) /2 << '\n';
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    int t; cin >> t;
    while(t--) solve();
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3536kb

input:

2
12 8
2 5 8 3 4 10 12 1
10 5 8 6 4 7 2 3 11 12 1 9
8 4
1 3 5 7
1 4 5 8 7 6 3 2

output:

3
4

result:

ok 2 number(s): "3 4"

Test #2:

score: -100
Wrong Answer
time: 1ms
memory: 3760kb

input:

100
10 3
10 5 2
2 4 9 8 7 3 1 5 10 6
10 3
8 1 10
8 4 3 7 1 2 9 5 6 10
10 3
6 5 2
8 7 6 4 2 10 1 3 5 9
10 3
5 8 4
10 4 5 7 8 9 1 2 3 6
10 3
8 4 10
10 6 9 2 8 7 1 4 3 5
10 3
9 8 1
8 5 6 10 2 4 1 7 9 3
10 3
5 4 1
7 5 8 4 3 6 9 10 2 1
10 3
2 4 3
6 7 3 9 1 2 5 8 4 10
10 3
9 5 3
6 10 7 4 9 3 1 8 5 2
10 3
...

output:

2
3
2
2
2
1
2
1
3
3
2
2
2
3
2
2
2
2
3
3
3
2
1
2
3
2
2
3
1
3
1
2
2
2
2
2
2
2
3
2
3
3
3
3
2
2
2
3
2
2
2
2
2
3
3
2
2
2
2
2
2
2
2
1
2
1
2
3
2
2
2
2
3
1
2
2
3
3
2
1
2
2
3
2
3
2
2
2
2
2
2
2
1
2
2
1
2
2
2
2

result:

wrong answer 5th numbers differ - expected: '3', found: '2'