QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#207999#7567. Joining Catscardinal_city#TL 0ms3784kbC++232.2kb2023-10-09 03:06:012023-10-09 03:06:02

Judging History

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

  • [2023-10-09 03:06:02]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3784kb
  • [2023-10-09 03:06:01]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;
#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()
#define smx(a, b) a = max(a, b);
#define smn(a, b) a = min(a, b);
#define pb push_back
#define endl '\n'

const ll MOD = 1e9 + 7;
const ld EPS = 1e-9;

mt19937 rng(time(0));

int main() {
	cin.tie(0)->sync_with_stdio(0);

	int n, k; cin >> n >> k;
	vector<ll> w(n), s(k);

	rep(i,0,n) cin >> w[i];
	rep(i,0,k) cin >> s[i];

	vector<ll> pfx(n+1);
	rep(i,0,n) pfx[i+1] = pfx[i] + w[i];

	auto bs_left = [&](int j, int l) { // find min i s.t. sum w_i + ... + w_j <= s_k
		ll tot = pfx[j + 1] - s[l];
		if (tot <= 0) { // s[k] >= sum w_0 through w_j
			return 0;
		}
		int pos = lower_bound(pfx.begin(), pfx.end(), tot) - pfx.begin();
		return pos;
	};

	auto bs_right = [&](int j, int l) { // find max i s.t. sum w_j + ... + w_i <= s_k
		// cout << j << ", " << l << ", " << pfx[j] + s[l] << "\n";
		int pos = upper_bound(pfx.begin(), pfx.end(), pfx[j] + s[l]) - pfx.begin();
		return pos - 1;
	};

	vector save_bs_left(n, vector<int>(k));
	vector save_bs_right(n, vector<int>(k));

	for (int i = 0; i < n; i++) {
		for (int l = 0; l < k; l++) {
			save_bs_left[i][l] = bs_left(i, l);
			save_bs_right[i][l] = bs_right(i, l);
		}
	}

	vector save_left(n, vector<int>(k + 1, n));
	vector save_right(n, vector<int>(k + 1, 0));
	for (int i = 0; i < n; i++) save_left[i][0] = i, save_right[i][0] = i;

	function<bool(int, int, int)> dfs = [&](int i, int j, int l) {
		if (save_left[j][l] <= i || save_right[i][l] >= j) return true;
		if (l == 0) return false;
		if (l < 0) return false;
		// cout << "solving case " << i << ", " << j << ", " << l << "\n";
		int p1 = save_bs_left[j][l - 1];
		// cout << "bs_left gives " << p1 - 1 << "\n";
		int p2 = save_bs_right[i][l - 1];
		// cout << "bs_right gives " << p2 << "\n";
		if (p1 - 1 <= i || p2 >= j || dfs(i, p1 - 1, l - 1) || dfs(p2, j, l - 1)) {
			save_left[j][l] = i;
			save_right[i][l] = j;
			return true;
		}
		return false;
	};

	cout << (dfs(0, n - 1, k) ? "Yes\n" : "No\n");
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3708kb

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: 3784kb

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: 3780kb

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: 3708kb

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...

output:


result: