QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#742113 | #6539. Treasure Box | yanshanjiahong | TL | 0ms | 0kb | C++23 | 2.1kb | 2024-11-13 15:54:22 | 2024-11-13 15:54:22 |
answer
#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define repp(i,j,k) for(int i=j;i>=k;i--)
#define ls(x) (x<<1)
#define rs(x) ((x<<1)|1)
#define mp make_pair
#define sec second
#define fir first
#define pii pair<int,int>
#define lowbit(i) i&-i
#define double long double
#define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=5e6+5,M=35,S=(1<<8)+5,inf=(ll)1e18+7,mo=998244353;
const double eps=1e-8;
void read(int &p){
int x=0,w=1;
char ch=0;
while(!isdigit(ch)){
if(ch=='-')w=-1;
ch=getchar();
}
while(isdigit(ch)){
x=(x<<1)+(x<<3)+ch-'0';
ch=getchar();
}
p=x*w;
}
int T;
int n,c;
int a[N],h[N],p[N],cntd;
int f[N],sumh[N],mnsh[N],minf,prem[N],sufm[N];
int ans[N];
void calc(){
cntd=0,minf=inf;
rep(i,1,n)
if(a[i]!=a[n-i+1])p[++cntd]=i;
if(!cntd){
rep(i,1,n)
ans[i]=0;
return;
}
cntd>>=1;
rep(i,1,cntd)
sumh[i]=sumh[i-1]+h[p[i]];
mnsh[cntd+1]=0;
repp(i,cntd,1)
mnsh[i]=mnsh[i+1]+min(h[p[i]],h[n-p[i]+1]);
rep(i,cntd,cntd*2)
f[i]=mnsh[cntd*2-i+1]+sumh[cntd*2-i]+c*(p[i]-p[1]),minf=min(minf,f[i]);
prem[cntd-1]=inf,sufm[cntd*2+1]=inf;
rep(i,cntd,cntd*2)
prem[i]=min(prem[i-1],f[i]-p[i]*c);
repp(i,cntd*2,cntd)
sufm[i]=min(sufm[i+1],f[i]+p[i]*c);
int targ=cntd;
rep(i,1,n){
while(targ<=cntd*2&&p[targ]<=i)
targ++;
ans[i]=min({ans[i],minf+c*abs(i-p[1]),c*i+prem[targ-1],sufm[targ]-c*i});
}
}
inline void write(int x){
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar(x%10+'0');
return;
}
void solve(){
read(n),read(c);
rep(i,1,n)
read(a[i]);
rep(i,1,n)
read(h[i]),ans[i]=inf;
calc();
reverse(a+1,a+n+1),reverse(h+1,h+n+1),reverse(ans+1,ans+n+1);
calc();
reverse(ans+1,ans+n+1);
rep(i,1,n)
write(ans[i]),putchar(' ');
puts("");
}
signed main(){
read(T);
while(T--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
2 5 1 ABCDE 7 1 4 5 1 5 1 ABCDA 7 1 4 5 1