QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#635863#9457. Prime Setucup-team296#AC ✓41ms31700kbC++204.5kb2024-10-12 21:14:302024-10-12 21:14:37

Judging History

你现在查看的是测评时间为 2024-10-12 21:14:37 的历史记录

  • [2024-10-13 19:28:09]
  • 自动重测本题所有获得100分的提交记录
  • 测评结果:AC
  • 用时:44ms
  • 内存:31668kb
  • [2024-10-13 19:27:24]
  • hack成功,自动添加数据
  • (/hack/967)
  • [2024-10-13 18:39:09]
  • 管理员手动重测本题所有得分≥97分的提交记录
  • 测评结果:100
  • 用时:42ms
  • 内存:30164kb
  • [2024-10-13 18:24:51]
  • hack成功,自动添加数据
  • (/hack/965)
  • [2024-10-13 18:17:54]
  • hack成功,自动添加数据
  • (/hack/963)
  • [2024-10-12 21:14:37]
  • 评测
  • 测评结果:100
  • 用时:41ms
  • 内存:31700kb
  • [2024-10-12 21:14:30]
  • 提交

answer

#include <bits/stdc++.h>

#define long long long int
#define DEBUG
using namespace std;

// @author: pashka

struct maxflow {
    struct edge {
        int src, dst, cap, flw;
    };

    vector<edge> edges;
    vector<vector<int>> graph;
    vector<int> first;
    vector<int> dist;

    maxflow(int n) {
        graph.resize(n);
    }

    void bfs(int s) {
        dist.assign(graph.size(), -1);
        dist[s] = 0;
        int h = 0;
        vector<int> q;
        q.push_back(s);
        while (h < q.size()) {
            int x = q[h++];
            for (int i = 0; i < graph[x].size(); i++) {
                edge &e = edges[graph[x][i]];
                if (e.flw == e.cap) continue;
                int y = e.dst;
                if (dist[y] == -1) {
                    dist[y] = dist[x] + 1;
                    q.push_back(y);
                }
            }
        }
    }

    int dfs(int x, int t, int f) {
        if (f == 0) return 0;
        if (x == t) return f;
        for (int i = first[x]; i < graph[x].size(); i++) {
            first[x] = i;
            edge &e = edges[graph[x][i]];
            edge &er = edges[graph[x][i] ^ 1];
            if (e.flw == e.cap || dist[e.dst] != dist[e.src] + 1)
                continue;
            int y = e.dst;
            int res = dfs(y, t, std::min(f, e.cap - e.flw));
            if (res > 0) {
                e.flw += res;
                er.flw -= res;
                return res;
            }
        }
        return 0;
    }

    long maxFlow(int s, int t) {
        long flow = 0;
        while (true) {
            bfs(s);
            first.assign(graph.size(), 0);
            if (dist[t] == -1) break;
            while (true) {
                int pushed = dfs(s, t, INT_MAX);
                if (pushed > 0) {
                    flow += pushed;
                } else {
                    break;
                }
            }
        }
        return flow;
    }

    int addEdge(int u, int v, int cap) {
//    cout << u << " " << v << "\n";
        edges.push_back({u, v, cap, 0});
        edges.push_back({v, u, 0, 0});
        graph[u].push_back(edges.size() - 2);
        graph[v].push_back(edges.size() - 1);
        return edges.size() - 2;
    }
};

const int MAX = 2000001;
vector<bool> prime(MAX, true);

struct test {
    void solve() {
        int n, k;
        cin >> n >> k;
        map<int, int> c0, c1;
        for (int i = 0; i < n; i++) {
            int x;
            cin >> x;
            if (x % 2 == 0) {
                c0[x]++;
            } else {
                c1[x]++;
            }
        }
        vector<int> p0, p1;
        for (auto [x, y]: c0) p0.push_back(x);
        for (auto [x, y]: c1) p1.push_back(x);
        int n0 = p0.size();
        int n1 = p1.size();
        maxflow mf(n0 + n1 + 2);
        int s = n0 + n1;
        int t = s + 1;
        for (int i = 0; i < n0; i++) {
            mf.addEdge(s, i, c0[p0[i]]);
        }
        for (int i = 0; i < n1; i++) {
            if (p1[i] != 1) {
                mf.addEdge(n0 + i, t, c1[p1[i]]);
            }
        }
        vector<bool> g0(n0), g1(n1);
        for (int i = 0; i < n0; i++) {
            for (int j = 0; j < n1; j++) {
                if (prime[p0[i] + p1[j]]) {
                    g0[i] = true;
                    g1[j] = true;
                    mf.addEdge(i, n0 + j, n);
                }
            }
        }
        if (c1[1] > 1) {
            g1[0] = true;
        }
        int gn = 0;
        for (int i = 0; i < n0; i++) {
            if (g0[i]) gn += c0[p0[i]];
        }
        for (int i = 0; i < n1; i++) {
            if (g1[i]) gn += c1[p1[i]];
        }
        int r = mf.maxFlow(s, t);

        if (n1 > 0 && p1[0] == 1) {
            int e = mf.addEdge(n0, t, c1[1]);
            r += mf.maxFlow(s, t);
            r += (mf.edges[e].cap - mf.edges[e].flw) / 2;
        }

        //        cout << "! " << r << " " << gn << "\n";
        r = min(r, k);
        int res = min(r * 2 + (k - r), gn);

        cout << res << "\n";
    }
};

int main() {
    ios::sync_with_stdio(false);

    prime[0] = prime[1] = false;
    for (int i = 2; i * i < MAX; i++) {
        if (prime[i]) {
            for (int j = i * i; j < MAX; j += i) {
                prime[j] = false;
            }
        }
    }

    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        test().solve();
    }

    return 0;
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 8ms
memory: 3784kb

input:

4
4 2
2 3 4 5
5 3
3 4 12 3 6
6 3
1 3 6 8 1 1
1 0
1

output:

4
3
6
0

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 41ms
memory: 31700kb

input:

110
3 3
2 2 2
3 1
1 1 1
3 2
1 1 1
3 3
1 1 1
4 2
1 1 1 1
10 7
1 1 1 2 4 6 8 10 12 14
18 1
10 5 9 4 10 7 2 4 9 1 6 1 4 9 2 4 9 7
19 5
1 6 1 8 8 1 5 10 9 2 10 2 1 9 6 10 4 3 6
14 4
6 6 8 9 5 3 4 9 2 2 1 10 10 1
15 10
5 4 2 4 10 1 8 4 10 3 10 3 7 10 4
17 5
10 7 2 4 3 1 10 8 2 8 3 4 1 1 1 1 1
20 6
8 4 6 ...

output:

0
2
3
3
4
8
2
10
8
15
10
12
10
10
12
16
10
10
12
16
14
13
13
14
17
0
19
12
12
11
16
10
19
2
12
10
5
14
10
10
13
2
18
2
4
4
11
8
11
8
0
10
10
0
10
0
8
15
12
11
13
18
10
17
9
8
20
19
13
6
4
6
11
9
13
11
6
2
8
12
17
14
6
2
20
2
18
17
6
2
10
0
7
16
2
2
0
2
18
14
11
8
4
6
0
19
890
204
2746
2372

result:

ok 110 lines

Extra Test:

score: 0
Extra Test Passed