Submission #145340

# Submission time Handle Problem Language Result Execution time Memory
145340 2019-08-19T15:59:33 Z MKopchev Homecoming (BOI18_homecoming) C++14
0 / 100
172 ms 107880 KB
#include <bits/stdc++.h>
#include "homecoming.h"
#define endl '\n'

using namespace std;
const int MAXN = (1 << 22);

template<class FlowT>
struct max_flow
{
    const static FlowT finf = 1e18 + 42 + 17;
    const static FlowT feps = 0;

    struct edge
    {
        FlowT flow, cap;
        int idx, rev, to;
        edge() { flow = 0; cap = 0; rev = 0; idx = 0; to = 0; }
        edge(int _to, int _rev, FlowT _flow, FlowT _cap, int _idx)
        {
            to = _to; rev = _rev;
            flow = _flow; cap = _cap;
            idx = _idx;
        }
    };

    vector<edge> G[MAXN];
    int n, dist[MAXN], po[MAXN];

    bool bfs(int s, int t)
    {
        dist[s] = -1, po[s] = 0;
        dist[t] = -1, po[t] = 0;
        for(int v = 0; v <= n; v++)
            dist[v] = -1, po[v] = 0;

        queue<int> Q;
        Q.push(s);
        dist[s] = 0;

        while(!Q.empty())
        {
            int u = Q.front();
            Q.pop();

            for(edge e: G[u])
                if(dist[e.to] == -1 && e.flow < e.cap)
                {
                    dist[e.to] = dist[u] + 1;
                    Q.push(e.to);
                }
        }

        return dist[t] != -1;
    }

    FlowT dfs(int u, int t, FlowT fl = finf)
    {
        if(u == t)
            return fl;

        for(; po[u] < G[u].size(); po[u]++)
        {
            auto &e = G[u][po[u]];
            if(dist[e.to] == dist[u] + 1 && e.flow < e.cap)
            {
                FlowT f = dfs(e.to, t, min(fl, e.cap - e.flow));

                e.flow += f;
                G[e.to][e.rev].flow -= f;

                if(f > 0)
                    return f;
            }
        }

        return 0;
    }

    void init(int _n) { n = _n; for(int i = 0; i <= n; i++) G[i].clear(); }

    void add_edge(int u, int v, FlowT w, int idx = -1)
    {
        //cout<<u<<" "<<v<<" "<<w<<endl;
        G[u].push_back(edge(v, G[v].size(), 0, w, idx));
        G[v].push_back(edge(u, G[u].size() - 1, 0, 0, -1));
    }

    FlowT flow(int s, int t)
    {
        if(s == t) return finf;

        FlowT ret = 0, to_add;
        while(bfs(s, t))
            while((to_add = dfs(s, t)))
                ret += to_add;

        return ret;
    }
};

max_flow<long long> f;

long long int solve(int N, int K, int *A, int *B)
{
    /*
    f.init(2*N+5);
    for(int i=1;i<=N;i++)
        f.add_edge(0,i,A[i-1]);
    for(int i=1;i<=N;i++)
        f.add_edge(N+i,2*N+1,B[i-1]);

    for(int i=0;i<N;i++)
        for(int add=0;add<K;add++)
        {
            int j=(i+add)%N;
            f.add_edge(i+1,N+j+1,1e18);
        }
    long long ret=0;
    for(int i=0;i<N;i++)
        ret=ret+A[i];
    return ret-f.flow(0,2*N+1);
    */
        long long ret=0;
    for(int i=0;i<N;i++)
        ret=ret+A[i];

    vector<int> order={};
    for(int i=0;i<N;i++)order.push_back(i);
    random_shuffle(order.begin(),order.end());

    for(auto i:order)
        for(int add=0;add<K;add++)
        {
            int j=(i+add)%N;
            long long rem=min(A[i],B[j]);
            ret=ret-rem;
            A[i]=A[i]-rem;
            B[j]=B[j]-rem;
        }
    return ret;
}
# Verdict Execution time Memory Grader output
1 Correct 93 ms 98960 KB Output is correct
2 Correct 93 ms 98936 KB Output is correct
3 Incorrect 93 ms 98812 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 93 ms 98960 KB Output is correct
2 Correct 93 ms 98936 KB Output is correct
3 Incorrect 93 ms 98812 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 172 ms 107880 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 93 ms 98960 KB Output is correct
2 Correct 93 ms 98936 KB Output is correct
3 Incorrect 93 ms 98812 KB Output isn't correct
4 Halted 0 ms 0 KB -