QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#537611#6308. Magicchenxinyang2006TL 2565ms10372kbC++203.2kb2024-08-30 16:37:522024-08-30 16:37:52

Judging History

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

  • [2024-08-30 16:37:52]
  • 评测
  • 测评结果:TL
  • 用时:2565ms
  • 内存:10372kb
  • [2024-08-30 16:37:52]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define eb emplace_back
#define SZ(S) (int)S.size()
//#define mod 998244353
//#define mod 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
using namespace std;

template <class T>
void chkmax(T &x,T y){
	if(x < y) x = y;
}

template <class T>
void chkmin(T &x,T y){
	if(x > y) x = y;
}

inline int popcnt(int x){
	return __builtin_popcount(x);
}

inline int ctz(int x){
	return __builtin_ctz(x);
}


/*ll power(ll p,int k = mod - 2){
	ll ans = 1;
	while(k){
		if(k % 2 == 1) ans = ans * p % mod;
		p = p * p % mod;
		k /= 2;	
	}
	return ans;
}*/
int T,n;
int a[5005],b[5005];

bitset <5005> G[2][5005],okl,okr,cur;
int prv[10005],ans[2][5005],p;
queue <int> Q;
void psh(int u,int v){
	prv[u] = v;
	if(u <= n){
		assert(okl.test(u));
		okl.reset(u);
	}else{
		assert(okr.test(u - n));
		okr.reset(u - n);
		if(!ans[1][u - n]) p = u - n;
	}
	Q.push(u);
}

void bfs(){
	okl.reset();
	okr.reset();
	rep(u,1,n){
		okl.set(u);
		okr.set(u);
	}
	while(!Q.empty()) Q.pop();
	rep(u,1,n) if(!ans[0][u]) psh(u,0);
	p = 0;

	while(!Q.empty()){
		int u = Q.front();
		Q.pop();
		if(u <= n){
			cur = G[0][u] & okr;
			while(cur.any()){
				int v = cur._Find_first();
				psh(v + n,u);
				cur.reset(v);
				if(p) break;
			}
		}else{
			cur = G[1][u - n] & okl;
			while(cur.any()){
				int v = cur._Find_first();
				psh(v,u);
				cur.reset(v);
				if(p) break;
			}
		}
	}
}

int _p[15],tag[25];
int brute(){
	rep(i,1,n) _p[i] = i;
	int result = 0,ssum;
	do{
		fill(tag,tag + 2 * n + 1,0);
		rep(_i,1,n){
			int i = _p[_i]; 
			rep(k,a[i],b[i]) tag[k] = 0;
			tag[a[i]] = tag[b[i]] = 1;
		}
		ssum = 0;
		rep(i,1,2 * n) ssum += tag[i];
		chkmax(result,ssum);
	}while(next_permutation(_p + 1,_p + n + 1));
//	printf("result=%d\n",result);
	return result;
}

void solve(int testid){
	scanf("%d",&n);	
	rep(i,1,n){
		G[0][i].reset();
		G[1][i].reset();
		ans[0][i] = ans[1][i] = 0;
	}
	rep(i,1,n) scanf("%d%d",&a[i],&b[i]);
	rep(i,1,n){
		rep(j,1,n){
			if(a[j] < a[i] && a[i] < b[j] && b[j] < b[i]){
				G[0][i].set(j);
//				printf("%d->%d\n",i,j);
			}
		}
	}

	int answer = 2 * n;
	while(1){
		bfs();
		if(!p) break;
		ans[1][p] = 1;
		p += n;
		int op = 1;
		while(prv[p]){
			if(op){
				G[1][p - n].set(prv[p]);
				G[0][prv[p]].reset(p - n);
			}else{
				G[0][p].set(prv[p] - n);
				G[1][prv[p] - n].reset(p);
			}
//			printf("op=%d p=%d\n",op,p);
			p = prv[p];
			op ^= 1;
		}
		assert(!ans[0][p]);
		ans[0][p] = 1;
		answer--;
//		printf("op=%d p=%d fin\n",op,p);
	}
/*	int answer2 = brute();
	if(answer != answer2){
		cerr << "output=" << answer << " answer=" << answer2 << endl;
		cerr << n << endl;
		rep(i,1,n) cerr << a[i] << " " << b[i] << endl;
	}
	assert(answer == answer2);*/
	printf("%d\n",answer);	
}

int main(){	
//	freopen("test.in","r",stdin);
//	scanf("%d",&T);
	T = 1;
	rep(i,1,T) solve(i);
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 5948kb

input:

5
2 3
6 7
1 9
5 10
4 8

output:

9

result:

ok 1 number(s): "9"

Test #2:

score: 0
Accepted
time: 1157ms
memory: 10108kb

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:

8134

result:

ok 1 number(s): "8134"

Test #3:

score: 0
Accepted
time: 1895ms
memory: 10372kb

input:

5000
3171 3172
4062 4064
4647 4651
3670 3673
7112 7114
9714 9717
3781 3789
8422 8426
457 460
5450 5454
7113 7122
6313 6320
9969 9973
828 832
6878 6892
4476 4483
892 903
251 259
6304 6315
130 134
9206 9215
2679 2686
9090 9091
8222 8228
9374 9375
2985 2989
3397 3401
4916 4918
6819 6821
883 889
2516 25...

output:

7047

result:

ok 1 number(s): "7047"

Test #4:

score: 0
Accepted
time: 2565ms
memory: 10112kb

input:

5000
7269 7286
1979 1990
4225 4241
7866 7872
2052 2067
1508 1514
2366 2370
3488 3493
8979 8987
302 306
6730 6732
7704 7705
5528 5544
7420 7425
4705 4712
593 601
6662 6668
5228 5257
2008 2013
548 562
7949 7950
1017 1020
1025 1028
6 11
4722 4736
9945 9950
8368 8379
6781 6787
4558 4566
400 404
858 864
...

output:

6191

result:

ok 1 number(s): "6191"

Test #5:

score: -100
Time Limit Exceeded

input:

5000
3005 3008
7811 7821
2832 2840
9812 9818
3947 3952
3629 3665
7443 7455
1473 1478
1467 1494
5499 5508
229 232
9477 9498
9500 9514
4769 4775
9488 9503
1514 1520
5101 5112
2455 2456
3558 3610
9072 9188
659 666
2286 2301
9735 9782
5959 5984
5823 5844
1827 1835
3658 3681
3494 3503
1016 1018
3418 3420...

output:


result: