QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#571751 | #9315. Rainbow Bracket Sequence | N_z_ | WA | 0ms | 3668kb | C++14 | 7.0kb | 2024-09-18 07:56:33 | 2024-09-18 07:56:34 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
struct time_helper
{
#ifdef LOCAL
clock_t time_last;
#endif
time_helper()
{
#ifdef LOCAL
time_last=clock();
#endif
}
void test()
{
#ifdef LOCAL
auto time_now=clock();
std::cerr<<"time:"<<1.*(time_now-time_last)/CLOCKS_PER_SEC<<";all_time:"<<1.*time_now/CLOCKS_PER_SEC<<std::endl;
time_last=time_now;
#endif
}
~time_helper()
{
test();
}
}time_helper;
#ifdef LOCAL
#include"dbg.h"
#else
#define dbg(...) (__VA_ARGS__)
#endif
namespace Fread{const int SIZE=1<<16;char buf[SIZE],*S,*T;inline char getchar(){if(S==T){T=(S=buf)+fread(buf,1,SIZE,stdin);if(S==T)return'\n';}return *S++;}}namespace Fwrite{const int SIZE=1<<16;char buf[SIZE],*S=buf,*T=buf+SIZE;inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}inline void putchar(char c){*S++=c;if(S==T)flush();}struct NTR{~NTR(){flush();}}ztr;}
#define getchar Fread::getchar
#define putchar Fwrite::putchar
#define Setprecision 10
#define between '\n'
#define __int128 long long
template<typename T>struct is_char{static constexpr bool value=(std::is_same<T,char>::value||std::is_same<T,signed char>::value||std::is_same<T,unsigned char>::value);};template<typename T>struct is_integral_ex{static constexpr bool value=(std::is_integral<T>::value||std::is_same<T,__int128>::value)&&!is_char<T>::value;};template<typename T>struct is_floating_point_ex{static constexpr bool value=std::is_floating_point<T>::value||std::is_same<T,__float128>::value;};namespace Fastio{struct Reader{template<typename T>typename std::enable_if_t<std::is_class<T>::value,Reader&>operator>>(T&x){for(auto &y:x)*this>>y;return *this;}template<typename T>typename std::enable_if_t<is_integral_ex<T>::value,Reader&>operator>>(T&x){char c=getchar();short f=1;while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}x=0;while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}x*=f;return *this;}template<typename T>typename std::enable_if_t<is_floating_point_ex<T>::value,Reader&>operator>>(T&x){char c=getchar();short f=1,s=0;x=0;T t=0;while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else return x*=f,*this;while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}template<typename T>typename std::enable_if_t<is_char<T>::value,Reader&>operator>>(T&c){c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();return *this;}Reader&operator>>(char*str){int len=0;char c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();while(c!='\n'&&c!=' '&&c!='\r')str[len++]=c,c=getchar();str[len]='\0';return*this;}Reader&operator>>(std::string&str){str.clear();char c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();while(c!='\n'&&c!=' '&&c!='\r')str.push_back(c),c=getchar();return*this;}Reader(){}}cin;const char endl='\n';struct Writer{typedef __int128 mxdouble;template<typename T>typename std::enable_if_t<std::is_class<T>::value,Writer&>operator<<(T x){for(auto &y:x)*this<<y<<between;*this<<'\n';return *this;}template<typename T>typename std::enable_if_t<is_integral_ex<T>::value,Writer&>operator<<(T x){if(x==0)return putchar('0'),*this;if(x<0)putchar('-'),x=-x;static int sta[45];int top=0;while(x)sta[++top]=x%10,x/=10;while(top)putchar(sta[top]+'0'),--top;return*this;}template<typename T>typename std::enable_if_t<is_floating_point_ex<T>::value,Writer&>operator<<(T x){if(x<0)putchar('-'),x=-x;x+=pow(10,-Setprecision)/2;mxdouble _=x;x-=(T)_;static int sta[45];int top=0;while(_)sta[++top]=_%10,_/=10;if(!top)putchar('0');while(top)putchar(sta[top]+'0'),--top;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top)putchar(sta[top]+'0'),--top;return*this;}template<typename T>typename std::enable_if_t<is_char<T>::value,Writer&>operator<<(T c){putchar(c);return*this;}Writer&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return *this;}Writer&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer&operator<<(std::string str){int st=0,ed=str.size();while(st<ed)putchar(str[st++]);return*this;}Writer(){}}cout;}
#define cin Fastio::cin
#define cout Fastio::cout
#define endl Fastio::endl
template<class Fun>class y_combinator_result{Fun fun_;public:template<class T>explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}template<class ...Args>decltype(auto) operator()(Args &&...args){return fun_(std::ref(*this), std::forward<Args>(args)...);}};template<class Fun>decltype(auto) y_combinator(Fun &&fun){return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));}
void solve();
main()
{
int t=1;
cin>>t;
while(t--)solve();
}
constexpr int inf=1e9;
int flow[1001],ins[1001];
pair<int,int>pre[1001];
long long dis[1001],nf[1001];
int n;
vector<tuple<int,int,int>>e[1001];
bool spfa(int s,int t)
{
for(int x=1;x<=n;x++)
dis[x]=1e18,ins[x]=0,nf[x]=0;
dis[s]=0;ins[s]=1;nf[s]=1e18;
queue<int>q;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
ins[u]=0;
for(auto [v,w,id]:e[u])
if(flow[id]&&dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
nf[v]=min<long long>(nf[u],flow[id]);
pre[v]={u,id};
if(!ins[v])q.push(v),ins[v]=1;
}
}
return dis[t]!=1e18;
}
pair<long long,long long>dinic(int s,int t)
{
long long res=0,ct=0;
while(spfa(s,t))
{
long long fl=nf[t];
res+=fl;
ct+=fl*dis[t];
int u=t;
while(u!=s)
{
flow[pre[u].second]-=fl;
flow[pre[u].second^1]+=fl;
u=pre[u].first;
}
}
return {res,ct};
}
int id=0;
void adde(int u,int v,int c,int w)
{
flow[id]=c;
e[u].emplace_back(v,w,id++);
flow[id]=0;
e[v].emplace_back(u,-w,id++);
}
int in[1001],out[1001],d[1001];
void solve()
{
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
memset(d,0,sizeof(d));
memset(flow,0,sizeof(flow));
n=0;
for(int x=0;x<=1000;x++)
e[x].clear();
id=0;
int n,m;
cin>>n>>m;
int s1=2*n+m+2,t1=s1+1,s=t1+1,t=s+1,S=t+1,T=S+1;
::n=T;
vector<int>l(m+1),c(2*n+1),v(2*n+1);
for(int x=1;x<=m;x++)
cin>>l[x];
for(int x=1;x<=2*n;x++)
cin>>c[x];
for(int x=1;x<=2*n;x++)
cin>>v[x];
if(![&](){
vector<int>cnt(m+1);
int lc=0;
for(int x=1;x<=2*n;x++)
{
if(cnt[c[x]]<l[c[x]])cnt[c[x]]++,lc++;
else if(lc>0)lc--;
else cnt[c[x]]++,lc++;
}
for(int x=1;x<=m;x++)
if(cnt[x]<l[x])return 0;
return 1;
}()){cout<<-1<<endl;return;}
for(int x=1;x<=m;x++)
{
adde(s1,x,2*n-l[x],0);
in[x]+=l[x],out[s1]+=l[x];
}
long long ans=0;
for(int x=1;x<=2*n;x++)
{
adde(x+m,c[x],1,v[x]);
ans+=-v[x];
d[c[x]]--,d[x+m]++;
adde(x+m,x+m+1,n-(x+1)/2,0);
in[x+m+1]+=(x+1)/2,out[x+m]+=(x+1)/2;
}
adde(2*n+m+1,t1,0,0);
in[t1]+=n,out[2*n+m+1]+=n;
for(int x=1;x<=t1;x++)
{
if(in[x]>out[x])adde(s,x,in[x]-out[x],0);
if(out[x]>in[x])adde(x,t,out[x]-in[x],0);
if(d[x]>0)adde(S,x,d[x],0);
if(d[x]<0)adde(x,T,-d[x],0);
}
adde(t1,s1,inf,0);
adde(t,s,inf,0);
ans+=dinic(S,T).second;
flow[id-2]=flow[id-1]=0;
ans+=dinic(s,t).second;
cout<<-ans<<endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3668kb
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 9
result:
wrong answer 2nd numbers differ - expected: '-1', found: '9'