Submission #155228

# Submission time Handle Problem Language Result Execution time Memory
155228 2019-09-27T07:19:32 Z SpeedOfMagic Popeala (CEOI16_popeala) C++17
17 / 100
2000 ms 12536 KB
/** MIT License Copyright (c) 2018-2019 Vasilev Daniil **/
#include <bits/stdc++.h>
using namespace std;
template<typename T> using v = vector<T>;
//#define int long long
typedef long double ld;
typedef string str;
typedef vector<int> vint;
#define rep(a, l, r) for(int a = (l); a < (r); a++)
#define pb push_back
#define fs first
#define sc second
#define sz(a) ((int) a.size())
const long long inf = 4611686018427387903; //2^62 - 1
const long double EPS = 1e-10;
#if 0  //FileIO
const string fileName = "";
ifstream fin ((fileName == "" ? "input.txt"  : fileName + ".in" ));
ofstream fout((fileName == "" ? "output.txt" : fileName + ".out"));
#define get fin >>
#define put fout <<
#else
#define get cin >>
#define put cout <<
#endif
#define eol put endl
void read() {} template<typename Arg,typename... Args> void read (Arg& arg,Args&... args){get (arg)     ;read(args...) ;}
void print(){} template<typename Arg,typename... Args> void print(Arg  arg,Args...  args){put (arg)<<" ";print(args...);}
int getInt(){int a; get a; return a;}
//code starts here
const int N = 32768;
const int oo = 2000000001;
int val[N * 2], lazy[N * 2];
int ll[N * 2], rr[N * 2], mid[N * 2];
 
void upd(int l, int r, int w, int cur = 1) {
    if (l > r)
        return;
    if (l == ll[cur] && r == rr[cur]) {
        lazy[cur] += w;
        val[cur] += w;
        return;
    }
 
    upd(l, min(r, mid[cur]), w, cur << 1);
    upd(max(l, mid[cur] + 1), r, w, cur << 1 | 1);
    val[cur] = min(val[cur << 1], val[cur << 1 | 1]) + lazy[cur];
}
 
inline int query(int l, int r, int cur = 1, int ll = 1, int rr = N, int laz = 0) {
    if (l > r)
        return oo;
 
    if (l == ll && r == rr)
        return val[cur] + laz;
    int mid = (ll + rr) >> 1; laz += lazy[cur];
    return min(query(l, min(r, mid), cur << 1, ll, mid, laz),
               query(max(l, mid + 1), r, cur << 1 | 1, mid + 1, rr, laz));
}
 
struct quer {
    int l, r, w;
    quer() {}
    quer(int ll, int rr, int ww) : l(ll), r(rr), w(ww) {}
};
 
void run() {
    int n, t, s;
    read(n, t, s);
    int pts[t];
    rep(i, 0, t)
        get pts[i];
 
    char res[n][t];
    rep(i, 0, n) {
        str st;
        get st;
        rep(j, 0, t)
            res[i][j] = st[j] - '0';
    }
 
    v<quer> queries[t];
    rep(i, 0, n) {
        int pr = 0;
        rep(j, 0, t) {
            if (res[i][j])
                queries[j].pb(quer(pr + 1, j, pts[j]));
            else {
				int sum = 0;
				for (int k = j - 1; k > pr; k--) {
					sum += pts[k];
					queries[j].pb({k, k, -sum});
				}
                pr = j;
            }
        }
    }
 
    rep(i, 0, t) {
        v<quer> nw;
 
		v<pair<int, int>> events;
        for (quer j : queries[i]) if (j.w) {
			events.pb({j.l, j.w});
			events.pb({j.r + 1, -j.w});
		}
 
		sort(events.begin(), events.end());
		int cur = 0, pr = -1;
		for (auto j : events) {
			if (pr == -1)
				pr = j.fs;
			else {
				nw.pb({pr, j.fs - 1, cur});
				pr = j.fs;
			}
			cur += j.sc;
		}
		queries[i] = nw;
    }
 
    int dp[t][s];
    rep(i, 0, t)
        rep(j, 0, s)
            dp[i][j] = oo;
 
    int cur[n];
    memset(cur, 0, sizeof cur);
    rep(i, 0, t) {
        rep(j, 0, n)
            if (res[j][i] && cur[j] != -1)
                cur[j] += pts[i];
            else
                cur[j] = -1;
 
        dp[i][0] = 0;
        rep(j, 0, n)
            if (cur[j] != -1)
                dp[i][0] += cur[j];
    }
 
    rep(j, 1, s) {
        memset(lazy, 0, sizeof lazy);
        for (int i = N; i < N + t; i++) {
            val[i] = dp[i - N][j - 1];
            ll[i] = i - N + 1;
            rr[i] = i - N + 1;
            mid[i] = (ll[i] + rr[i]) >> 1;
        }
 
        for (int i = N + t; i < N * 2; i++) {
            val[i] = oo;
            ll[i] = i - N + 1;
            rr[i] = i - N + 1;
            mid[i] = (ll[i] + rr[i]) >> 1;
        }
        for (int i = N - 1; i; i--) {
            val[i] = min(val[i << 1], val[i << 1 | 1]);
            ll[i] = ll[i << 1];
            rr[i] = rr[i << 1 | 1];
            mid[i] = (ll[i] + rr[i]) >> 1;
        }
 
        rep(i, 0, t) {
            for (quer q : queries[i])
		        upd(q.l, q.r, q.w);
            dp[i][j] = min(dp[i][j], query(1, i));
        }
    }
 
    rep(j, 0, s)
        put dp[t - 1][j], eol;
 
 
 /*
    rep(i, 0, s) {
        rep(j, 0, t)
            print(dp[j][i]);
        eol;
    }*/
}
/* 0 8 16
2 3 3
4 3 5
101
110
*//* 7 7
1 2 2
4 3
11
*//* 0 1
1 3 2
2 4 1
011
*//* 0 0
3 4 2
4 5 6 7
1001
0110
1010
*//* 0 10
2 5 2
1 2 3 4 5
01111
11101
*/
signed main() {srand(time(0)); ios::sync_with_stdio(0); cin.tie(0); put fixed << setprecision(12); run();}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1628 KB Output is correct
2 Correct 8 ms 1656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 262 ms 3028 KB Output is correct
2 Correct 285 ms 2936 KB Output is correct
3 Correct 269 ms 2996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1085 ms 7344 KB Output is correct
2 Correct 1641 ms 9704 KB Output is correct
3 Execution timed out 2041 ms 12536 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1628 KB Output is correct
2 Correct 8 ms 1656 KB Output is correct
3 Correct 262 ms 3028 KB Output is correct
4 Correct 285 ms 2936 KB Output is correct
5 Correct 269 ms 2996 KB Output is correct
6 Correct 1085 ms 7344 KB Output is correct
7 Correct 1641 ms 9704 KB Output is correct
8 Execution timed out 2041 ms 12536 KB Time limit exceeded