QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#409021 | #7762. 数据库 | zqs | 20 | 187ms | 4408kb | C++14 | 3.2kb | 2024-05-11 15:51:35 | 2024-05-11 15:51:36 |
Judging History
answer
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstring>
typedef std::pair<int, int> PII;
using std::min;
const int INF = 1000000000;
namespace Flow {
struct Edge {int to, nxt, cap, cost;} e[100005];
int head[5005], dis[5005], delta[5005], tot = 1, s, t;
std::queue<int> QQ;
std::priority_queue<PII, std::vector<PII>, std::greater<PII> > Q;
bool vis[5005], mark[5005], done[5005];
inline void ae(int u, int v, int cap, int cost) {
e[++ tot].to = v, e[tot].cap = cap, e[tot].cost = cost,
e[tot].nxt = head[u], head[u] = tot;
e[++ tot].to = u, e[tot].cap = 0, e[tot].cost = -cost,
e[tot].nxt = head[v], head[v] = tot;
}
bool spfa() {
memset(dis, 0x3f, sizeof dis);
memset(vis, false, sizeof vis);
QQ.push(s), dis[s] = 0;
while (QQ.size()) {
int u = QQ.front(); QQ.pop(); mark[u] = false;
for (int i = head[u]; i; i = e[i].nxt)
if (e[i].cap && dis[u] + e[i].cost < dis[e[i].to]) {
dis[e[i].to] = dis[u] + e[i].cost;
if (!mark[e[i].to]) QQ.push(e[i].to), mark[e[i].to] = true;
}
}
return dis[t] <= INF;
}
bool dijkstra() {
memset(delta, 0x3f, sizeof delta);
memset(vis, false, sizeof vis);
memset(done, false, sizeof done);
Q.push(std::make_pair(delta[s] = 0, s));
while (Q.size()) {
int u = Q.top().second; Q.pop();
if (done[u]) continue; done[u] = true;
for (int i = head[u]; i; i = e[i].nxt)
if (e[i].cap && delta[u] + e[i].cost < delta[e[i].to])
Q.push(std::make_pair(delta[e[i].to] = delta[u] + e[i].cost, e[i].to));
}
for (int i = 1; i <= t; ++ i) dis[i] += delta[i];
return dis[t] <= INF;
}
int dfs(int u, int flow) {
if (u == t) return flow;
vis[u] = true;
int used = 0;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (!vis[v] && dis[u] + e[i].cost == dis[v] && e[i].cap) {
int tmp = dfs(v, min(e[i].cap, flow));
e[i].cap -= tmp, e[i ^ 1].cap += tmp, used += tmp, flow -= tmp;
if (!flow) return used;
}
}
return used;
}
int Dinic() {
int mincost = 0;
if (!spfa()) return 0;
do mincost += dfs(s, INF) * dis[t]; while (dijkstra());
return mincost;
}
}
int w[5005], a[5005], lst[5005], b[5005], n, m, q;
int main() {
scanf("%d%d%d", &n, &m, &q);
int sum = 0;
for (int i = 1; i <= n; ++ i) scanf("%d", w + i);
for (int i = 1; i <= q; ++ i) scanf("%d", a + i);
int newq = 0;
for (int i = 1; i <= q; ++ i) {
int j = i;
while (j < q && a[j + 1] == a[i]) ++ j;
b[++ newq] = a[i], i = j;
}
memcpy(a, b, sizeof a);
for (int i = 1; i <= q; ++ i) Flow::ae(i, i + 1, m - 1, 0);
for (int i = 1; i <= q; ++ i) {
if (lst[a[i]]) Flow::ae(lst[a[i]] + 1, i, 1, -w[a[i]]);
lst[a[i]] = i, sum += w[a[i]];
}
Flow::s = 1, Flow::t = q + 1;
printf("%d", sum + Flow::Dinic());
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 122ms
memory: 4392kb
input:
10 3 5000 23077 34848 88221 8067 83132 62621 41320 69146 32971 27594 2 7 5 3 9 6 1 9 4 8 1 8 8 3 6 9 1 7 5 5 6 8 1 3 10 6 8 7 10 2 1 2 6 7 5 5 9 5 7 10 10 6 6 7 2 4 3 1 10 10 5 1 1 6 1 2 8 9 2 5 10 1 10 7 5 5 10 5 2 5 6 10 9 5 6 3 5 3 6 5 7 4 1 5 8 1 1 9 7 1 1 2 1 8 6 2 9 8 4 2 5 4 5 2 10 4 3 6 9 7 ...
output:
140913750
result:
wrong answer 1st lines differ - expected: '100598924', found: '140913750'
Subtask #2:
score: 20
Accepted
Test #4:
score: 20
Accepted
time: 79ms
memory: 4408kb
input:
10 2 5000 65644 5214 85000 40719 98071 56616 35404 16019 96748 89032 5 9 6 1 3 6 8 10 2 9 1 10 5 9 9 9 2 7 7 7 7 10 5 1 3 10 4 2 5 5 2 8 3 2 1 3 3 6 2 4 5 5 2 5 2 3 4 2 10 1 4 6 10 9 6 4 9 10 5 3 9 7 7 2 1 9 5 8 8 4 8 8 4 5 6 1 3 4 8 10 4 3 6 6 9 2 5 2 8 5 10 4 7 10 3 9 3 2 9 3 10 7 1 3 9 9 3 5 1 3 ...
output:
173524192
result:
ok single line: '173524192'
Test #5:
score: 20
Accepted
time: 51ms
memory: 4360kb
input:
100 2 5000 49570 6371 37107 2261 33457 98048 84700 76277 32602 53831 20995 86351 57905 93492 65198 80688 44394 48442 57924 88655 16250 11904 21033 99426 59241 71456 7697 85276 81310 49884 64543 72091 63969 23936 88032 62420 42269 76663 37639 16930 61480 97674 38809 77434 25043 46618 93378 74399 1031...
output:
231972847
result:
ok single line: '231972847'
Test #6:
score: 20
Accepted
time: 17ms
memory: 4268kb
input:
1000 2 5000 40824 4748 88829 17859 98470 82824 82849 16663 96731 71333 73050 75770 22643 62690 87789 72306 46178 68415 85222 18729 54074 53251 45445 35978 1417 85067 89297 42145 43426 1108 28947 87941 49299 51501 80512 41395 71615 26966 60841 87838 41260 59483 31968 54392 50188 65874 275 38593 32941...
output:
241831233
result:
ok single line: '241831233'
Subtask #3:
score: 0
Wrong Answer
Test #7:
score: 0
Wrong Answer
time: 187ms
memory: 3844kb
input:
5000 15 400 34145 93322 29976 7963 53362 50640 10859 94528 13329 49980 18826 50286 60155 79748 73253 18329 5216 83079 48220 98825 46592 76855 14037 19859 80960 4461 377 70496 28092 99806 5355 27013 92051 95231 65553 32365 3862 89764 86063 93033 12754 68996 38965 52942 69948 34370 3023 52079 16066 57...
output:
19701248
result:
wrong answer 1st lines differ - expected: '19341503', found: '19701248'
Subtask #4:
score: 0
Wrong Answer
Test #10:
score: 0
Wrong Answer
time: 21ms
memory: 3868kb
input:
10 5 1000 86764 81108 88408 93029 87155 18790 28170 29349 81866 77109 8 7 10 7 2 7 1 8 4 10 9 10 4 1 7 1 9 9 1 6 6 1 9 6 7 1 8 10 1 7 9 1 1 9 7 10 8 5 5 1 2 3 10 6 2 6 2 6 1 2 7 8 5 6 10 2 9 8 2 6 8 5 10 8 1 10 4 6 5 6 3 8 1 3 5 2 2 7 2 4 5 5 8 2 4 1 3 6 8 2 2 3 1 8 1 5 8 6 9 7 7 3 7 9 8 9 9 7 5 3 6...
output:
40475379
result:
wrong answer 1st lines differ - expected: '15248002', found: '40475379'
Subtask #5:
score: 0
Skipped
Dependency #1:
0%