104172024-04-01 23:30:16111Kutyavetélkedőcpp17Accepted 100/10096ms24116 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,K;
	cin>>N>>K;
	vector<int>v(N+3);
	for(int i=1;i<=N;i++){
		cin>>v[i];
	}
	vector<vector<int>>g(K+2);
	int M;
	cin>>M;
	for(int i=0;i<M;i++){
		int a,b;
		cin>>a>>b;
		g[a].push_back(b);
	}
	for(int i=1;i<=K;i++){
		g[0].push_back(i);
		g[i].push_back(0);
		sort(g[i].begin(),g[i].end());
	}
	vector<int>dp(N+3,-N-2);
	dp[N+1]=-1;
	dp[N+2]=-1;
	for(int i=N;i>=0;i--){
		if(binary_search(g[v[i]].begin(),g[v[i]].end(),v[i+1])){
			dp[i]=max(dp[i],dp[i+1]+1);
		}
		if(binary_search(g[v[i]].begin(),g[v[i]].end(),v[i+2])){
			dp[i]=max(dp[i],dp[i+2]+1);
		}
	}
	cout<<max(dp[0],0ll)<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1824 KiB
2Accepted3ms2056 KiB
subtask215/15
3Accepted3ms2256 KiB
4Accepted3ms2480 KiB
5Accepted3ms2680 KiB
6Accepted3ms2744 KiB
7Accepted23ms9016 KiB
8Accepted23ms8972 KiB
9Accepted23ms8972 KiB
subtask319/19
10Accepted3ms2956 KiB
11Accepted3ms2984 KiB
12Accepted3ms3060 KiB
13Accepted3ms3060 KiB
14Accepted3ms3052 KiB
15Accepted3ms3188 KiB
16Accepted3ms3264 KiB
subtask434/34
17Accepted3ms3696 KiB
18Accepted4ms3744 KiB
19Accepted4ms4188 KiB
20Accepted4ms4172 KiB
21Accepted6ms4272 KiB
22Accepted6ms4280 KiB
subtask532/32
23Accepted37ms9364 KiB
24Accepted43ms10620 KiB
25Accepted46ms12052 KiB
26Accepted46ms13080 KiB
27Accepted46ms14004 KiB
28Accepted75ms13728 KiB
29Accepted96ms24116 KiB
30Accepted93ms24076 KiB
31Accepted74ms17384 KiB
32Accepted52ms12332 KiB
33Accepted89ms23888 KiB
34Accepted85ms23840 KiB