QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#431450 | #8772. House Deconstruction | A_zjzj | WA | 68ms | 17668kb | C++14 | 1.7kb | 2024-06-05 16:16:20 | 2024-06-05 16:16:20 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include"debug.h"
#else
#define debug(...) void()
#endif
#define all(x) (x).begin(),(x).end()
template<class T>
auto ary(T *a,int l,int r){
return vector<T>{a+l,a+1+r};
}
using ll=long long;
using ull=unsigned ll;
const int N=2e5+10,M=N*2,mod=998244353;
const ll INF=1e18;
int k,n,m,a[M],cnt[M],pre[M];
ll sum[M],f[M];
int g[M];
void chkmin(ll &x,ll y,int &tx,int ty){
if(x>y)x=y,tx=ty;
else if(x==y)(tx+=ty)%=mod;
}
pair<ll,int> solve(int d){
memset(f,0x3f,sizeof f);
memset(g,0,sizeof g);
int st=0,ed=n;
for(;st<=n&&cnt[st]!=d;st++);
for(;ed>=0&&cnt[ed]!=cnt[n]+d;ed--);
if(st>ed)return {INF,0};
f[st]=0,g[st]=1;
for(int i=st+1;i<=ed;i++){
if(a[i]>0)chkmin(f[i],f[i-1],g[i],g[i-1]);
if(pre[i]>=st)chkmin(f[i],f[pre[i]]+abs(sum[i]-sum[pre[i]]),g[i],g[pre[i]]);
}
return {f[ed]+abs(sum[st]+sum[n]-sum[ed]+1ll*k*d),g[ed]};
}
int main(){
scanf("%d%d%d",&k,&n,&m),n+=m;
for(int i=1,x;i<=n;i++){
static char str[9];
scanf("%d%s",&x,str);
a[i]=*str=='H'?1:-1;
cnt[i]=cnt[i-1]+a[i];
sum[i]=sum[i-1]+a[i]*x;
}
static int cur[M];
memset(cur,-1,sizeof cur);
for(int i=0;i<=n;i++){
pre[i]=cur[cnt[i]+m],cur[cnt[i]+m]=i;
}
int l=n,r=-n,mid;
for(int i=0;i<=n;i++){
l=min(l,cnt[i]),r=max(r,cnt[i]);
}
r-=cnt[n];
for(;l+1<r;){
mid=(l+r)>>1;
if(solve(mid)<solve(mid+1))r=mid;
else l=mid+1;
}
ll ans=min(solve(l),solve(r)).first;
int res=0;
for(int i=r;;i++){
auto [x,y]=solve(i);
if(x==ans)(res+=y)%=mod;
else break;
}
for(int i=l;;i--){
auto [x,y]=solve(i);
if(x==ans)(res+=y)%=mod;
else break;
}
cout<<ans<<endl<<res<<endl;
return 0;
}
#ifdef DEBUG
#include"debug.hpp"
#endif
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 13940kb
input:
6 2 4 1 P 2 H 3 P 4 H 5 H 6 H
output:
2 3
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 4ms
memory: 13280kb
input:
1000000000 2 4 1 P 6 H 31415926 H 999999998 H 999999999 H 1000000000 P
output:
4 1
result:
ok 2 lines
Test #3:
score: 0
Accepted
time: 68ms
memory: 17668kb
input:
1000000000 199999 200000 3087 P 3282 H 6407 H 6748 P 9535 P 10098 H 16470 H 22311 H 31045 H 31968 H 32549 H 33037 P 36005 P 36308 H 40284 P 46224 H 52394 P 54922 H 55800 P 56582 H 57548 H 62155 P 63067 P 64237 H 66133 P 71351 P 75667 P 79089 P 79646 P 81144 H 91883 P 96680 H 102797 H 105388 H 105971...
output:
262753966206 1
result:
ok 2 lines
Test #4:
score: 0
Accepted
time: 55ms
memory: 17292kb
input:
1000000000 150000 200000 3087 P 3282 H 6407 H 6748 H 9535 H 10098 H 16470 H 22311 H 31968 H 32549 H 33037 P 36005 P 40284 H 52394 P 54922 H 55800 H 56582 H 57548 H 62155 P 63067 P 64237 H 66133 P 71351 P 75667 H 79089 P 79646 P 81144 H 91883 P 96680 H 102797 H 105388 H 105971 P 106057 P 107807 P 116...
output:
1212175277 8
result:
ok 2 lines
Test #5:
score: -100
Wrong Answer
time: 34ms
memory: 17320kb
input:
1000000000 100000 200000 3068 H 3087 P 3106 H 33031 H 33037 P 33043 H 52370 H 52394 P 52418 H 62996 H 63067 P 63138 H 66040 H 66133 P 66226 H 71350 H 71351 P 71352 H 91852 H 91883 P 91914 H 105924 H 105969 H 105971 P 106018 H 106057 P 106145 H 116361 H 116385 P 116409 H 126036 H 126084 P 126132 H 12...
output:
4978306 927394736
result:
wrong answer 2nd lines differ - expected: '463697368', found: '927394736'