QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#207994#7567. Joining Catscardinal_city#TL 0ms3632kbC++231.8kb2023-10-09 02:38:342023-10-09 02:38:34

Judging History

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

  • [2023-10-09 02:38:34]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3632kb
  • [2023-10-09 02:38:34]
  • 提交

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(n, vector<int>(n, k + 1));
	for (int i = 0; i < n; i++) save[i][i] = 0;

	function<bool(int, int, int)> dfs = [&](int i, int j, int l) {
		if (save[i][j] <= l) return true;
		if (l == 0) return false;
		if (l < 0) return false;
		// cout << "solving case " << i << ", " << j << ", " << l << "\n";
		int p1 = bs_left(j, l - 1);
		// cout << "bs_left gives " << p1 - 1 << "\n";
		int p2 = 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[i][j] = min(save[i][j], l);
			return true;
		}
		return false;
	};

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

詳細信息

Test #1:

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

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

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

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

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: