2732021-06-22 15:23:51mraronNövekvő Ödön és a Másoló Varázslócpp14Accepted 100/100554ms95740 KiB
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cassert>
#include<cassert>
#include<unordered_map>
#include<unordered_set>
#include<functional>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<sstream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<numeric>
#include<random>
#include<chrono>
#include<bitset>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define xx first
#define yy second
#define sz(x) (int)(x).size()
#define gc getchar
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mp make_pair

#ifndef ONLINE_JUDGE
#  define LOG(x) (cerr << #x << " = " << (x) << endl)
#else
#  define LOG(x) ((void)0)
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const double PI=acos(-1);
const ll INF = 1LL<<62;
const ll MINF = -(1LL<<62);

template<typename T> T getint() {
	T val=0;
	char c;
	
	bool neg=false;
	while((c=gc()) && !(c>='0' && c<='9')) {
		neg|=c=='-';
	}

	do {
		val=(val*10)+c-'0';
	} while((c=gc()) && (c>='0' && c<='9'));

	return val*(neg?-1:1);
}

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int>(0, n-1)(rng)
vector<int> a,b,pos;
vector<int> dp;

vector<int> solve(int l, int r) {
	if(l==r) {
		if(pos[l]>=l) dp[l]=max(dp[l], 1);
		return {l};
	}
	
	int mid=(l+r)/2;
	vector<int> lst=solve(l, mid);
	lst.reserve(r-l+1);
	
	if(mid+1<=r) {
		for(int i=mid+1;i<=r;++i) lst.pb(i);
		
		sort(lst.begin()+mid+1-l, lst.end(), [&](int x, int y) -> bool {
			return a[x]<a[y];
		});
		
		
	}

	inplace_merge(lst.begin(),lst.begin()+mid+1-l,lst.end(), [&](int x, int y) -> bool {
		return a[x]<a[y];
	});
	
	set<pair<int,int>> st; //{i-pos[i], dp[i]}
	for(auto i:lst) {
		if(i<=mid) {
			st.insert(mp(i-pos[i], dp[i]));
			auto it=st.lower_bound(mp(i-pos[i], dp[i]));
			while(it!=st.begin() && prev(it)->yy<=it->yy) st.erase(prev(it));
			if(next(it)!=st.end() && next(it)->yy>=it->yy) st.erase(it);
		}else {
			auto it=st.lower_bound(mp(i-pos[i]-1, 0));
			if(it!=st.end()) {
				dp[i]=max(dp[i], it->yy+1);
			}
		}
	}
	
	solve(mid+1, r);
	return lst;
}

int main() {
	IO;
	int n,m;
	cin>>n>>m;
	a=vector<int>(n);
	b=vector<int>(m);
	for(int i=0;i<n;++i) {
		cin>>a[i];
	}
	
	for(int i=0;i<m;++i) {
		cin>>b[i];
	}
	
	n++;
	a.pb(1e9+1);
	
	sort(all(b));
	
	pos=vector<int>(n);
	for(int i=0;i<n;++i) {
		pos[i]=lower_bound(all(b),a[i])-b.begin();
	}
	
	dp=vector<int>(n,-1e9);

	solve(0,n-1);
	cout<<n-dp[n-1]<<"\n";
		
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted2ms1756 KiB
2Accepted107ms5188 KiB
subtask25/5
3Accepted39ms6096 KiB
4Accepted45ms8024 KiB
5Accepted39ms9968 KiB
subtask310/10
6Accepted1ms8656 KiB
7Accepted1ms8664 KiB
8Accepted1ms8664 KiB
subtask415/15
9Accepted1ms8688 KiB
10Accepted2ms8692 KiB
11Accepted1ms8704 KiB
12Accepted2ms8716 KiB
subtask55/5
13Accepted7ms9100 KiB
14Accepted8ms9200 KiB
15Accepted10ms9304 KiB
subtask65/5
16Accepted7ms9400 KiB
17Accepted7ms9504 KiB
18Accepted8ms9608 KiB
19Accepted7ms9572 KiB
subtask710/10
20Accepted7ms9780 KiB
21Accepted12ms9880 KiB
22Accepted9ms9996 KiB
23Accepted4ms9912 KiB
24Accepted8ms10012 KiB
subtask825/25
25Accepted119ms16660 KiB
26Accepted145ms18584 KiB
27Accepted125ms20528 KiB
28Accepted1ms15656 KiB
29Accepted237ms22796 KiB
30Accepted153ms24404 KiB
31Accepted159ms26300 KiB
32Accepted173ms28216 KiB
33Accepted200ms30168 KiB
34Accepted188ms32096 KiB
35Accepted219ms34032 KiB
36Accepted162ms35984 KiB
37Accepted172ms37916 KiB
38Accepted199ms39856 KiB
39Accepted246ms42168 KiB
40Accepted163ms43676 KiB
41Accepted204ms45676 KiB
42Accepted93ms45220 KiB
43Accepted150ms48772 KiB
44Accepted158ms50712 KiB
45Accepted187ms52640 KiB
subtask925/25
46Accepted400ms61780 KiB
47Accepted407ms65664 KiB
48Accepted342ms69524 KiB
49Accepted504ms73520 KiB
50Accepted522ms77832 KiB
51Accepted554ms81632 KiB
52Accepted328ms83712 KiB
53Accepted374ms87512 KiB
54Accepted405ms91864 KiB
55Accepted314ms95740 KiB