QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#116030#5812. MarblesZesty_Fox39 ✓27ms11928kbC++203.1kb2023-06-27 23:51:452023-06-27 23:51:47

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-27 23:51:47]
  • 评测
  • 测评结果:39
  • 用时:27ms
  • 内存:11928kb
  • [2023-06-27 23:51:45]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

using u32=unsigned int;
using i64=long long;
using u64=unsigned long long;
using db=double;
using vi=vector<int>;
using pii=pair<int,int>;

template<typename T>
T read(){
    T x=0,f=0;char ch=getchar();
    while(!isdigit(ch)) f|=(ch=='-'),ch=getchar();
    while(isdigit(ch)) x=x*10+(ch-'0'),ch=getchar();
    return f?-x:x;
}

#define rdi read<int>
#define rdi64 read<i64>
#define fi first
#define se second
#define pb push_back

const int N=1010,INF=0x3f3f3f3f;

int n;
pii p[N];
vi e[N];

int bl[N],col[N],cnt;
pii rg[N];
bool dfs1(int x,int c,int b){
    bl[x]=b,col[x]=c;
    rg[b].fi=min(rg[b].fi,p[x].fi);
    rg[b].se=max(rg[b].se,p[x].se);
    bool ret=1;
    for(auto y:e[x]){
        if(!col[y]) ret&=dfs1(y,3-c,b);
        else if(col[y]==col[x]) ret=0;
    }
    return ret;
}

int f[N][N],g[N][N];
void dfs2(int x){
    vi ct,son;
    for(int i=1;i<=cnt;i++)
        if(rg[x].fi<rg[i].fi&&rg[i].se<rg[x].se) ct.pb(i);
    sort(ct.begin(),ct.end(),
        [&](int x,int y){return rg[x]<rg[y];});
    int lst=0;
    for(auto y:ct)
        if(lst<rg[y].fi) son.pb(y),lst=rg[y].se;
    for(auto y:son) dfs2(y);

    static int up[N],dw[N];
    for(int i=1;i<=2*n;i++) up[i]=dw[i]=0;
    for(int i=1;i<=n;i++){
        if(bl[i]==x){
            if(col[i]==1) up[p[i].fi]++,up[p[i].se+1]--;
            else dw[p[i].fi]++,dw[p[i].se+1]--;
        }
    }
    int mxu=0,mxd=0;
    for(int i=1;i<=2*n;i++){
        up[i]+=up[i-1],dw[i]+=dw[i-1];
        mxu=max(mxu,up[i]),mxd=max(mxd,dw[i]);
    }

    for(int i=0;i<=n;i++)
        f[x][i]=(i<mxu?INF:mxd),g[x][i]=(i<mxd?INF:mxu);
    for(auto y:son){
        int u=up[rg[y].fi],d=dw[rg[y].fi];
        for(int i=0;i<=n;i++){
            if(i>=u) f[x][i]=max(f[x][i],d+f[y][i-u]);
            if(i>=d) g[x][i]=max(g[x][i],u+f[y][i-d]);
        }
    }
    for(int i=0;i<=n;i++)
        f[x][i]=min(f[x][i],g[x][i]);
}

void clear(){
    cnt=0;
    for(int i=1;i<=n;i++)
        e[i].clear(),bl[i]=col[i]=0;
}

void solve(int id){
    cout<<"Case #"<<id<<": ";
    cin>>n;
    map<string,int> mp;
    for(int i=1,c=0;i<=2*n;i++){
        string s;cin>>s;
        auto it=mp.find(s);
        if(it==mp.end()) mp.insert({s,i});
        else p[++c]={it->se,i};
    }
    
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++){
            if((p[i].fi<p[j].fi&&p[j].fi<p[i].se&&p[i].se<p[j].se)||
                (p[j].fi<p[i].fi&&p[i].fi<p[j].se&&p[j].se<p[i].se))
                e[i].pb(j),e[j].pb(i);
        }
    bool fl=1;
    for(int i=1;i<=n;i++)
        if(!col[i]){
            rg[++cnt]={2*n+1,0};
            fl&=dfs1(i,1,cnt);
        }
    if(!fl) return cout<<"-1"<<'\n',clear();
    rg[0]={0,2*n+1},dfs2(0);
    int ans=2*n;
    for(int i=0;i<=n;i++)
        ans=min(ans,i+f[0][i]);
    cout<<ans<<'\n',clear();
}

int main(){
#ifdef LOCAL
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif
    ios::sync_with_stdio(false);
    int T;cin>>T;
    for(int i=1;i<=T;i++) solve(i);
    return 0;
}

詳細信息

Subtask #1:

score: 7
Accepted

Test #1:

score: 7
Accepted
time: 2ms
memory: 5580kb

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: 27ms
memory: 11928kb

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