QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#571753 | #9315. Rainbow Bracket Sequence | N_z_ | WA | 7ms | 3920kb | C++14 | 7.0kb | 2024-09-18 07:58:15 | 2024-09-18 07:58:17 |
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++;
}
int s=0;
for(int x=1;x<=m;s+=cnt[x],x++)
if(cnt[x]<l[x])return false;
return s<=n;
}()){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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3624kb
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: 0
Accepted
time: 1ms
memory: 3596kb
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...
output:
-1 343766215 2351080746 3426965219 -1 -1 1351561190 2539318868 1013080942 4656646546 -1 -1 2231197660 2719131728 3983627641 4712174168 -1 1046749330 6115214757 3920988203 -1 3902088946 -1 2566553992 5268471900 5977120748 7505501534 -1 5054275471 1467678317 883992368 1044562986 -1 4024634503 -1 14472...
result:
ok 50 numbers
Test #3:
score: 0
Accepted
time: 0ms
memory: 3920kb
input:
25 20 4 0 0 0 1 2 2 2 2 4 4 4 1 2 2 2 1 3 4 1 3 4 4 3 1 3 2 1 4 2 2 4 1 2 2 3 3 4 1 3 1 4 1 2 1 569363376 821862673 89663213 862407789 442940149 823902948 903631686 838830270 645793571 397350060 806574247 373166844 448916252 435880456 969841293 998227951 276194969 654967687 396909705 696186514 16992...
output:
16418796680 10558213381 -1 1502390329 8534183652 13571062458 8007768610 12656453595 3378659874 8410746077 12352187024 5743130624 5952844559 2285684441 4242613506 836778846 4838639494 8586807028 8535185475 8089358175 5627473863 14399529671 -1 11483578919 4919631490
result:
ok 25 numbers
Test #4:
score: 0
Accepted
time: 1ms
memory: 3712kb
input:
83 6 5 1 0 1 1 0 2 4 4 5 3 2 4 5 3 3 3 3 597659626 649040970 33207011 223207847 960704874 418600362 658594226 417168695 767527655 622701955 867509363 235369723 6 2 0 0 1 1 2 2 2 2 1 1 1 2 2 1 405752009 976807343 267881918 26193206 947664189 555835767 587219021 231445627 755417826 541362608 846129246...
output:
-1 4518989634 3550182642 4529809699 4042429510 4145000717 -1 3635082691 -1 -1 3476472607 3732904849 3631909633 4479534795 3586223781 3380039505 2946284506 3615784040 -1 -1 -1 4940773463 3195952843 4073152216 4177883697 3398540362 3578975642 4308395607 -1 3078447178 3069102942 3135294474 5256676097 -...
result:
ok 83 numbers
Test #5:
score: 0
Accepted
time: 0ms
memory: 3704kb
input:
71 7 4 0 1 0 4 3 4 1 1 4 4 2 4 1 1 1 4 4 4 580852652 638740575 585501313 439482552 637837864 335796176 447934224 259084035 778210267 469729886 908657968 750731414 508195022 701461051 7 6 0 1 1 0 0 1 3 2 4 3 5 3 1 1 5 4 3 1 6 1 198076752 601490845 123074777 392892100 148645775 938575995 355185760 558...
output:
4300550873 4711297430 -1 4468072610 4652801753 4661069155 5134971483 4367382968 4983190626 3065242360 -1 -1 4834379200 4355918462 -1 3592789392 3058869770 -1 3825913893 -1 4785350296 -1 4759459279 5342744097 4716826205 4873131448 5329193547 4821943641 3777324532 4115469556 -1 -1 -1 5061832610 520025...
result:
ok 71 numbers
Test #6:
score: 0
Accepted
time: 1ms
memory: 3908kb
input:
62 8 3 0 2 0 3 3 3 1 1 1 3 2 1 2 2 1 1 2 1 1 222368048 906033133 8623893 807375696 461796409 362923880 194114590 733391789 137574156 670510137 237249112 673135534 595041001 875171159 112263159 649035661 8 6 2 1 0 0 0 0 3 5 2 2 1 3 3 3 6 4 5 5 1 2 5 4 28938721 556768926 23366504 887715271 624732370 3...
output:
5349781905 4269103485 4384563617 5171071054 4895598910 4667548481 -1 4157414045 -1 3927911942 -1 5127481462 5534185037 6071114899 4515756162 5965926191 -1 5617252300 5920664214 5826871785 5730385164 5947153970 5426721265 5820040011 5677486289 5193366586 6129016249 5739984903 5993708705 5520537026 54...
result:
ok 62 numbers
Test #7:
score: 0
Accepted
time: 1ms
memory: 3648kb
input:
55 9 9 2 2 0 0 0 0 0 2 0 6 2 3 9 5 4 2 4 1 1 4 7 1 4 5 8 6 2 907208811 754008138 161288468 562810007 149992530 997421612 144383292 832081887 228097972 446662965 77258752 375836694 743196568 527846851 580675905 438791943 977960026 39388076 9 6 0 1 0 0 0 0 5 3 3 4 3 6 5 4 6 5 2 5 6 5 5 1 2 2 861149655...
output:
-1 5600105080 -1 7505123959 7048625501 4827971490 -1 7031642652 -1 6001013535 -1 -1 6353971413 5896906204 3896102243 6540293759 5621534278 6599175212 -1 6721932183 6965230904 5681597954 8167088460 5632185532 -1 4750725522 6285591217 6320310809 6388859035 4686377743 5728065266 5503485599 6260485694 6...
result:
ok 55 numbers
Test #8:
score: 0
Accepted
time: 2ms
memory: 3744kb
input:
50 10 8 0 0 1 0 0 0 1 0 1 6 7 7 2 2 1 1 3 1 1 3 7 5 4 1 8 4 7 2 535526356 404334728 653535984 998133227 879226371 59632864 356493387 62611196 827258251 296576565 204244054 812713672 780267148 614679390 447700005 102067050 544546349 116002772 761999375 546951131 10 5 0 0 1 1 0 4 5 5 3 5 1 3 3 5 1 1 5...
output:
7267674502 6912276073 -1 -1 8427372986 -1 7057744914 6452405474 7564223610 7193916415 -1 5496837745 6671753900 7137256654 6574886409 7690341704 7357840728 8164970807 7172570060 6778745196 7668051341 6936083804 7305907682 7631088969 5717910532 6156605721 6923807013 -1 8207034493 -1 7418567782 6923770...
result:
ok 50 numbers
Test #9:
score: 0
Accepted
time: 2ms
memory: 3716kb
input:
33 15 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 985238289 459697693 970988548 370603489 160471107 36299429 908579552 62669495 649913003 478356148 805843616 136680216 158560673 261854484 857048420 32835236 430050478 327696352 417017537 857880465 568473106 750242567 865990206 869...
output:
11069294141 9757752433 10517453854 10675484732 9851733289 11571987501 10382709663 11006679388 9835650684 10482963923 10190220836 11857634113 -1 -1 10077553084 9896319722 11821564137 11828952526 9761971634 9940132164 -1 -1 9227926173 13037241524 11565236192 11800412693 12028054248 11502933189 9949512...
result:
ok 33 numbers
Test #10:
score: 0
Accepted
time: 2ms
memory: 3716kb
input:
25 20 20 3 0 0 1 0 0 0 0 0 3 0 0 1 2 0 1 0 2 2 4 12 19 17 19 14 5 16 6 6 20 13 2 14 7 19 16 17 7 13 16 9 6 5 16 13 13 9 9 8 6 10 11 20 7 4 12 16 13 11 9 654967687 396909705 696186514 169923749 8142639 81507010 67587218 966803487 991350519 551259762 962079443 918589 708293964 213990501 934701547 8468...
output:
-1 14023274173 12588200963 13988453624 15030243485 13076569052 -1 -1 13842307153 -1 12832546330 14189266584 16492323989 16163650514 14012035305 -1 -1 -1 13929001098 13862644942 -1 15246522629 -1 13299413733 -1
result:
ok 25 numbers
Test #11:
score: -100
Wrong Answer
time: 7ms
memory: 3712kb
input:
5 100 15 3 5 8 6 7 7 5 3 2 6 5 3 11 4 8 8 13 6 13 2 3 1 8 15 12 13 14 10 12 4 4 8 8 9 2 15 3 4 10 8 5 2 5 11 11 2 13 10 7 12 11 4 2 9 4 15 5 15 13 9 6 7 6 2 12 6 1 6 13 9 7 2 2 11 11 10 1 3 12 8 7 2 13 2 5 3 13 5 11 5 2 3 1 14 7 11 5 11 2 14 2 14 6 4 6 3 8 14 4 8 3 14 10 7 8 3 6 3 10 4 15 1 7 7 15 7...
output:
result:
wrong answer 1st numbers differ - expected: '68656287465', found: '0'