QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#106390 | #6308. Magic | bear0131 | RE | 0ms | 0kb | C++11 | 2.8kb | 2023-05-17 16:19:27 | 2023-05-17 16:19:29 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; ++i)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const int inf = 0x3f3f3f3f;
int n;
array<int, 2> a[5050];
int cn = 0;
namespace mf{
int hd[350350], cur[350350], dis[350350];
int to[350350], cap[350350], nxt[350350], ce = 0;
void clr(){
memset(hd, -1, sizeof(hd));
}
void ae(int f, int t, int c){
//cout << "ae " << f << " " << t << " " << c << endl;
nxt[ce] = hd[f], to[ce] = t, cap[ce] = c, hd[f] = ce++;
nxt[ce] = hd[t], to[ce] = f, cap[ce] = 0, hd[t] = ce++;
}
bool bfs(int S, int T){
fill(dis, dis+cn, inf);
static int q[350350];
int qhd = 0, qrr = 0;
q[qrr++] = S, dis[S] = 0;
while(qhd < qrr){
int u = q[qhd++];
for(int e = hd[u]; e >= 0; e = nxt[e]){
if(!cap[e]) continue;
int t = to[e];
if(dis[t] > dis[u] + 1){
dis[t] = dis[u] + 1;
q[qrr++] = t;
}
}
}
return dis[T] < inf;
}
int dfs(int u, int dest, int f){
if(u == dest) return f;
int nf = 0;
for(int &e = cur[u]; e >= 0; e = nxt[e]){
int t = to[e];
if(!cap[e] || dis[t] != dis[u] + 1) continue;
int ret = dfs(t, dest, min(f, cap[e]));
nf += ret, cap[e] -= ret, cap[e^1] += ret;
if(nf == f) break;
}
return nf;
}
int mf(int S, int T){
int ret = 0;
while(bfs(S, T)){
rep(i, cn) cur[i] = hd[i];
ret += dfs(S, T, inf);
}
return ret;
}
}
int nid[40040];
int upd(int p, int l, int r, int u){
nid[u] = cn++;
//cout << "upd " << p << " " << l << " " << r << " " << u << ": " << nid[u] << endl;
if(l == r)
return nid[u];
int mid = (l+r) >> 1, ret;
if(p <= mid){
ret = upd(p, l, mid, u+u);
} else {
ret = upd(p, mid+1, r, u+u+1);
}
if(nid[u+u] >= 0) mf::ae(nid[u], nid[u+u], inf);
if(nid[u+u+1] >= 0) mf::ae(nid[u], nid[u+u+1], inf);
return ret;
}
void segae(int f, int tl, int tr, int l, int r, int u){
//cout << "segae " << f << " " << tl << " " << tr << " " << l << " " << r << " " << u << ": " << nid[u] << endl;
if(nid[u] < 0 || tl > r || l > tr) return ;
if(tl <= l && r <= tr) return mf::ae(f, nid[u], 1);
int mid = (l+r) >> 1;
segae(f, tl, tr, l, mid, u+u), segae(f, tl, tr, mid+1, r, u+u+1);
}
int main(){
freopen("frog.in", "r", stdin);
freopen("frog.out", "w", stdout);
ios::sync_with_stdio(false);
cin >> n;
rep(i, n)
cin >> a[i][0] >> a[i][1], --a[i][0], --a[i][1];
sort(a, a+n);
memset(nid, -1, sizeof(nid));
mf::clr();
cn = 2;
rep(i, n){
//cout << "-----------" << i << ": " << a[i][0] << " " << a[i][1] << "-----------------" << endl;
mf::ae(upd(a[i][1], 0, 2*n-1, 1), 1, 1);
mf::ae(0, cn, 1);
segae(cn, a[i][0]+1, a[i][1]-1, 0, 2*n-1, 1);
++cn;
}
cout << 2*n - mf::mf(0, 1) << "\n";
return 0;
}
// by zxb
// start coding at 15:32
// submission #1 at 16:14
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
5 2 3 6 7 1 9 5 10 4 8