QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#577994 | #9315. Rainbow Bracket Sequence | fishcathu | TL | 0ms | 3552kb | C++23 | 4.1kb | 2024-09-20 15:53:09 | 2024-09-20 15:53:09 |
Judging History
answer
#include<bits/stdc++.h>
#define ls i<<1
#define rs i<<1|1
#define fi first
#define se second
#define min amin
#define max amax
#define eb emplace_back
using namespace std;
using ll=long long;
using LL=__int128;
using pii=pair<int,int>;
const int N=310;
const int inf=1E9;
const int p=998244353;
template<typename T=int>T read(){T x;cin>>x;return x;}
template<typename U,typename V>U min(const U &x,const V &y){return x<y?x:y;}
template<typename U,typename V>U max(const U &x,const V &y){return y<x?x:y;}
template<typename U,typename ...V>U min(const U &x,const V &...y){return min(x,min(y...));}
template<typename U,typename ...V>U max(const U &x,const V &...y){return max(x,max(y...));}
template<typename U,typename V>bool cmin(U &x,const V &y){return y<x?x=y,true:false;}
template<typename U,typename V>bool cmax(U &x,const V &y){return x<y?x=y,true:false;}
template<typename U,typename ...V>bool cmin(U &x,const V &...y){return cmin(x,min(y...));}
template<typename U,typename ...V>bool cmax(U &x,const V &...y){return cmax(x,max(y...));}
template<typename T>T qpow(T x,int n){T y=1;for(;n;n>>=1,x*=x)if(n&1)y*=x;return y;}
istream &operator>>(istream &is,LL &x){string a;is>>a;bool k=a[0]=='-';if(k)a=a.substr(1);x=0;for(char &t:a)x=x*10+t-48;if(k)x=-x;return is;}
ostream &operator<<(ostream &os,LL x){if(x<0)os<<'-',x=-x;string a;do a+=x%10|48;while(x/=10);reverse(a.begin(),a.end());return os<<a;}
struct mint{
int x;
mint():x(){}
mint(const int &x):x(x<0?x+p:x){}
mint inv()const{return qpow(*this,p-2);}
mint &operator+=(const mint &t){return (x+=t.x)<p?0:x-=p,*this;}
mint &operator-=(const mint &t){return (x-=t.x)<0?x+=p:0,*this;}
mint &operator*=(const mint &t){return x=ll(x)*t.x%p,*this;}
mint &operator/=(const mint &t){return *this*=t.inv();}
mint operator+(const mint &t)const{return mint(*this)+=t;}
mint operator-(const mint &t)const{return mint(*this)-=t;}
mint operator*(const mint &t)const{return mint(*this)*=t;}
mint operator/(const mint &t)const{return mint(*this)/=t;}
bool operator==(const mint &t)const{return x==t.x;}
friend istream &operator>>(istream &is,mint &t){return is>>t.x;}
friend ostream &operator<<(ostream &os,const mint &t){return os<<t.x;}
};
struct node{
int x;
ll d;
node(const int &x,const ll &d):x(x),d(d){}
bool operator<(const node &t)const{
return d>t.d;
}
};
ll dis[N],h[N],ans;
int pre[N],col[N],n,m,S,T,flow;
vector<tuple<int,int,int,int>>G[N];
void add(int u,int v,int k,int w){
G[u].eb(v,k,w,G[v].size());
G[v].eb(u,0,-w,G[u].size()-1);
}
bool dij(){
for(int i=0;i<=T;++i){
h[i]+=dis[i];
dis[i]=LLONG_MAX;
}
priority_queue<node>q;
dis[S]=0;
q.emplace(S,0);
while(!q.empty()){
auto [u,d]=q.top();
q.pop();
if(d!=dis[u])continue;
for(auto &[v,k,w,id]:G[u])if(k&&cmin(dis[v],d+w+h[u]-h[v])){
pre[v]=id;
q.emplace(v,dis[v]);
}
}
return dis[T]!=LLONG_MAX;
}
void MCMF(){
while(dij()){
int mn=inf;
for(int i=T;i!=S;){
auto &[v,k,w,id]=G[i][pre[i]];
cmin(mn,get<1>(G[i=v][id]));
}
flow+=mn;
ans-=mn*(dis[T]+h[T]);
for(int i=T;i!=S;){
auto &[v,k,w,id]=G[i][pre[i]];
k+=mn;
get<1>(G[i=v][id])-=mn;
}
}
}
void solve(){
cin>>n>>m;
int _n=n,sum=0;
n<<=1;
S=n+m+1;
T=S+1;
for(int i=0;i<=T;++i)G[i].clear();
for(int i=n+1,x;i<S;++i){
cin>>x;
sum+=x;
add(i,T,x,0);
add(i,0,inf,0);
}
for(int i=1;i<=n;++i)cin>>col[i];
for(int i=1,x;i<=n;++i){
cin>>x;
add(i,n+col[i],1,-x);
if(i&1)add(S,i,1,0);
if(i!=1)add(i,i-1,inf,0);
}
if(_n<sum){
cout<<"-1\n";
return;
}
add(0,T,_n-sum,0);
MCMF();
if(flow==_n)cout<<ans<<'\n';
else cout<<"-1\n";
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin>>T;
while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3552kb
input:
2 3 2 1 2 1 2 2 2 1 2 3 1 4 2 2 1 3 2 2 2 1 2 2 2 1 2 3 1 4 2 2 1
output:
9 -1
result:
ok 2 number(s): "9 -1"
Test #2:
score: -100
Time Limit Exceeded
input:
50 8 6 0 2 2 0 3 1 3 5 1 1 5 3 5 2 3 2 2 1 2 2 4 6 998133227 879226371 59632864 356493387 62611196 827258251 296576565 204244054 812713672 780267148 614679390 447700005 102067050 544546349 116002772 761999375 1 1 1 1 1 343766215 374461155 3 1 2 1 1 1 1 1 1 796323508 303640854 701432076 853325162 610...