QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#639779#9457. Prime SettovarischAC ✓459ms23052kbC++233.1kb2024-10-13 22:24:202024-10-13 22:24:20

Judging History

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

  • [2024-10-13 22:24:20]
  • 评测
  • 测评结果:AC
  • 用时:459ms
  • 内存:23052kb
  • [2024-10-13 22:24:20]
  • 提交

answer

#include <bits/stdc++.h>

#define ff first
#define ss second
#define mp make_pair
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;

struct edge { int v, cap, inv, flow; };
struct network {
	int n, s, t; 
	vector<int> lvl;
	vector<vector<edge>> g;

	network(int n_): n(n_), lvl(n_), g(n_) {}
	void add_edge(int u, int v, int c) {
		g[u].pb({v, c, (int)g[v].size(), 0});
		g[v].push_back({u, 0, (int)g[u].size()-1, c});
	}

	bool bfs() {
		fill(lvl.begin(), lvl.end(), -1);
		queue <int> q;
		lvl[s] = 0;
		for (q.push(s); q.size(); q.pop()) {
			int u = q.front();
			for (auto& e : g[u]) {
				if (e.cap > 0 && lvl[e.v] == -1) {
					lvl[e.v] = lvl[u] + 1;
					q.push(e.v);
				}
			}
		}

		return lvl[t] != -1;
	}

	int dfs(int u, int nf) {
		if (u == t) return nf;
		int res = 0;
		for (auto& e : g[u]) {
			if (e.cap >0 && lvl[e.v] == lvl[u] + 1) {
				int tf = dfs(e.v, min(nf, e.cap));
				res += tf; nf -= tf; e.cap -= tf;
				g[e.v][e.inv].cap += tf;
				g[e.v][e.inv].flow -= tf;
				e.flow += tf;
				if (nf == 0) return res;
			}
		}

		if (!res) lvl[u] = -1;
		return res;
	}

	int max_flow(int so, int si, int res = 0) {
		s = so; t = si;
		while (bfs()) res += dfs(s, INT_MAX);
		return res;
	}
};

const int maxn = 2e6 + 5;
bool sieve[maxn];

int main(){
	#ifdef LOCAL
		freopen("in","r", stdin);
		freopen("out","w",stdout);
	#endif
	ios_base::sync_with_stdio(0); cin.tie(0);

	sieve[0] = sieve[1] = 1;
	for (int i=2; i<maxn; ++i) {
		for (int j = i+i; j<maxn; j += i) sieve[j] = 1;
	}

	int tt; cin >> tt;
	while (tt--) {
		int n, k; cin >> n >> k;
		vector<int> a(n); 
		for (int i=0; i<n; ++i) cin >> a[i];

		sort(a.begin(), a.end());
		vector<pair<int, int>> b;
		for (int i=0, j=0; i<n; i=j) {
			while (j<n && a[i] == a[j]) ++j;
			b.pb({a[i], j - i});
		}

		int m = b.size(), ones = 0;
		vector<bool> good(m, 0);

		int s = m, t = m+1;
		network net(m+3);
		for (int i = 0; i<m; ++i) {
			int ai = b[i].ff, c = b[i].ss;
			if (ai == 1) { ones = c; continue; }

			if (ai&1) net.add_edge(i, t, c);
			else {
				net.add_edge(s, i, c);

				for (int j=0; j<m; ++j) {
					int aj = b[j].ff;
					if (!sieve[ai + aj]) {

						net.add_edge(i, j, INT_MAX);
						good[i] = good[j] = 1;
					}
				}
			}
		}


		auto solve = [&](int& flow, int rem) -> int {
			flow = net.max_flow(s, t, flow);

			int total = flow + (rem / 2);
			if(total >= k) return 2*k;

			int ans = 2*total;
			for(edge& e : net.g[s]) if (good[e.v]) {
				int mn = min(k - total, e.cap);
				total += mn, ans += mn;
			}

			for (edge& e : net.g[t]) if (good[e.v]) {
				int mn = min(k - total, e.flow);
				total += mn, ans += mn;
			}

			int mn = min(rem%2, k - total);
			if (good[0] || (rem > 1 || ones > rem)) total += mn, ans += mn;

			return ans;
		};

		int flow = 0;	
		int ans = solve(flow, ones);
		for (int i=1; i<=ones; ++i) {
			net.add_edge(0, t, 1);
			ans = max(ans, solve(flow, ones - i));
		}

		cout << ans << '\n';
	}
	return 0;
}

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

详细

Test #1:

score: 100
Accepted
time: 37ms
memory: 5520kb

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: 459ms
memory: 23052kb

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