답안 #824192

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
824192 2023-08-13T17:39:36 Z vanic Exam (eJOI20_exam) C++14
100 / 100
490 ms 99520 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <cstring>

using namespace std;

const int maxn=1e5+5;

int n;
int a[maxn], b[maxn];

void solve1(){
	int val=b[0];
	bool p=0;
	int prosl=-1;
	int br=0;
	for(int i=0; i<n; i++){
		if(a[i]==val && !p){
			br+=i-prosl;
			p=1;
		}
		else if(a[i]>val){
			prosl=i;
			p=0;
		}
		else if(p){
			br++;
		}
	}
	cout << br << '\n';
}

int l[maxn], r[maxn];
map < int, int > pos;
vector < int > lis;

void dodaj(int x){
	int ind=upper_bound(lis.begin(), lis.end(), x)-lis.begin();
	if(ind==(int)lis.size()){
		lis.push_back(x);
	}
	else{
		lis[ind]=x;
	}
}

void solve2(){
	stack < pair < int, int > > s;
	s.push({2e9, -1});
	for(int i=0; i<n; i++){
		pos[a[i]]=i;
		while(s.top().first<a[i]){
			s.pop();
		}
		l[i]=s.top().second+1;
		s.push({a[i], i});
	}
	while(!s.empty()){
		s.pop();
	}
	s.push({2e9, n});
	for(int i=n-1; i>-1; i--){
		while(s.top().first<a[i]){
			s.pop();
		}
		r[i]=s.top().second-1;
		s.push({a[i], i});
	}
	for(int i=0; i<n; i++){
		if(pos.find(b[i])!=pos.end() && i>=l[pos[b[i]]] && i<=r[pos[b[i]]]){
			dodaj(pos[b[i]]);
		}
	}
	cout << lis.size() << '\n';
}


int dp[5005][5005];

int rek(int x, int y){
	if(x==-1 || y==-1){
		return 0;
	}
	if(dp[x][y]!=-1){
		return dp[x][y];
	}
	if(a[x]==b[y] && l[x]<=y && r[x]>=y){
		return dp[x][y]=max(rek(x-1, y), rek(x, y-1)+1);
	}
	else{
		return dp[x][y]=max(rek(x-1, y), rek(x, y-1));
	}
}

void solve3(){
	memset(dp, -1, sizeof(dp));
	stack < pair < int, int > > s;
	s.push({2e9, -1});
	for(int i=0; i<n; i++){
		while(s.top().first<a[i]){
			s.pop();
		}
		l[i]=s.top().second+1;
		s.push({a[i], i});
	}
	while(!s.empty()){
		s.pop();
	}
	s.push({2e9, n});
	for(int i=n-1; i>-1; i--){
		while(s.top().first<a[i]){
			s.pop();
		}
		r[i]=s.top().second-1;
		s.push({a[i], i});
	}
	cout << rek(n-1, n-1) << '\n';
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	set < int > s;
	for(int i=0; i<n; i++){
		cin >> a[i];
		s.insert(a[i]);
	}
	bool p=1;
	for(int i=0; i<n; i++){
		cin >> b[i];
		if(i && b[i]!=b[i-1]){
			p=0;
		}
	}
	if(p){
		solve1();
	}
	else if((int)s.size()==n){
		solve2();
	}
	else{
		solve3();
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 32 ms 98360 KB Output is correct
3 Correct 32 ms 98340 KB Output is correct
4 Correct 32 ms 98312 KB Output is correct
5 Correct 32 ms 98316 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 4 ms 848 KB Output is correct
3 Correct 24 ms 6272 KB Output is correct
4 Correct 12 ms 1492 KB Output is correct
5 Correct 37 ms 7780 KB Output is correct
6 Correct 9 ms 1492 KB Output is correct
7 Correct 11 ms 1620 KB Output is correct
8 Correct 34 ms 5968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Correct 3 ms 852 KB Output is correct
5 Correct 3 ms 980 KB Output is correct
6 Correct 4 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 992 KB Output is correct
2 Correct 35 ms 5456 KB Output is correct
3 Correct 93 ms 12428 KB Output is correct
4 Correct 112 ms 13184 KB Output is correct
5 Correct 86 ms 14016 KB Output is correct
6 Correct 81 ms 12852 KB Output is correct
7 Correct 88 ms 13632 KB Output is correct
8 Correct 94 ms 12436 KB Output is correct
9 Correct 103 ms 12756 KB Output is correct
10 Correct 69 ms 13132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 32 ms 98360 KB Output is correct
3 Correct 32 ms 98340 KB Output is correct
4 Correct 32 ms 98312 KB Output is correct
5 Correct 32 ms 98316 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 31 ms 98316 KB Output is correct
8 Correct 32 ms 98372 KB Output is correct
9 Correct 31 ms 98380 KB Output is correct
10 Correct 31 ms 98368 KB Output is correct
11 Correct 31 ms 98384 KB Output is correct
12 Correct 31 ms 98368 KB Output is correct
13 Correct 33 ms 98392 KB Output is correct
14 Correct 31 ms 98284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 32 ms 98360 KB Output is correct
3 Correct 32 ms 98340 KB Output is correct
4 Correct 32 ms 98312 KB Output is correct
5 Correct 32 ms 98316 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 3 ms 852 KB Output is correct
11 Correct 3 ms 980 KB Output is correct
12 Correct 4 ms 980 KB Output is correct
13 Correct 31 ms 98316 KB Output is correct
14 Correct 32 ms 98372 KB Output is correct
15 Correct 31 ms 98380 KB Output is correct
16 Correct 31 ms 98368 KB Output is correct
17 Correct 31 ms 98384 KB Output is correct
18 Correct 31 ms 98368 KB Output is correct
19 Correct 33 ms 98392 KB Output is correct
20 Correct 31 ms 98284 KB Output is correct
21 Correct 32 ms 98360 KB Output is correct
22 Correct 40 ms 98504 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 469 ms 99332 KB Output is correct
25 Correct 442 ms 99332 KB Output is correct
26 Correct 414 ms 99284 KB Output is correct
27 Correct 490 ms 99256 KB Output is correct
28 Correct 450 ms 99484 KB Output is correct
29 Correct 430 ms 99404 KB Output is correct
30 Correct 422 ms 99520 KB Output is correct
31 Correct 415 ms 99372 KB Output is correct
32 Correct 450 ms 99464 KB Output is correct