QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#212478#7567. Joining CatskiwiHM#TL 4ms56744kbC++202.1kb2023-10-13 16:19:452023-10-13 16:19:47

Judging History

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

  • [2023-10-13 16:19:47]
  • 评测
  • 测评结果:TL
  • 用时:4ms
  • 内存:56744kb
  • [2023-10-13 16:19:45]
  • 提交

answer

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

typedef long long LL;

const int Maxn = 5050;

short f[Maxn][Maxn], maxr[Maxn][Maxn], minl[Maxn][Maxn];

LL sum[Maxn];
int a[Maxn], w[Maxn], mx[Maxn];
int n, m;

LL qsum(int l, int r){
    if (l > r) 
        return 0;
    return sum[r] - sum[l - 1];
}

short func(int l, int r);

bool checkleft(int l, int r, int i){
    if (mx[i] >= qsum(l, r))
        return true;
    short pos = maxr[i][l];
    if (pos < l)
        return false;
    if (func(pos + 1, r) <= i - 1)
        return true;
    if (func(pos + 2, r) <= i - 1 && mx[i - 1] >= qsum(pos + 2, r))
        return true;
    return false;
}

bool checkright(int l, int r, int i){
    if (mx[i] >= qsum(l, r))
        return true;
    short pos = minl[i][r];
    if (pos > r)
        return false;
    if (func(l, pos - 1) <= i - 1)
        return true;
    if (func(l, pos - 2) <= i - 1 && mx[i - 1] >= qsum(l, pos - 2))
        return true;
    return false;
}

short func(int l, int r){
    if (l >= r)
        return 0;
    if (f[l][r] >= 0)
        return f[l][r];
    short sk = max(func(l + 1, r), func(l, r - 1));
    for (int i = sk; i <= m; i++)
        if (checkleft(l, r, i) || checkright(l, r, i))
            return i;
    return f[l][r] = m + 1;
}

int main(){
    ios :: sync_with_stdio(false), cin.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> a[i], sum[i] = sum[i - 1] + a[i];
    for (int i = 1; i <= m; i++)
        cin >> w[i], mx[i] = max(mx[i - 1], w[i]);
    w[++m] = sum[n], mx[m] = max(mx[m - 1], w[m]);
    for (int i = 1; i <= m; i++){
        for (int l = 1, r = 1; l <= n; l++){
            while (r <= n && qsum(l, r) <= w[i])
                r++;
            maxr[i][l] = r - 1;
        }
    }
    for (int i = 1; i <= m; i++){
        for (int r = n, l = n; r >= 1; r--){
            while (l >= 1 && qsum(l, r) <= w[i])
                l--;
            minl[i][r] = l + 1;
        }
    }
    memset(f, -1, sizeof f);
    cout << (func(1, n) <= m - 1 ? "Yes" : "No") << endl;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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: 3ms
memory: 56240kb

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: 4ms
memory: 55724kb

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: