QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#176919 | #6308. Magic | dengziyue | WA | 11ms | 12432kb | C++14 | 2.4kb | 2023-09-12 10:19:03 | 2023-09-12 10:19:04 |
Judging History
answer
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define s 0
#define t 300000
#define inf 0x3f3f3f3f
int n;
struct A{int l,r;}a[5002];
int to[10002];
int son[300002][2];
int rt[10002];
int ti=0;
struct E{int v,w,nx;}e[1000002];
int ei=1;
int fir[320002];
int de[320002];
queue<int>q;
int ans=0;
void addedge(int u,int v,int w){
e[++ei]=(E){v,w,fir[u]}; fir[u]=ei;
e[++ei]=(E){u,0,fir[v]}; fir[v]=ei;
}
void build(int &o,int l,int r){
if(!o)o=++ti;
if(l>=r)return;
int mid=(l+r)>>1;
build(son[o][0],l,mid);
build(son[o][1],mid+1,r);
addedge(o,son[o][0],inf);
addedge(o,son[0][1],inf);
}
void upd(int &o,int o2,int l,int r,int x,int w){
o=++ti;
if(l>=r){
if(w)addedge(o,x+t,inf);
return;
}
int mid=(l+r)>>1;
if(x<=mid){
son[o][1]=son[o2][1];
upd(son[o][0],son[o2][0],l,mid,x,w);
addedge(o,son[o][0],inf);
}
else{
son[o][0]=son[o2][0];
upd(son[o][1],son[o2][1],mid+1,r,x,w);
addedge(o,son[o][1],inf);
}
}
void query(int o,int l,int r,int ql,int qr,int w){
if(!o)return;
if(ql<=l&&r<=qr){addedge(w+t,o,1); return;}
int mid=(l+r)>>1;
if(ql<=mid)query(son[o][0],l,mid,ql,qr,w);
if(qr>mid)query(son[o][1],mid+1,r,ql,qr,w);
}
bool bfs(){
memset(de,0,sizeof(de));
de[s]=1; q.push(s);
while(!q.empty()){
int u=q.front(); q.pop();
for(int i=fir[u],v;i;i=e[i].nx){
v=e[i].v;
if(e[i].w&&!de[v]){de[v]=de[u]+1; q.push(v);}
}
}
return de[t];
}
int dfs(int u,int fl){
if(u==t||!fl)return fl;
int res=0;
for(int i=fir[u],v,w;i;i=e[i].nx){
v=e[i].v;
if(e[i].w&&de[u]+1==de[v]){
w=dfs(v,min(fl,e[i].w));
if(w){
e[i].w-=w; e[i^1].w+=w;
fl-=w; res+=w;
}
else de[v]=0;
}
}
return res;
}
int main(){
#ifdef dzy
freopen("QOJ6308_1.in","r",stdin);
freopen("QOJ6308_1.out","w",stdout);
#endif
scanf("%d",&n);
for(int i=1;i<=n;++i)scanf("%d%d",&a[i].l,&a[i].r);
memset(fir,0,sizeof(fir));
memset(to,0,sizeof(to));
for(int i=1;i<=n;++i){addedge(s,a[i].l+t,1); addedge(a[i].r+t,t,1);}
for(int i=1;i<=n;++i)to[a[i].l]=a[i].r;
build(rt[n*2+1],1,n*2);
for(int i=n*2;i>=1;--i){
if(to[i]){
if(i+1<=to[i]-1)query(rt[i+1],1,n*2,i+1,to[i]-1,i);
upd(rt[i],rt[i+1],1,n*2,to[i],0);
}
else upd(rt[i],rt[i+1],1,n*2,i,1);
}
while(bfs())ans+=dfs(s,inf);
printf("%d\n",n*2-ans);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 6964kb
input:
5 2 3 6 7 1 9 5 10 4 8
output:
9
result:
ok 1 number(s): "9"
Test #2:
score: -100
Wrong Answer
time: 11ms
memory: 12432kb
input:
5000 7985 7987 42 46 1591 1593 407 410 6305 6306 1456 1457 5874 5875 7135 7137 7041 7046 6813 6815 8868 8871 665 666 4055 4056 9789 9796 7067 7068 4745 4746 5167 5171 1735 1737 2125 2128 1444 1447 1348 1352 6087 6090 1381 1384 1600 1601 5187 5190 2801 2802 8449 8450 9376 9377 4021 4024 2674 2676 490...
output:
8262
result:
wrong answer 1st numbers differ - expected: '8134', found: '8262'