QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#580604#9315. Rainbow Bracket SequenceQingyyxWA 1ms7960kbC++204.0kb2024-09-21 22:40:272024-09-21 22:40:28

Judging History

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

  • [2024-09-21 22:40:28]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:7960kb
  • [2024-09-21 22:40:27]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long 
#define enl putchar('\n')
#define N 605
#define M 500005 
using namespace std;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
char buf[1 << 21], * p1 = buf, * p2 = buf, obuf[1 << 21], * o = obuf, of[35];
#define gc()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline ll qpow(ll a, ll n) { ll res = 1; while (n) { if (n & 1)res = res * a % mod; n >>= 1; a = a * a % mod; }return res; }
inline int read() { int s = 0, f = 1; char c = gc(); for (; !isdigit(c); c = gc())if (c == '-')f = -1; for (; isdigit(c); c = gc())s = s * 10 + c - '0'; return s * f; }
inline void read(int* a, int n) { for (int i = 1; i <= n; ++i)a[i] = read(); }
inline int inal(char* s) { int n = 0; for (s[0] = gc(); !isalpha(s[0]); s[0] = gc()); for (; isalpha(s[n]); s[++n] = gc()); return s[n] = 0, n; }
int n, m, S, T;
int head[N], tot, cur[N], dis[N]; ll cst;
bool vis[N];
struct node {
    int to, nxt, val, ct;
}e[M << 1];
queue<int>que;
inline void add(int u, int v, int w, int c) {
    // printf("%d %d %d\n", u, v, c);
    e[tot] = {v, head[u], w, c};
    head[u] = tot++;

    e[tot] = {u, head[v], 0, -c};
    head[v] = tot++;
}
inline bool spfa() {
    while (!que.empty())que.pop();
    memset(dis, 0x7f, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    memcpy(cur, head, sizeof(head));
    // for (int i = 1; i <= n; ++i)cur[i] = head[i];
    dis[S] = 0;

    que.push(S);
    while (!que.empty()) {
        int t = que.front();
        que.pop();
        vis[t] = 0;
        for (int i = head[t]; ~i; i = e[i].nxt) {
            int to = e[i].to;
            if (dis[to] > dis[t] + e[i].ct && e[i].val) {
                dis[to] = dis[t] + e[i].ct;
                if (!vis[to]) {
                    vis[to] = 1;
                    que.push(to);
                }
            }
        }
    }
    if (dis[T] ^ 0x7f7f7f7f)return true;
    else return false;
}
inline ll dfs(int now, int lim) {
    if (!lim || now == T)return lim;
    int flow = 0, f;
    vis[now] = 1;
    for (int i = cur[now]; ~i; i = e[i].nxt) {
        cur[now] = i;
        if (!vis[e[i].to] && dis[e[i].to] == dis[now] + e[i].ct && (f = dfs(e[i].to, min(lim, e[i].val)))) {
            flow += f;
            cst += (ll)f * e[i].ct;
            lim -= f;
            e[i].val -= f;
            e[i ^ 1].val += f;
            if (!lim)break;
        }
    }
    vis[now] = 0;
    return flow;
}
inline int MCMF() {
    ll ans = 0;
    while (spfa())
        ans += dfs(S, 0x7fffffff);
    return ans;
}
int du[N];

int l[N], c[N], v[N];

void solve() {
    memset(head, -1, sizeof(head));
    memset(du, 0, sizeof(du));
    cst = tot = 0;



    n = read() * 2, m = read();
    read(l, m);
    read(c, n); read(v, n);
    int s = n + n + m + 1, t = s + 1;
    S = t + 1, T = S + 1;
    int sum = 0;
    for (int i = 2; i <= n; ++i)
        add(i, i - 1, n, 0);
    for (int i = 1; i <= n; ++i)
        add(i, i + n, 1, 0);
    for (int i = 1; i <= n; i += 2)
        add(s, i, 1, 0);
    for (int i = 1; i <= n; ++i)
        add(i + n, n + n + c[i], 1, inf - v[i]);
    for (int i = 1; i <= m; ++i)
        add(n + n + i, t, n, 0);
    for (int i = 1; i <= m; ++i)
        du[n + n + i] -= l[i],
        du[t] += l[i],
        sum += l[i];
    for (int i = n + n + 1; i <= t; ++i) {
        if (du[i] > 0)add(S, i, du[i], 0);
        if (du[i] < 0)add(i, T, -du[i], 0);
    }
    add(t, s, inf, 0);
    int ans = MCMF();
    if (ans < sum)printf("-1\n");
    else printf("%lld\n", 1ll * (n / 2) * inf - cst);
}


signed main() {
    clock_t c1 = clock();
#ifdef LOCAL
    freopen("in.in", "r", stdin);
    freopen("out.out", "w", stdout);
#endif
    //=============================================================
    int TxT = read();
    while (TxT--)
        solve();
    //=============================================================
#ifdef LOCAL
    end :
    cerr << "Time Used:" << clock() - c1 << "ms" << endl;
#endif
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 7960kb

input:

2
3 2
1 2
1 2 2 2 1 2
3 1 4 2 2 1
3 2
2 2
1 2 2 2 1 2
3 1 4 2 2 1

output:

9
-1

result:

ok 2 number(s): "9 -1"

Test #2:

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

input:

50
8 6
0 2 2 0 3 1
3 5 1 1 5 3 5 2 3 2 2 1 2 2 4 6
998133227 879226371 59632864 356493387 62611196 827258251 296576565 204244054 812713672 780267148 614679390 447700005 102067050 544546349 116002772 761999375
1 1
1
1 1
343766215 374461155
3 1
2
1 1 1 1 1 1
796323508 303640854 701432076 853325162 610...

output:

-1
343766215
2710758237
4996234876
-1
-1
1892561958
2539318868
1013080942
5341457976
-1
-1
2231197660
5002664193
6366657402
5995341117
-1
1903045212
6750751734
5590178776
-1
5172233034
-1
4182716768
5268471900
9549986103
9864154610
-1
5297975014
2122219134
883992368
1900878794
-1
5698622606
-1
29785...

result:

wrong answer 3rd numbers differ - expected: '2351080746', found: '2710758237'