QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#222578 | #7567. Joining Cats | Lain | TL | 0ms | 3884kb | C++20 | 4.0kb | 2023-10-21 17:39:52 | 2023-10-21 17:39:54 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
struct Tree {
typedef int T;
static constexpr T unit = INT_MAX;
T f (T a, T b) { return min(a, b); }
vector<T> s; int n;
Tree(int n = 0, T def = unit) : s(2*n, def), n(n) {}
void update(int pos, T val) {
for (s[pos += n] = val; pos /= 2;)
s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
}
T query(int b, int e) { // query [b, e)
T ra = unit, rb = unit;
for (b += n, e += n; b < e; b /= 2, e /= 2) {
if (b % 2) ra = f(ra, s[b++]);
if (e % 2) rb = f(s[--e], rb);
}
return f(ra, rb);
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
enum {
LEFT_CUT,
RIGHT_CUT,
};
struct node {
int l, r, st;
};
int n, k;
cin >> n >> k;
vi a(n), s(k);
for (auto& x : a) cin >> x;
for (auto& x : s) cin >> x;
reverse(all(s));
auto compress= s;
sort(all(compress));
compress.erase(unique(all(compress)), compress.end());
auto get = [&](int x)->int {
return lower_bound(all(compress), x) - compress.begin();
};
// For the value a[i], if I have used j times, what is the next usable?
vector<vi> next(n, vi(k+1, -1));
rep(i, 0, n) {
Tree T(sz(compress));
for (int j = k; j >= 0; j--) {
int _ = 1;
while(_--) {
int idx = get(a[i]);
if (idx == sz(compress)) continue;
int best = T.query(idx, sz(compress));
if (best == T.unit) continue;
next[i][j] = best;
}
if (j)
T.update(get(s[j-1]), j-1);
}
}
vector<ll> pref(n), suff(n);
for (int i = 0; i < n; i++) {
if (i) pref[i] += pref[i-1];
pref[i] += a[i];
}
for (int i = n-1; i >= 0; i--) {
if (i != n-1) suff[i] += suff[i+1];
suff[i] += a[i];
}
reverse(all(suff));
// TODO:
// 1. Insert the segment at 0 in the correct spot.
// 2. Iterate in increasing order, and do a BFS.
// 3. If you encounter a [x, x] segment, we win
bool found = false;
vector<vector<node>> events(k);
if (next[0][0] != -1)
events[next[0][0]].push_back({0, n-1, LEFT_CUT});
if (next[n-1][0] != -1)
events[next[n-1][0]].push_back({0, n-1, RIGHT_CUT});
set<pair<int,int>> used;
rep(i, 0, k) {
if (found) break;
if (events[i].empty()) continue;
for (auto& [l, r, st] : events[i]) {
if (st == LEFT_CUT) {
ll want = (l?pref[l-1]:0) + s[i];
auto it = upper_bound(pref.begin() + l, pref.begin() + r + 1, want);
if (it == pref.begin()) continue;
int nl = it - pref.begin(), nr = r;
if (nl >= nr) {
found = true;
break;
}
if (used.count({nl, nr})) continue;
used.insert({nl, nr});
if (next[nl][i+1] != -1) {
events[next[nl][i+1]].push_back({nl, nr, LEFT_CUT});
}
if (next[nr][i+1] != -1) {
events[next[nr][i+1]].push_back({nl, nr, RIGHT_CUT});
}
} else if (st == RIGHT_CUT) {
int suffl = (n-1) - r, suffr = (n-1) - l;;
ll want = (suffl?suff[suffl - 1]: 0) + s[i];
auto it = upper_bound(suff.begin() + suffl, suff.begin() + suffr + 1, want);
if (it == suff.begin()) continue;
int nsuffl = it - suff.begin(), nsuffr = suffr;
int nl = n-1-nsuffr, nr = n-1-nsuffl;
if (nl >= nr) {
found = true;
break;
}
if (used.count({nl, nr})) continue;
used.insert({nl, nr});
if (next[nl][i+1] != -1) {
events[next[nl][i+1]].push_back({nl, nr, LEFT_CUT});
}
if (next[nr][i+1] != -1) {
events[next[nr][i+1]].push_back({nl, nr, RIGHT_CUT});
}
} else assert(false);
}
}
cout << (found?"Yes":"No") << '\n';
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3544kb
input:
5 2 1 1 1 1 1 2 2
output:
Yes
result:
ok answer is YES
Test #2:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
6 7 3 2 1 1 2 3 2 2 2 2 2 2 2
output:
No
result:
ok answer is NO
Test #3:
score: 0
Accepted
time: 0ms
memory: 3884kb
input:
7 4 1 2 3 4 3 2 1 3 3 3 3
output:
Yes
result:
ok answer is YES
Test #4:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
5 1 5 4 3 2 1 10
output:
Yes
result:
ok answer is YES
Test #5:
score: -100
Time Limit Exceeded
input:
5000 5000 775487425 856128884 277783434 903422359 477267301 475103384 297595527 426167697 732858986 408894759 274205836 78265305 841664344 827278645 235744961 539622829 661053351 709331224 497285040 688977639 794889854 890450616 730989757 164925481 519732355 5132018 793806705 617096813 966338860 838...