QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#386148#7605. Yet Another Mex Problembear0131WA 7ms20568kbC++145.9kb2024-04-11 13:01:232024-04-11 13:01:23

Judging History

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

  • [2024-04-11 13:01:23]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:20568kb
  • [2024-04-11 13:01:23]
  • 提交

answer

#include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef array<int, 3> ai3;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int Mod = 1e9 + 7;
inline int sign(int a){ return (a&1) ? (Mod-1) : 1; }
inline void uadd(int &a, int b){ a += b-Mod; a += (a>>31) & Mod; }
inline void usub(int &a, int b){ a -= b, a += (a>>31) & Mod; }
inline void umul(int &a, int b){ a = (int)(1ll * a * b % Mod); }
inline int add(int a, int b){ a += b-Mod; a += (a>>31) & Mod; return a; }
inline int sub(int a, int b){ a -= b, a += (a>>31) & Mod; return a; }
inline int mul(int a, int b){ a = (int)(1ll * a * b % Mod); return a; }
int qpow(int b, ll p){ int ret = 1; while(p){ if(p&1) umul(ret, b); umul(b, b), p >>= 1; } return ret; }
const int fN = 100;
int fact[fN], invfact[fN], inv[fN];
void initfact(int n){
	fact[0] = 1; for(int i = 1; i <= n; ++i) fact[i] = mul(fact[i-1], i);
	invfact[n] = qpow(fact[n], Mod-2); for(int i = n; i > 0; --i) invfact[i-1] = mul(invfact[i], i);
	for(int i = 1; i <= n; ++i) inv[i] = mul(invfact[i], fact[i-1]);
}
int binom(int n, int m){ return (m < 0 || m > n) ? 0 : mul(fact[n], mul(invfact[m], invfact[n-m])); }
const double pi = acos(-1);
inline void chmax(int &a, int b){ (b>a) ? (a=b) : 0; }
inline void chmin(int &a, int b){ (b<a) ? (a=b) : 0; }
inline void chmax(ll &a, ll b){ (b>a) ? (a=b) : 0; }
inline void chmin(ll &a, ll b){ (b<a) ? (a=b) : 0; }

int n;

struct line{
	ll k, b;
	line(){ k = 0, b = -INF; }
	line(ll _k, ll _b){ k = _k, b = _b; }
	inline ll gety(ll x){ return k * x + b; }
};

namespace ktt{
	// max(f[j] + pre[j] * (-x))
	line dat[200200];
	const int B = 450;
	int nxt[200200], out[200200];
	void rebuild(int bi){
		int bl = bi * B, br = min(n, (bi+1) * B);
		for(int i = br-1; i >= bl; --i){
			if(nxt[i] >= br) out[i] = i;
			else out[i] = out[nxt[i]];
		}
	}

	int h[200200], sp = 0;
	bool elim(int a, int b, int c){
		return (__int128)(dat[b].k - dat[a].k) * (dat[c].b - dat[a].b) - (__int128)(dat[c].k - dat[a].k) * (dat[b].b - dat[a].b) >= 0;
	}
	void ins(int i, line l){
		//cerr << "! ins " << i << endl;
		nxt[i] = inf, dat[i] = l;
		rebuild(i / B);
		while(sp >= 2 && elim(h[sp-2], h[sp-1], i)) nxt[h[--sp]] = i, rebuild(h[sp] / B);
		h[sp++] = i;
	}
	ll qry(int tl, int x){
		////cerr << "! qry " << tl << " " << x << endl;
		ll ret = -INF;
		for(int i = tl; i < n; ++i) chmax(ret, dat[i].gety(x));
		return ret;
		//{
		//	int lb = 0, ub = sp-1;
		//	while(lb <= ub){
		//		int mid = (lb+ub) >> 1;
		//		if(mid < sp-1 && dat[h[mid+1]].gety(x) > dat[h[mid]].gety(x))
		//			lb = mid+1;
		//		else 
		//			ub = mid-1;
		//	}
		//	ret = dat[h[ub]].gety(x);
		//}
		//{
		//	auto isup = [&](int p){
		//		return nxt[p] != inf && dat[nxt[p]].gety(x) > dat[p].gety(x);
		//	};
		//	int p = tl;
		//	while(isup(out[p])) p = nxt[out[p]];
		//	while(isup(p)) p = nxt[p];
		//	chmax(ret, dat[p].gety(x));
		//}
		//return ret;
	}
}

ll pre[200200];

namespace lct{
	// max(k * pre[j] + b)
	line dat[525252];
	void ins(int tl, int tr, line cur, int l, int r, int k){
		//cout << "ins " << tl << " " << tr << " " << l << " " << r << " " << k << endl;
		if(tl > r || l > tr) return ;
		int mid = (l+r) >> 1;
		if(tl <= l && r <= tr){
			if(dat[k].gety(pre[mid]) < cur.gety(pre[mid])) swap(cur, dat[k]);
			if(l == r) return ;
			if(dat[k].gety(pre[l]) < cur.gety(pre[l])) ins(tl, tr, cur, l, mid, k+k);
			if(dat[k].gety(pre[r]) < cur.gety(pre[r])) ins(tl, tr, cur, mid+1, r, k+k+1);
		} else 
			ins(tl, tr, cur, l, mid, k+k), ins(tl, tr, cur, mid+1, r, k+k+1);
	}
	ll qry(int p, int l, int r, int k){
		ll ret = dat[k].gety(pre[p]);
		if(l < r){
			int mid = (l+r) >> 1;
			chmax(ret, (p <= mid) ? qry(p, l, mid, k+k) : qry(p, mid+1, r, k+k+1));
		}
		return ret;
	}
}

int k, a[200200];
vi ps[200200];
multiset< array<int, 3> > st; // mex, left bound, start time
ll f[200200];

void ins(int fl, int fr, int tl, int tr, int v){
	//cout << "------ins " << fl << " " << fr << " " << tl << " " << tr << " " << v << "--------" << endl;
	for(int i = max(fl, tl - k); i <= min(fr, tr - k); ++i){
		lct::ins(tl, i + k, line(v, f[i] - v * pre[i]), 0, n, 1);
	}
	if(tr - k < fr){
		ll b = ktt::qry(tr - k + 1, -v);
		lct::ins(tl, tr, line(v, b), 0, n, 1);
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> n >> k;
	rep(i, n) cin >> a[i], ps[a[i]].push_back(i);
	rep(i, n) pre[i+1] = pre[i] + a[i];

	{
		static int cnt[200200];
		int cur = 0;
		rep(i, n){
			++cnt[a[i]];
			if(cnt[cur]){
				while(cnt[cur]) ++cur;
				st.insert({cur, i, 0});
			}
		}
		st.insert({inf, n, 0});
	}

	memset(ktt::nxt, 0x3f, sizeof(ktt::nxt));

	f[0] = 0, ktt::ins(0, line(pre[0], f[0]));
	rep(i, n + 1){
		//cout << "----------------" << i << "-------------------" << endl;
		//for(auto p : st) cout << "{" << p[0] << ", " << p[1] << ", " << p[2] << "} ";
		//cout << endl;
		if(i) f[i] = max(f[i-1], lct::qry(i, 0, n, 1)), ktt::ins(i, line(pre[i], f[i]));
		//cout << "f[" << i << "] = " << f[i] << "\n";
		if(i < n){
			auto it = st.lower_bound({a[i], 0, 0});
			int nxt = (int)(lower_bound(ps[a[i]].begin(), ps[a[i]].end(), i+1) - ps[a[i]].begin());
			//cout << "[" << nxt << "] = ";
			nxt = (nxt == (int)ps[a[i]].size() ? n : ps[a[i]][nxt]);
			//cout << nxt << endl;
			int tmpl = (*it)[1];
			if(tmpl < nxt){
				int ok = 1;
				while(ok){
					int r = (*next(it))[1];
					ins((*it)[2], i, (*it)[1] + 1, r, (*it)[0]);
					if(nxt <= r) ok = 0;
					if(nxt < r) st.insert({(*it)[0], nxt, (*it)[2]});
					it = st.erase(it);
				}
				if(a[i]) st.insert({a[i], tmpl, i});
			}
		}
	}

	cout << f[n] << "\n";

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 20516kb

input:

5 3
3 4 0 0 3

output:

10

result:

ok 1 number(s): "10"

Test #2:

score: 0
Accepted
time: 7ms
memory: 20436kb

input:

8 4
0 1 2 0 3 1 4 1

output:

26

result:

ok 1 number(s): "26"

Test #3:

score: 0
Accepted
time: 0ms
memory: 20424kb

input:

10 5
0 2 0 1 2 1 0 2 2 1

output:

33

result:

ok 1 number(s): "33"

Test #4:

score: 0
Accepted
time: 0ms
memory: 20552kb

input:

20 10
3 1 3 2 3 3 0 1 3 0 2 3 3 3 3 1 3 0 0 3

output:

160

result:

ok 1 number(s): "160"

Test #5:

score: 0
Accepted
time: 0ms
memory: 20432kb

input:

30 10
14 15 10 1 14 1 8 0 12 8 6 15 1 8 9 12 15 10 11 12 7 10 10 3 3 10 8 14 13 13

output:

172

result:

ok 1 number(s): "172"

Test #6:

score: 0
Accepted
time: 0ms
memory: 20544kb

input:

40 3
0 4 2 4 3 1 5 5 2 3 4 2 1 1 1 5 5 4 1 3 3 0 1 0 2 0 1 4 2 1 5 3 0 4 0 0 0 5 3 3

output:

51

result:

ok 1 number(s): "51"

Test #7:

score: 0
Accepted
time: 0ms
memory: 20404kb

input:

50 20
29 6 30 26 8 11 22 26 24 8 30 25 19 12 28 19 28 4 13 9 2 23 30 15 21 5 30 5 19 17 25 29 2 28 20 16 0 4 26 23 22 30 3 25 29 5 29 24 11 27

output:

378

result:

ok 1 number(s): "378"

Test #8:

score: 0
Accepted
time: 0ms
memory: 20492kb

input:

80 15
2 13 20 11 12 10 19 17 3 7 10 2 14 11 9 17 19 11 17 15 10 18 11 11 14 5 20 8 8 12 13 17 14 19 11 20 13 2 12 2 19 12 6 7 3 4 11 16 1 12 4 16 17 4 1 2 5 11 17 12 13 7 8 12 2 4 15 20 18 1 1 13 1 14 6 5 20 12 12 11

output:

0

result:

ok 1 number(s): "0"

Test #9:

score: 0
Accepted
time: 0ms
memory: 20548kb

input:

100 30
41 50 33 22 1 34 7 14 31 44 46 49 49 23 33 12 9 41 25 23 22 1 49 45 45 8 10 49 37 23 48 17 32 44 38 21 29 27 23 27 47 44 6 12 7 2 18 12 9 43 20 26 26 8 14 25 18 48 2 41 49 7 48 38 10 45 34 27 17 12 1 19 9 29 50 13 25 21 9 13 26 15 6 9 5 13 47 30 26 17 40 0 21 6 25 24 22 31 43 16

output:

1308

result:

ok 1 number(s): "1308"

Test #10:

score: 0
Accepted
time: 3ms
memory: 20548kb

input:

200 30
1 26 8 2 6 36 43 49 15 48 39 7 12 18 28 46 13 6 24 17 43 44 31 17 30 5 40 19 13 24 26 1 23 39 34 29 28 2 22 23 32 21 32 5 38 11 18 10 15 14 16 7 40 40 35 30 8 3 46 25 36 50 13 37 16 42 39 9 24 5 8 2 15 17 24 35 39 26 5 24 39 23 47 17 23 49 21 30 36 46 26 15 0 24 23 25 12 16 12 18 42 30 20 0 5...

output:

3389

result:

ok 1 number(s): "3389"

Test #11:

score: 0
Accepted
time: 0ms
memory: 20568kb

input:

300 30
39 28 33 6 10 36 27 31 1 1 9 41 27 39 48 30 43 49 0 11 39 36 15 40 43 28 18 15 17 23 4 9 37 32 5 34 3 4 45 44 18 24 6 23 18 21 40 7 18 38 35 14 5 44 4 10 25 34 14 8 23 43 28 11 22 13 44 16 30 49 40 13 21 32 50 30 29 31 27 35 1 30 10 49 42 33 46 30 47 48 13 5 5 41 22 3 26 26 33 20 34 41 46 27 ...

output:

6636

result:

ok 1 number(s): "6636"

Test #12:

score: 0
Accepted
time: 0ms
memory: 20560kb

input:

400 30
25 30 7 36 38 37 10 15 37 6 4 49 42 34 43 13 46 40 1 6 35 29 50 13 30 23 48 12 43 23 32 44 28 28 1 41 2 31 44 40 5 1 6 17 50 5 40 5 48 36 32 47 20 24 25 42 17 40 8 43 9 10 43 34 30 36 48 48 37 18 21 23 26 20 24 2 44 10 22 46 38 12 50 4 9 17 19 30 6 25 1 20 33 33 21 6 15 11 27 22 2 25 22 30 8 ...

output:

5312

result:

ok 1 number(s): "5312"

Test #13:

score: 0
Accepted
time: 3ms
memory: 20468kb

input:

500 30
11 7 6 40 43 14 47 49 22 9 25 32 6 4 12 48 25 31 2 26 30 46 10 36 43 46 2 34 45 48 11 28 43 22 47 47 1 32 41 36 41 3 31 8 31 14 12 2 2 8 0 30 34 5 46 46 6 20 25 27 46 3 34 8 36 33 27 4 19 10 3 32 33 9 49 24 9 15 18 6 0 20 13 11 28 1 18 30 18 4 12 34 39 50 20 35 30 47 46 24 46 36 49 34 21 10 7...

output:

9118

result:

ok 1 number(s): "9118"

Test #14:

score: 0
Accepted
time: 0ms
memory: 20504kb

input:

600 30
49 8 31 19 46 14 31 32 33 39 20 15 46 25 6 32 2 48 28 20 26 39 44 9 5 43 31 30 23 47 39 10 33 42 44 3 26 7 15 6 28 31 5 2 11 24 11 1 6 6 21 10 25 36 16 26 23 27 19 10 33 47 49 7 43 5 32 37 24 3 9 17 39 49 24 20 50 20 15 18 12 27 3 43 46 36 43 31 28 32 50 50 44 43 19 13 20 6 15 26 14 45 25 11 ...

output:

9497

result:

ok 1 number(s): "9497"

Test #15:

score: 0
Accepted
time: 3ms
memory: 20516kb

input:

700 200
190 11 82 65 81 32 130 4 124 52 155 181 166 29 44 49 187 134 155 130 127 17 76 156 59 155 171 194 110 2 102 122 48 191 31 25 83 154 184 56 38 175 50 190 162 191 116 198 170 173 160 177 184 194 195 64 120 27 154 192 96 160 183 196 76 109 15 81 9 189 120 55 42 17 192 20 100 53 29 197 181 152 1...

output:

142372

result:

ok 1 number(s): "142372"

Test #16:

score: 0
Accepted
time: 3ms
memory: 20452kb

input:

800 200
197 112 65 12 115 42 97 158 105 122 140 175 154 63 192 103 43 87 11 114 164 35 179 101 171 13 104 179 185 78 96 75 93 19 191 108 136 161 152 183 123 199 99 197 147 185 82 112 6 157 178 180 200 47 95 15 153 71 89 172 182 98 187 19 129 126 59 166 2 75 135 86 64 37 58 64 148 195 45 165 125 115 ...

output:

193511

result:

ok 1 number(s): "193511"

Test #17:

score: -100
Wrong Answer
time: 4ms
memory: 20460kb

input:

900 200
27 187 75 160 123 52 39 137 85 65 149 67 65 122 140 57 101 39 143 200 100 153 57 47 7 172 62 140 34 153 91 4 61 148 51 165 64 92 119 10 183 97 48 2 58 53 48 1 43 117 71 84 115 176 96 192 109 14 124 51 168 137 191 168 182 143 4 50 71 162 75 16 86 158 50 84 120 60 137 158 69 53 32 24 59 94 178...

output:

217020518514587992

result:

wrong answer 1st numbers differ - expected: '387825', found: '217020518514587992'