답안 #463397

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
463397 2021-08-11T05:41:08 Z amunduzbaev Exam (eJOI20_exam) C++14
55 / 100
163 ms 1896 KB
#include "bits/stdc++.h"
using namespace std;

#define int long long

void solve1(int n, vector<int>& a, vector<int>& b){
	vector<int> dp(n);
	if(n <= 205){
		vector<array<int, 3>> tmp;
		for(int i=0;i<n;i++){
			tmp.push_back({a[i], 0, i});
			tmp.push_back({b[i], 1, i});
		}
		
		sort(tmp.begin(), tmp.end());
		for(int last=0, i=0;i<(int)tmp.size();){
			int j = i;
			while(j < (int)tmp.size() && tmp[j][0] == tmp[i][0]) j++;
			while(i < j){
				if(tmp[i][1]){
					b[tmp[i][2]] = last;
				} else {
					a[tmp[i][2]] = last;
				} i++; 
			} last++;
		}
		
		//~ for(int i=0;i<n;i++) cout<<a[i]<<" ";
		//~ cout<<endl;
		//~ for(int i=0;i<n;i++) cout<<b[i]<<" ";
		//~ cout<<endl;
		
		vector<vector<int>> cnt(2 * n, vector<int>(n));
		for(int i=0;i<n;i++){
			assert(b[i] < 2 * n);
			cnt[b[i]][i]++;
			if(i + 1 < n){
				for(int j=0;j<2*n;j++){
					cnt[j][i+1] = cnt[j][i];
				}
			}
		}

		int res = 0;
		for(int i=0;i<n;i++){
			int s = i;
			while(s + 1 < n && a[s + 1] <= a[i]) s++;
			for(int r=s;r>=0;r--){
				if(a[r] > a[i]) break;
				for(int l=r;l>=0;l--){
					if(a[l] > a[i]) break;
					dp[r] = max(dp[r], cnt[a[i]][r] - (l ? cnt[a[i]][l-1] - dp[l-1] : 0));
				}
			}
		}
		
		//~ int res = 0;
		//~ for(int i=0;i<n;i++){
			//~ int r = i, l = i;
			//~ while(r + 1 < n && a[r + 1] <= a[i]) r++;
			//~ while(l > 0 && a[l - 1] <= a[l]) l--;
			//~ vector<int> cnt(n);
			//~ for(int j=0;j<n;j++){
				 //~ if(b[j] == a[i]) cnt[j]++;
				 //~ if(j + 1 < n) cnt[j+1] = cnt[j];
			//~ }

			//~ vector<int> pref(n);
			//~ for(int i=0;i<n;i++){
				//~ pref[i] = max(pref[i], dp[i] - cnt[i]);
				//~ if(i + 1 < n) pref[i+1] = pref[i];
			//~ }

			//~ for(int j=l;j<=r;j++){
				//~ dp[j] = max(dp[j], cnt[j] + (j ? pref[j - 1] : 0));
			//~ }
		//~ }
		
		//~ for(int i=0;i<n;i++) cout<<dp[i]<<" ";
		//~ cout<<"\n";
		
		for(int i=0;i<n;i++) res = max(res, dp[i]);
		
		cout<<res<<"\n";
	} else {
		int res = 0;
		for(int i=0;i<n;i++){
			int r = i, l = i;
			while(r + 1 < n && a[r + 1] <= a[i]) r++;
			while(l > 0 && a[l - 1] <= a[l]) l--;
			vector<int> cnt(n);
			for(int j=0;j<n;j++){
				 if(b[j] == a[i]) cnt[j]++;
				 if(j + 1 < n) cnt[j+1] = cnt[j];
			}

			vector<int> pref(n);
			for(int i=0;i<n;i++){
				pref[i] = max(pref[i], dp[i] - cnt[i]);
				if(i + 1 < n) pref[i+1] = pref[i];
			}

			for(int j=l;j<=r;j++){
				dp[j] = max(dp[j], cnt[j] + (j ? pref[j - 1] : 0));
			}
		}
		
		//~ for(int i=0;i<n;i++) cout<<dp[i]<<" ";
		//~ cout<<"\n";
		
		for(int i=0;i<n;i++) res = max(res, dp[i]);
		
		cout<<res<<"\n";
	}
}

void solve2(int n, vector<int>& a, vector<int>& b){
	int last = 0, cnt = 0, res = 0;
	for(int i=0;i<n;i++){
		if(a[i] > b[0]){
			if(cnt){
				res += (i - last);
			}
			
			cnt = 0, last = i + 1;
		} else {
			cnt |= (a[i] == b[i]);
		}
	} if(cnt){
		res += (n - last);
	}
	
	cout<<res<<"\n";
}

void solve(){
	int n; cin>>n;
	vector<int> a(n), b(n);
	for(auto& x : a) cin>>x;
	for(auto& x : b) cin>>x;
	
	int mx = 0, mn = 1e9;
	for(auto x : b) mx = max(mx, x), mn = min(mn, x);
	if(mx == mn){
		solve2(n, a, b);
		return;
	}
	
	if(n <= 5005){
		solve1(n, a, b);
	} else {
		solve2(n, a, b);
	}
}

/*

3
1 2 3
2 2 2

*/

signed main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	int t = 1;
	//~ cin>>t;
	while(t--) solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 5 ms 588 KB Output is correct
3 Correct 20 ms 1740 KB Output is correct
4 Correct 15 ms 1888 KB Output is correct
5 Correct 29 ms 1876 KB Output is correct
6 Correct 16 ms 1868 KB Output is correct
7 Correct 17 ms 1896 KB Output is correct
8 Correct 30 ms 1880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 27 ms 332 KB Output is correct
4 Correct 142 ms 484 KB Output is correct
5 Correct 163 ms 484 KB Output is correct
6 Correct 141 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 118 ms 488 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 244 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 2 ms 844 KB Output is correct
11 Correct 2 ms 844 KB Output is correct
12 Correct 1 ms 844 KB Output is correct
13 Correct 1 ms 972 KB Output is correct
14 Correct 3 ms 844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 27 ms 332 KB Output is correct
10 Correct 142 ms 484 KB Output is correct
11 Correct 163 ms 484 KB Output is correct
12 Correct 141 ms 492 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 244 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 2 ms 844 KB Output is correct
17 Correct 2 ms 844 KB Output is correct
18 Correct 1 ms 844 KB Output is correct
19 Correct 1 ms 972 KB Output is correct
20 Correct 3 ms 844 KB Output is correct
21 Correct 1 ms 972 KB Output is correct
22 Incorrect 5 ms 332 KB Output isn't correct
23 Halted 0 ms 0 KB -