QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#249074 | #7567. Joining Cats | USP_USP_USP# | ML | 1ms | 9572kb | C++20 | 2.8kb | 2023-11-12 00:25:05 | 2023-11-12 00:25:05 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define int int64_t
#define pb push_back
void dbg_out() { cerr << endl; }
template <typename H, typename... T>
void dbg_out(H h, T... t) { cerr << ' ' << h; dbg_out(t...); }
#define dbg(...)// { cerr << #__VA_ARGS__ << ':'; dbg_out(__VA_ARGS__); }
const int MAXN = 5e3+5;
int dp[MAXN][MAXN];
int maxin[MAXN][MAXN];
int matf[MAXN][MAXN];
const int INF = 1e9;
void solve() {
int n,k;
cin >> n >> k;
for(int i = 0; i< n; i++){
for(int j = 0; j < n; j++){
dp[i][j] = INF;
}
dp[i][i] = 0;
}
vector<int> pl(n);
vector<int> pr(n);
for(int i = 0; i < n; i++){
pl[i] = i;
pr[i] = i;
}
vector<int> a(n);
for(int i = 0; i < n; i++){
cin >> a[i];
}
vector<int> s(k);
for(int i = 0; i < k; i++){
cin >> s[i];
}
for(int i = 0; i < k; i++){
maxin[i][i] = s[i];
for(int j = i+1; j < k; j++){
maxin[i][j] = max(maxin[i][j-1],s[j]);
}
}
for(int i= 0 ; i < n; i++){
matf[i][k] = INF;
for(int j = k-1; j >= 0; j--){
matf[i][j] = matf[i][j+1];
if(a[i] <= s[j]) matf[i][j] = j;
}
}
auto pa = a;
for(int i = 1; i < n; i++){
pa[i] += pa[i-1];
}
auto sumin = [&](int i, int j){
return pa[j] - (i ? pa[i-1] : 0);
};
auto acha = [&](int i, int at){
//preciso do primeiro s[j] >= a[i] e j >= at
return matf[i][at-1]+1;
};
for(int len = 2; len <= n; len++){
for(int i = 0; i <= n-len; i++){
int j = i+len-1;
int at = max(dp[i][j-1],dp[i+1][j]);
if(at >= INF){
continue;
}
dbg(i,j);
//ou é at a resposta pra i,j, ou o proximo maior que tem wi >= min(a[i],a[j])
//testar se é at pela esqeurda
dbg(at);
while(pl[i]+1 < n && dp[i][pl[i]+1] <= at-1) pl[i]++;
while(pr[j]-1 >= 0 && dp[pr[j]-1][j] <= at-1) pr[j]--;
dbg(pl[i],pr[j]);
vector<array<int,4>> op;
op.pb({i,pr[j]-1,pr[j],j});
op.pb({pl[i]+1,j,i,pl[i]});
bool ok = false;
for(auto [x,y,z,w] : op){
dbg(x,y);
int soma = sumin(x,y);
int vdp = dp[z][w];
if(vdp >= at) continue;
dbg(soma,vdp,at);
ok |= maxin[vdp][at-1] >= soma;
}
dbg(ok);
if(ok){
dp[i][j] = at;
continue;
}
if(a[i] <= a[j]){
dp[i][j] = acha(i,at+1);
}else{
dp[i][j] = acha(j,at+1);
}
dp[i][j] = min(dp[i][j],INF);
}
}
for(int i = 0 ; i < n; i++){
for(int j = i; j < n; j++){
//cout << dp[i][j] << ' ';
}
//cout << '\n';
}
int ans = dp[0][n-1] <= k;
cout << (ans ? "Yes" : "No") << '\n';
}
signed main(){
ios::sync_with_stdio(false); cin.tie(0);
solve();
}
/*
Makefile:
CXXFLAGS=-Wall -Wextra -Wshadow -g -pedantic -fsanitize=address,undefined -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUGPEDANTIC -std=gnu++17
*/
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 7520kb
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: 9572kb
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: 1ms
memory: 7612kb
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: 1ms
memory: 7540kb
input:
5 1 5 4 3 2 1 10
output:
Yes
result:
ok answer is YES
Test #5:
score: -100
Memory 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:
No