QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#115073 | #5812. Marbles | zhouhuanyi | 39 ✓ | 34ms | 6668kb | C++11 | 3.4kb | 2023-06-24 16:01:43 | 2023-06-24 16:01:44 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<map>
#include<vector>
#include<algorithm>
#define N 1000
using namespace std;
int read()
{
char c=0;
int sum=0;
while (c<'0'||c>'9') c=getchar();
while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
return sum;
}
int T,n,ans,length,st[N+1],leng,cl[N+1],l[N+1],r[N+1],sl[N+1],sr[N+1],delta[N+1],delta2[N+1],dp[N+1][N+1],DP[N+1];
const int inf=(int)(1e9);
bool op,used[N+1],vis[N+1];
string s[N+1];
map<string,int>P;
vector<int>p[N+1];
vector<int>E[N+1];
vector<int>ES[N+1];
void dfs(int x,int rt)
{
used[x]=1,p[rt].push_back(x);
for (int i=0;i<E[x].size();++i)
{
if (!used[E[x][i]]) cl[E[x][i]]=cl[x]^1,dfs(E[x][i],rt);
else if (cl[x]==cl[E[x][i]]) op=0;
}
return;
}
bool cmp(int x,int y)
{
return sl[x]<sl[y];
}
void dfs2(int x)
{
int maxn=0,maxn2=0;
for (int i=0;i<ES[x].size();++i) dfs2(ES[x][i]);
for (int i=1;i<=(n<<1);++i) delta[i]=delta2[i]=0;
for (int i=0;i<p[x].size();++i)
{
if (!cl[p[x][i]])
{
for (int j=l[p[x][i]];j<=r[p[x][i]];++j) delta[j]++;
}
else
{
for (int j=l[p[x][i]];j<=r[p[x][i]];++j) delta2[j]++;
}
}
for (int i=1;i<=(n<<1);++i) maxn=max(maxn,delta[i]),maxn2=max(maxn2,delta2[i]);
//0
for (int i=0;i<=n;++i) DP[i]=0;
for (int i=0;i<ES[x].size();++i)
{
for (int j=0;j<=n;++j) st[j]=inf;
for (int j=0;j+delta[l[ES[x][i]]]<=n;++j) st[j+delta[l[ES[x][i]]]]=dp[ES[x][i]][j]+delta2[l[ES[x][i]]];
for (int j=1;j<=n;++j) st[j]=min(st[j],st[j-1]);
for (int j=0;j<=n;++j) DP[j]=max(DP[j],st[j]);
}
for (int i=0;i<=maxn-1;++i) DP[i]=inf;
for (int i=0;i<=n;++i) DP[i]=max(DP[i],maxn2),dp[x][i]=min(dp[x][i],DP[i]);
//1
for (int i=0;i<=n;++i) DP[i]=0;
for (int i=0;i<ES[x].size();++i)
{
for (int j=0;j<=n;++j) st[j]=inf;
for (int j=0;j+delta2[l[ES[x][i]]]<=n;++j) st[j+delta2[l[ES[x][i]]]]=dp[ES[x][i]][j]+delta[l[ES[x][i]]];
for (int j=1;j<=n;++j) st[j]=min(st[j],st[j-1]);
for (int j=0;j<=n;++j) DP[j]=max(DP[j],st[j]);
}
for (int i=0;i<=maxn2-1;++i) DP[i]=inf;
for (int i=0;i<=n;++i) DP[i]=max(DP[i],maxn),dp[x][i]=min(dp[x][i],DP[i]);
return;
}
int main()
{
T=read();
for (int i=1;i<=T;++i)
{
n=read(),P.clear(),length=leng=0,op=1,ans=inf;
for (int j=1;j<=(n<<1);++j)
{
cin>>s[j];
if (P.find(s[j])==P.end()) P[s[j]]=++length,l[length]=j;
else r[P[s[j]]]=j;
}
for (int j=0;j<=length;++j) p[j].clear(),E[j].clear(),ES[j].clear(),used[j]=vis[j]=0;
for (int j=1;j<=length;++j)
for (int k=1;k<=length;++k)
if (l[j]<l[k]&&l[k]<r[j]&&r[j]<r[k])
E[j].push_back(k),E[k].push_back(j);
for (int j=1;j<=length;++j)
if (!used[j])
{
cl[j]=0,dfs(j,j),sl[j]=inf,sr[j]=0;
for (int k=0;k<p[j].size();++k) sl[j]=min(sl[j],l[p[j][k]]),sr[j]=max(sr[j],r[p[j][k]]);
st[++leng]=j;
}
if (!op)
{
printf("Case #%d: -1\n",i);
continue;
}
sort(st+1,st+leng+1,cmp);
for (int j=leng;j>=1;--j)
for (int k=j+1;k<=leng;++k)
if (sr[st[k]]<sr[st[j]]&&!vis[st[k]])
ES[st[j]].push_back(st[k]),vis[st[k]]=1;
for (int j=1;j<=leng;++j)
if (!vis[st[j]])
ES[0].push_back(st[j]);
for (int j=0;j<=length;++j)
for (int k=0;k<=n;++k)
dp[j][k]=inf;
dfs2(0);
for (int i=0;i<=n;++i) ans=min(ans,i+dp[0][i]);
printf("Case #%d: %d\n",i,ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 7
Accepted
Test #1:
score: 7
Accepted
time: 2ms
memory: 3836kb
input:
50 14 l c c i l i g g e e h o h f o f d d m m b b n n k k j j 20 n r q b i b i r n q k u o j c p m l m l d e d e h t h t p j c u o g k g s f s f 12 j k b l g d i h f e m c g l b k j c m e f h i d 18 s n o m m j o n d s f d f p p j e q i g q c g i e c h r l h b k l k r b 12 i e i e j m b h b j h m k ...
output:
Case #1: 2 Case #2: 8 Case #3: 12 Case #4: 5 Case #5: 4 Case #6: 3 Case #7: 2 Case #8: -1 Case #9: 2 Case #10: 4 Case #11: -1 Case #12: 10 Case #13: 4 Case #14: 5 Case #15: -1 Case #16: 3 Case #17: 2 Case #18: 6 Case #19: 2 Case #20: 5 Case #21: -1 Case #22: -1 Case #23: 5 Case #24: 4 Case #25: 2 Ca...
result:
ok 50 lines
Subtask #2:
score: 32
Accepted
Test #2:
score: 32
Accepted
time: 34ms
memory: 6668kb
input:
50 500 fl sr ec fl fn ec eq fn qb eq cf qb rk cf tf rk tk tf j tk qc j af qc vc af ul vc br ul rr br pd rr zm pd nq zm re nq gi re hr gi sh hr ip sh nl ip pg nl ih pg yj ih ss yj ck ss or ck qp or ol qp nh ol jb nh is jb gg is di gg pp di sn pp mn sn zn mn vp zn wp vp bi wp pm bi zr pm ao zr kf ao i...
output:
Case #1: -1 Case #2: -1 Case #3: 4 Case #4: 5 Case #5: 20 Case #6: -1 Case #7: 51 Case #8: 21 Case #9: 11 Case #10: 33 Case #11: 28 Case #12: 28 Case #13: 19 Case #14: 72 Case #15: 47 Case #16: 5 Case #17: 26 Case #18: 12 Case #19: 13 Case #20: 77 Case #21: -1 Case #22: 43 Case #23: 18 Case #24: 20 ...
result:
ok 50 lines
Extra Test:
score: 0
Extra Test Passed