QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#498253 | #8776. Not Another Constructive! | qL | WA | 1ms | 6384kb | C++14 | 3.8kb | 2024-07-30 10:04:41 | 2024-07-30 10:04:42 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int mian(); int main() { return mian(); }
const int N = 41, K = 2525;
int n, k;
char ch[N];
bitset<K> dp[N][N][N];
char ci[] = {'B','D','E','F','G','H','I','J','K','L','M','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
void out(const int i, const int cn, const int cc, const int kkl) {
// cout<<ch[i + 1]<<" "<<i<<" "<<cn<<" "<<cc<<" "<<kkl<<" "<<dp[i][cn][cc].test(kkl)<<endl;
if( i == 0 ) { for(int j = 1; j <= n; ++j) putchar(ch[j]); return ; }
switch( ch[i] ) {
case 'N' : { out( i - 1, cn - 1, cc, kkl ); break; }
case 'C' : { out( i - 1, cn, cc + 1, kkl ); break; }
case 'A' : { out( i - 1, cn, cc, kkl - cn * cc ); break; }
case '?' : {
if( dp[i - 1][cn - 1][cc].test(kkl) ) ch[i] = 'N', out( i - 1, cn - 1, cc, kkl );
else if( dp[i - 1][cn][cc + 1].test(kkl) ) ch[i] = 'C', out( i - 1, cn, cc + 1, kkl );
else if( kkl >= cn * cc and dp[i - 1][cn][cc].test( kkl - cn * cc ) ) ch[i] = 'A', out( i - 1, cn, cc, kkl - cn * cc );
else ch[i] = ci[ rand() % 23 ], out( i - 1, cn, cc, kkl );
break;
}
default : { out( i - 1, cn, cc, kkl ); }
}
}
int mian() {
srand(time(0));
scanf("%d%d%s", &n, &k, ch + 1);
for(int i = 0; i <= n; ++i) dp[0][0][i].set(0);//如果太少了也是一样的不会统计到答案中
for(int i = 0; i < n; ++i) {
for(int cn = 0; cn <= i; ++cn){
for(int cc = 0; cc + cn <= n; ++cc) {
// if(i == 10 and cn == 1 and cc == 3 and dp[i][cn][cc].count()) cout<<cn<<" "<<cc<<" "<<dp[i][cn][cc]<<endl;
// if(i == 11 and cn == 1 and cc == 2 and dp[i][cn][cc].count()) cout<<cn<<" "<<cc<<" "<<dp[i][cn][cc]<<endl;
switch( ch[i + 1] ) {
case 'N' : { dp[i + 1][cn + 1][cc] |= dp[i][cn][cc]; break; }
case 'C' : { dp[i + 1][cn][cc - 1] |= dp[i][cn][cc]; break; }
case 'A' : { dp[i + 1][cn][cc] |= dp[i][cn][cc] << ( cn * cc ); break; }
case '?' : {
dp[i + 1][cn + 1][cc] |= dp[i][cn][cc];
dp[i + 1][cn][cc - 1] |= dp[i][cn][cc];
dp[i + 1][cn][cc] |= dp[i][cn][cc] << ( cn * cc );
dp[i + 1][cn][cc] |= dp[i][cn][cc];
break;
}
default : { dp[i + 1][cn][cc] |= dp[i][cn][cc]; }
}
}
}
}
for(int cntn = 0; cntn <= n; ++cntn) {
// cout<<dp[n][cntn][0]<<endl;
if( dp[n][cntn][0].test(k) ) return out(n, cntn, 0, k), 0;
}
puts("-1");
return 0;
}/*
给出一个大小为 $n$ 的串,要求将其中的 $?$ 替换成任意的大写字母,使得最终的串中 $NAC$ 子序列的个数为 $k$
构造方案
$n\le40, k\le 2500$
$1s, 2G$
朴素的 $dp$ 可以设计为 $N,NA,NAC$ 的子序列各有多少个,但这样是 $O(n^3k)$ 的
考虑状态上的优化,考虑 $NAC$ 三个位置中比较特殊的位置,比如说中间的 $A$
那么对于一个 $A$ ,他的贡献就是前面的 $n$ 的数量 $\times$ 后面 $c$ 的数量
于是设计为 $f(i,n,c,k)$ 表示当前位置上,前面有了 $n$ 个 $N$,后面有了 $c$ 个 $C$,能否得到最终 $k$ 的贡献
如果这一位是 $N$,那么 $f(i,n,c,k) \to f(i+1,n+1,c,k)$
如果这一位是 $C$,那么 $f(i,n,c,k) \to f(i+1,n,c-1,k)$
如果这一位是 $A$,那么 $f(i,n,c,k) \to f(i+1,n,c,k+n*c)$
如果这一位是 $B$,那么 $f(i,n,c,k) \to f(i+1,n,c,k)$
如果这一位是 $?$,那么全转移一遍
最终答案就是 $f(n,cnt_N,0,k)$,初始化为 $f(0,0,cnt_c,0)=1$
构造方案就直接向回找哪一位是 $1$ 即可 (直接记录pre或许会挂,因为空间太大)
因为每一个 $dp$ 表示的是 $01$,且最后一维 $k$ 只在不断增加
那么就可以直接将最后一维 $k$ 压在一个 bitset 中
然后对于 $cnt_c$ 和 $cnt_n$,需要把 $?$ 的情况算上。最后如果存在一个方案一开始规定了 $x$ 个 $c$ 却没有用完,那么一定不在答案之中
22 2
N??A??????C???????????
NBBABBBBBBCBBBAAAAAAAC
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 6384kb
input:
22 2 N??A??????C???????????
output:
NCCAPPVYSPCPLIDXQQRKZC
result:
ok correct
Test #2:
score: 0
Accepted
time: 1ms
memory: 5304kb
input:
18 0 COUNTINGSATELLITES
output:
COUNTINGSATELLITES
result:
ok correct
Test #3:
score: 0
Accepted
time: 0ms
memory: 3692kb
input:
2 1 ??
output:
-1
result:
ok correct
Test #4:
score: 0
Accepted
time: 0ms
memory: 3896kb
input:
1 0 ?
output:
C
result:
ok correct
Test #5:
score: 0
Accepted
time: 0ms
memory: 3776kb
input:
1 0 N
output:
N
result:
ok correct
Test #6:
score: 0
Accepted
time: 0ms
memory: 3888kb
input:
1 0 X
output:
X
result:
ok correct
Test #7:
score: 0
Accepted
time: 0ms
memory: 3896kb
input:
1 1 ?
output:
-1
result:
ok correct
Test #8:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
1 1 N
output:
-1
result:
ok correct
Test #9:
score: 0
Accepted
time: 0ms
memory: 3752kb
input:
1 1 X
output:
-1
result:
ok correct
Test #10:
score: 0
Accepted
time: 0ms
memory: 3904kb
input:
2 0 ??
output:
CC
result:
ok correct
Test #11:
score: 0
Accepted
time: 0ms
memory: 3904kb
input:
2 0 N?
output:
NC
result:
ok correct
Test #12:
score: 0
Accepted
time: 0ms
memory: 3824kb
input:
2 0 ?C
output:
CC
result:
ok correct
Test #13:
score: 0
Accepted
time: 0ms
memory: 3828kb
input:
2 1 N?
output:
-1
result:
ok correct
Test #14:
score: 0
Accepted
time: 0ms
memory: 3840kb
input:
2 1 ?C
output:
-1
result:
ok correct
Test #15:
score: 0
Accepted
time: 0ms
memory: 3928kb
input:
3 1 ???
output:
NAC
result:
ok correct
Test #16:
score: 0
Accepted
time: 0ms
memory: 3708kb
input:
3 1 N??
output:
NAC
result:
ok correct
Test #17:
score: 0
Accepted
time: 0ms
memory: 3852kb
input:
3 1 ?A?
output:
NAC
result:
ok correct
Test #18:
score: 0
Accepted
time: 0ms
memory: 3792kb
input:
3 1 ??C
output:
NAC
result:
ok correct
Test #19:
score: 0
Accepted
time: 0ms
memory: 3772kb
input:
3 1 NA?
output:
NAC
result:
ok correct
Test #20:
score: 0
Accepted
time: 0ms
memory: 3788kb
input:
3 1 N?C
output:
NAC
result:
ok correct
Test #21:
score: 0
Accepted
time: 0ms
memory: 3900kb
input:
3 1 ?AC
output:
NAC
result:
ok correct
Test #22:
score: 0
Accepted
time: 1ms
memory: 3704kb
input:
4 1 ????
output:
CNAC
result:
ok correct
Test #23:
score: 0
Accepted
time: 0ms
memory: 3960kb
input:
4 1 X???
output:
XNAC
result:
ok correct
Test #24:
score: 0
Accepted
time: 0ms
memory: 3888kb
input:
4 1 ???Z
output:
NACZ
result:
ok correct
Test #25:
score: 0
Accepted
time: 0ms
memory: 3856kb
input:
4 1 ?AA?
output:
-1
result:
ok correct
Test #26:
score: 0
Accepted
time: 0ms
memory: 3748kb
input:
4 1 N???
output:
NCAC
result:
ok correct
Test #27:
score: 0
Accepted
time: 0ms
memory: 3816kb
input:
4 1 ?N??
output:
CNAC
result:
ok correct
Test #28:
score: 0
Accepted
time: 0ms
memory: 3876kb
input:
4 1 ??N?
output:
NANC
result:
ok correct
Test #29:
score: 0
Accepted
time: 0ms
memory: 3748kb
input:
4 1 ???N
output:
NACN
result:
ok correct
Test #30:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
4 1 A???
output:
ANAC
result:
ok correct
Test #31:
score: 0
Accepted
time: 0ms
memory: 3828kb
input:
4 1 ?A??
output:
NAZC
result:
ok correct
Test #32:
score: 0
Accepted
time: 0ms
memory: 3952kb
input:
4 1 ??A?
output:
CNAC
result:
ok correct
Test #33:
score: 0
Accepted
time: 0ms
memory: 3928kb
input:
4 1 ???A
output:
NACA
result:
ok correct
Test #34:
score: 0
Accepted
time: 0ms
memory: 3952kb
input:
4 1 C???
output:
CNAC
result:
ok correct
Test #35:
score: 0
Accepted
time: 0ms
memory: 3944kb
input:
4 1 ?C??
output:
NCAC
result:
ok correct
Test #36:
score: 0
Accepted
time: 0ms
memory: 3952kb
input:
4 1 ??C?
output:
NACA
result:
ok correct
Test #37:
score: 0
Accepted
time: 0ms
memory: 3808kb
input:
4 1 ???C
output:
CNAC
result:
ok correct
Test #38:
score: 0
Accepted
time: 0ms
memory: 3792kb
input:
5 4 ?????
output:
NAACC
result:
ok correct
Test #39:
score: 0
Accepted
time: 1ms
memory: 3840kb
input:
6 14 ??????
output:
-1
result:
ok correct
Test #40:
score: 0
Accepted
time: 0ms
memory: 4096kb
input:
7 14 ???????
output:
-1
result:
ok correct
Test #41:
score: 0
Accepted
time: 0ms
memory: 4088kb
input:
8 43 ????????
output:
-1
result:
ok correct
Test #42:
score: 0
Accepted
time: 0ms
memory: 4100kb
input:
9 55 ?????????
output:
-1
result:
ok correct
Test #43:
score: 0
Accepted
time: 1ms
memory: 4316kb
input:
10 112 ??????????
output:
-1
result:
ok correct
Test #44:
score: 0
Accepted
time: 0ms
memory: 4436kb
input:
11 110 ???????????
output:
-1
result:
ok correct
Test #45:
score: 0
Accepted
time: 1ms
memory: 4420kb
input:
12 4 ????????????
output:
CCCCCCNACCCC
result:
ok correct
Test #46:
score: 0
Accepted
time: 1ms
memory: 4680kb
input:
13 193 ?????????????
output:
-1
result:
ok correct
Test #47:
score: 0
Accepted
time: 1ms
memory: 4600kb
input:
14 91 ??????????????
output:
NNNANAAACACCCC
result:
ok correct
Test #48:
score: -100
Wrong Answer
time: 1ms
memory: 4904kb
input:
15 15 ???????????????
output:
NCCCNACACCCCCCC
result:
wrong answer incorrect number of subsequences: 16