100402024-03-25 18:38:07111Növekvő Ödön és a Másoló Varázslócpp17Accepted 100/100230ms21052 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

#define INF 999999

int N,M;
int F[1000000];

void add(int i,int x){
	for(i++;i<1000000;i+=i&-i){
		F[i]+=x;
	}
}

void add(int i,int j,int x){
	add(i,x);
	add(j+1,-x);
}

int get(int i){
	int x=0;
	for(i++;i;i-=i&-i){
		x+=F[i];
	}
	return x;
}

void sset(int i,int x){
	add(i,i,x-get(i));
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin>>N>>M;
	vector<int>v(N+1),w(M),u(N+1);
	for(int i=1;i<=N;i++){
		cin>>v[i];
	}
	for(int i=0;i<M;i++){
		cin>>w[i];
	}
	sort(w.begin(),w.end());
	for(int i=1;i<=N;i++){
		u[i]=upper_bound(w.begin(),w.end(),v[i])-w.begin();
	}
	int a=0,l=0,h=500000;
	add(0,h-1,INF);
	h++;
	for(int i=1;i<=N;i++){
		int aa=INF;
		int j=u[i]-1;
		if(j>=l){
			aa=min(aa,get(h+j));
		}
		h--;
		if(w[l]>v[i-1]&&a<INF){
			sset(h+l,get(h+l+1));
		}
		else{
			l++;
		}
		int ll=u[i-1],hh=M;
		while(ll<hh){
			int mm=(ll+hh)/2;
			if(get(h+mm)<=a){
				hh=mm;
			}
			else{
				ll=mm+1;
			}
		}
		add(h+l,h+u[i-1]-1,1);
		if(a+1<get(h+u[i-1])){
			sset(h+u[i-1],a+1);
		}
		add(h+ll,h+M-1,1);
		if(v[i]>v[i-1]){
			aa=min(aa,a);
		}
		a=aa;
	}
	for(int i=l;i<M;i++){
		a=min(a,get(h+i));
	}
	cout<<a<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2172 KiB
2Accepted52ms6160 KiB
subtask25/5
3Accepted54ms5788 KiB
4Accepted54ms5936 KiB
5Accepted54ms6160 KiB
subtask310/10
6Accepted3ms3396 KiB
7Accepted3ms3220 KiB
8Accepted3ms3092 KiB
subtask415/15
9Accepted3ms3364 KiB
10Accepted3ms3308 KiB
11Accepted3ms3308 KiB
12Accepted3ms3464 KiB
subtask55/5
13Accepted6ms4180 KiB
14Accepted6ms4048 KiB
15Accepted6ms4188 KiB
subtask65/5
16Accepted6ms4388 KiB
17Accepted4ms4424 KiB
18Accepted6ms4504 KiB
19Accepted4ms4388 KiB
subtask710/10
20Accepted7ms4420 KiB
21Accepted7ms4420 KiB
22Accepted7ms4552 KiB
23Accepted4ms4636 KiB
24Accepted7ms4996 KiB
subtask825/25
25Accepted94ms12592 KiB
26Accepted94ms12536 KiB
27Accepted94ms12536 KiB
28Accepted3ms4832 KiB
29Accepted109ms12632 KiB
30Accepted93ms11280 KiB
31Accepted94ms12176 KiB
32Accepted96ms12684 KiB
33Accepted100ms12652 KiB
34Accepted98ms12704 KiB
35Accepted101ms12652 KiB
36Accepted94ms12008 KiB
37Accepted96ms12632 KiB
38Accepted98ms12720 KiB
39Accepted109ms12964 KiB
40Accepted97ms13012 KiB
41Accepted100ms13016 KiB
42Accepted54ms9580 KiB
43Accepted94ms11848 KiB
44Accepted96ms12820 KiB
45Accepted97ms13000 KiB
subtask925/25
46Accepted196ms20520 KiB
47Accepted201ms20808 KiB
48Accepted194ms19248 KiB
49Accepted229ms21052 KiB
50Accepted229ms21024 KiB
51Accepted230ms20936 KiB
52Accepted165ms18504 KiB
53Accepted184ms19784 KiB
54Accepted201ms20824 KiB
55Accepted194ms20856 KiB