QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#744658 | #6539. Treasure Box | highkj | WA | 9ms | 7952kb | C++14 | 3.0kb | 2024-11-13 22:44:06 | 2024-11-13 22:44:12 |
Judging History
answer
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define rep(i,x,y) for(register int i=x;i<=y;i++)
#define rep1(i,x,y) for(register int i=x;i>=y;--i)
#define int long long
#define fire signed
#define il inline
template<class T> il void print(T x) {
if(x<0) printf("-"),x=-x;
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
}
template<class T> il void in(T &x) {
x = 0; char ch = getchar();
int f = 1;
while (ch < '0' || ch > '9') {if(ch=='-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); }
x *= f;
}
int T=1;
const int N=5e6+10;
int n,c,cnt;
int h[N],f[N][4],sum[N];
string a;
il void solve1() {
rep(i,1,n) {
sum[i]=sum[i-1];
if(a[i]!=a[n-i+1]) {
sum[i]++;
}
}
int lst=false,ss=0,pr=0;
rep1(i,n,(n+1)/2+1) {
if(a[i]!=a[n-i+1]) {
if(!lst) pr=i;
lst=i;
ss+=h[i];
}
}
f[lst][1]=ss+(pr-lst)*c;
int now=0;
rep1(i,lst-1,1) {
if(sum[i-1]==0) {
now=i;
break;
}
if(a[i]==a[n-i+1]) f[i][1]=f[i+1][1]+c;
else {
if(i<=(n+1)/2&&h[i]<h[n-i+1]) f[i][1]=min(f[i][1],f[i+1][1]-max(h[i],h[n-i+1])+min(h[i],h[n-i+1])+c);
else f[i][1]=min(f[i][1],f[i+1][1]+c);
}
}
int j=now,s=0,cc=0;
while(j<=n) {
int dui=n-j+1;
if(a[j]!=a[dui]) {
if(dui>=now&&dui<=j) {
if(h[j]<h[dui]) {
s-=h[dui];
s+=h[j];
}
}else s+=h[j],cc++;
}
if(cc==cnt) f[now][1]=min(f[now][1],s+(j-now)*c);
j++;
}
rep1(i,now-1,1) f[i][1]=min(f[i][1],f[i+1][1]+c);
}
il void solve2() {
int lst=false;
int ss=false,pr=0;
rep(i,1,(n+1)/2) {
if(a[i]!=a[n-i+1]) {
if(!lst) pr=i;
lst=i;
ss+=h[i];
}
}
f[lst][0]=ss+(lst-pr)*c;
int now=0;
rep(i,lst+1,n) {
if(sum[n]-sum[i]==0) {
now=i;
break;
}
if(a[i]==a[n-i+1]){
f[i][0]=f[i-1][0]+c;
} else {
if(h[i]<h[n-i+1]) f[i][0]=min(f[i][0],f[i-1][0]-h[n-i+1]+h[i]+c);
else f[i][0]=min(f[i][0],f[i-1][0]+c);
}
}
int j=now,s=0,cc=0;
while(j>=1) {
int dui=n-j+1;
if(a[j]!=a[dui]) {
if(dui>=j&&dui<=now) {
if(h[j]<h[dui]) {
s-=h[dui];
s+=h[j];
}
}else s+=h[j],cc++;
}
if(cc==cnt) f[now][0]=min(f[now][0],s+(now-j)*c);
j--;
}
rep(i,now+1,n) f[i][0]=min(f[i][0],f[i-1][0]+c);
}
il void solve() {
in(n),in(c);
cin>>a;
a=" "+a;
rep(i,1,n) in(h[i]);
// if(n==1) {
// cout<<0<<endl;
// return ;
// }
cnt=false;
rep(i,1,n) if(a[i]!=a[n-i+1]) cnt++;
rep(i,0,n+1) rep(j,0,3) f[i][j]=2e18;
cnt/=2;
// if(cnt==0) {
// cout<<0<<endl;
// return ;
// }
solve1();
solve2();
rep(i,1,n) f[i][3]=f[i][0],f[i][2]=f[i][1];
rep1(i,n,1) f[i][3]=min(f[i][3],f[i+1][3]+c);
rep(i,1,n) f[i][2]=min(f[i][2],f[i-1][2]+c),printf("%lld ",min(f[i][2],f[i][3]));
puts("");
}
fire main() {
// freopen(".in","r",stdin);
// freopen("s.out","w",stdout);
in(T);
while(T--) {
solve();
}
return false;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7952kb
input:
2 5 1 ABCDE 7 1 4 5 1 5 1 ABCDA 7 1 4 5 1
output:
6 5 6 6 5 2 1 2 3 4
result:
ok 10 numbers
Test #2:
score: 0
Accepted
time: 0ms
memory: 7824kb
input:
1 10 4 AABABBABAB 10 3 4 10 7 1 8 10 7 6
output:
10 14 18 22 26 22 18 14 10 6
result:
ok 10 numbers
Test #3:
score: -100
Wrong Answer
time: 9ms
memory: 7888kb
input:
10000 10 4 ABBBAABABB 6 10 3 9 1 8 5 4 4 10 10 4 BABBAAABAB 3 7 5 9 2 3 9 8 4 1 10 4 ABBBAAABAA 3 2 4 8 2 9 1 9 6 3 10 4 BBAABBAABB 8 8 10 7 1 1 4 9 6 4 10 4 BAABBBBBAB 5 7 3 5 4 2 1 8 2 8 10 4 ABAAAABABA 10 8 2 7 5 3 7 8 10 5 10 4 BBABABAABA 10 6 10 2 2 6 4 4 3 9 10 4 BBBAAAABBB 7 2 2 8 9 10 10 6 7...
output:
17 21 17 21 25 29 26 22 26 22 21 17 13 9 13 13 9 13 17 21 22 18 22 18 22 19 15 19 15 19 0 4 8 12 16 20 24 28 32 36 11 7 3 7 11 15 12 8 12 16 19 15 11 7 11 11 7 11 15 19 30 34 38 34 30 34 38 42 39 35 0 4 8 12 16 20 24 28 32 36 11 7 3 7 11 13 9 5 9 13 8 4 8 12 16 14 10 6 2 6 18 14 10 6 10 14...
result:
wrong answer 32nd numbers differ - expected: '0', found: '4'