QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#866815 | #9222. Price Combo | Hanghang | TL | 1ms | 45588kb | C++20 | 2.8kb | 2025-01-22 19:36:14 | 2025-01-22 19:36:15 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=4e5+3;
ll n,tot;
array<array<ll,2>,2>f[N];
array<ll,3>a[N],num[N];
map<array<ll,3>,ll>mp;
void Min(ll &x,ll y){if(x>y)x=y;}
struct Info
{
array<ll,4>p,v;
Info(){for(int o:{0,1,2,3})p[o]=v[o]=0;}
friend Info operator *(Info A,Info B)
{
Info C;
for(int o:{0,1,2,3})C.p[o]=B.p[A.p[o]],C.v[o]=B.v[A.p[o]]+A.v[o];
return C;
}
}I,c[N];
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n;I.p={0,1,2,3};I.v={0,0,0,0};
for(int i=1;i<=n;i++)cin>>a[i][0];
for(int i=1;i<=n;i++)cin>>a[i][1],a[i][2]=a[i][0]>a[i][1];
for(int i=1;i<=n;i++)
{
ll x=a[i][0],y=a[i][1],o=a[i][2];
mp[{x,o,i}]=mp[{y,o^1,i}]=1;
}
for(auto &p:mp)p.second=++tot,num[tot]=p.first;
memset(f,0x3f,sizeof(f));f[tot+1][0][0]=0;
for(int t=1;t<=tot;t++)
{
if(num[t][1]){c[t]=I;continue;}
int id=num[t][2];
if(!a[id][2])c[t].p={2,3,0,1},c[t].v={a[id][0],a[id][0],0,0};
else c[t].p={1,0,3,2},c[t].v={a[id][1],0,a[id][1],0};
}
//cout<<"I: "<<I.p[0]<<" "<<I.p[1]<<" "<<I.p[2]<<" "<<I.p[3]<<endl;
//cout<<"C: "<<c[2].p[0]<<" "<<c[2].p[1]<<" "<<c[2].p[2]<<" "<<c[2].p[3]<<endl;
for(int t=tot;t>=1;t--)
{
int id=num[t][2];
if(num[t][1]){f[t]=f[t+1];continue;}
if(!a[id][2])for(int x:{0,1})for(int y:{0,1})f[t][x][y]=f[t+1][x^1][y]+(x==1)*a[id][0];
else for(int x:{0,1})for(int y:{0,1})f[t][x][y]=f[t+1][x][y^1]+(y==1)*a[id][1];
int j=mp[{a[id][a[id][2]^1],1,id}];
auto F=f[j+1];auto G=F;
if(!a[id][2])for(int x:{0,1})for(int y:{0,1})F[x][y]=G[x][y^1]+(y==1)*a[id][1];
else for(int x:{0,1})for(int y:{0,1})F[x][y]=G[x^1][y]+(x==1)*a[id][0];
Info bas=I;bas.v={F[0][0],F[0][1],F[1][0],F[1][1]};
//cout<<"Start: "<<id<<endl;
//cout<<F[0][0]<<" "<<F[0][1]<<" "<<F[1][0]<<" "<<F[1][1]<<endl;
for(int k=j;k>t;k--)
{
bas=bas*c[k];
// cerr<<k<<endl;
// for(int o=0;o<4;o++)cout<<bas.p[o]<<" ";
// cout<<endl;
// for(int o=0;o<4;o++)cout<<bas.v[o]<<" ";
// cout<<endl;
}
for(int k=j;k>t;k--)if(!num[k][1])
{
// cout<<"Num: "<<k<<" "<<num[k][2]<<endl;
id=num[k][2];G=F;
//cout<<a[id][0]<<" "<<a[id][1]<<" "<<a[id][2]<<endl;
if(!a[id][2])for(int x:{0,1})for(int y:{0,1})F[x][y]=G[x^1][y]+(x==1)*a[id][0];
else for(int x:{0,1})for(int y:{0,1})F[x][y]=G[x][y^1]+(y==1)*a[id][1];
//cout<<F[0][0]<<" "<<F[0][1]<<" "<<F[1][0]<<" "<<F[1][1]<<endl;
}
// cout<<"Sol: "<<t<<" "<<j<<endl;
//for(int x:{0,1})for(int y:{0,1})cout<<bas.v[x*2+y]<<" "<<F[x][y]<<endl;
//for(int x:{0,1})for(int y:{0,1})Min(f[t][x][y],bas.v[x*2+y]);
for(int o:{0,1,2,3})Min(f[t][o/2][o&1],bas.v[bas.p[o]]);
//for(int x:{0,1})for(int y:{0,1})Min(f[t][x][y],F[x][y]);
// cout<<f[t][0][0]<<" "<<f[t][0][1]<<" "<<f[t][1][0]<<" "<<f[t][1][1]<<endl;
}
cout<<min({f[1][0][0],f[1][0][1],f[1][1][0],f[1][1][1]});
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 45440kb
input:
7 10 12 19 99 10 8 49 9 14 15 199 11 7 19
output:
131
result:
ok single line: '131'
Test #2:
score: 0
Accepted
time: 1ms
memory: 45588kb
input:
7 10 12 19 99 10 8 49 9 14 15 199 11 7 19
output:
131
result:
ok single line: '131'
Test #3:
score: -100
Time Limit Exceeded
input:
199913 1212731 2525164 3210261 2457211 1013738 1931420 923123 867112 762069 2108660 108920 2491869 867107 387025 2278045 574027 1661570 820133 1274150 2001346 779766 3305537 3000211 2418643 2108660 2001343 1074820 2754411 826712 3117447 1661569 338161 1849064 3007920 3057426 468078 3252798 1274146 4...