12602022-03-29 11:43:16vandrasNövekvő Ödön és a Másoló Varázslócpp14Time limit exceeded 40/100697ms60536 KiB
#include <bits/stdc++.h>
using namespace std;

/*<DEBUG>*/
#define tem template <typename 
#define can_shift(_X_, ...) enable_if_t<sizeof test<_X_>(0) __VA_ARGS__ 8, debug&> operator<<(T i)
#define _op debug& operator<<
tem C > auto test(C *x) -> decltype(cerr << *x, 0LL);
tem C > char test(...);
tem C > struct itr{C begin, end; };
tem C > itr<C> get_range(C b, C e) { return itr<C>{b, e}; }
struct debug{
#ifdef _LOCAL
	~debug(){ cerr << endl; }
	tem T > can_shift(T, ==){ cerr << boolalpha << i; return *this; }
	tem T> can_shift(T, !=){ return *this << get_range(begin(i), end(i)); }
	tem T, typename U > _op (pair<T, U> i){ 
		return *this << "< " << i.first << " , " << i.second << " >"; }
	tem T> _op (itr<T> i){
		*this <<  "{ ";
		for(auto it = i.begin; it != i.end; it++){
			*this << " , " + (it==i.begin?2:0) << *it;
		}
		return *this << " }";
	}
#else
tem T> _op (const T&) { return *this; }
#endif 
};

tem T>
string _ARR_(T* arr, int sz){
	string ret = "{ " + to_string(arr[0]); 
	for(int i = 1; i < sz; i++) ret += " , " +  to_string(arr[i]);
	ret += " }"; return ret;
}

#define exp(...) " [ " << #__VA_ARGS__ << " : " << (__VA_ARGS__) << " ]"
/*</DEBUG>*/

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef unsigned int uint;
typedef pair<int, int> pii;
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define pb push_back
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define TC int __TC__; cin >> __TC__; while(__TC__--)
#define ar array

const int INF = 1e9 + 7, N = 200005, M = 5003;

int a[N], b[N], n, m, dp[N];
vector<int> vals;

int get(int v){
	return lower_bound(vals.begin(), vals.end(), v) - vals.begin();
}

int cnt_between(int l, int r){
//	debug() << exp(lower_bound(b+1, b+m+1, l) - b) exp(l);
	return lower_bound(b+1, b+m+1, r) - lower_bound(b+1, b+m+1, l);
}

int main()
{
	FAST;
	cin >> n >> m;
	vals.resize(n+m);
	for(int i = 1; i <= n; ++i){
		cin >> a[i];
		vals[i-1] = a[i];
	}
	for(int i = 1; i <= m; ++i){
		cin >> b[i];
		vals[n+i-1] = b[i];
	}

	sort(vals.begin(), vals.end());

	a[0] = -1;
	for(int i = 1; i <= n; ++i) a[i] = get(a[i]);
	for(int i = 1; i <= m; ++i) b[i] = get(b[i]);

	sort(b+1, b+m+1);

	int mxN = vals.size();

	//dp[i] : best solution up to ith element (ith is left unchanged)

	dp[0] = 0;
	dp[1] = 0;


	set<ar<int,2>> st;
	a[n+1] = INF;

	st.insert({a[0], 0});
	st.insert({a[1], 1});

	for(int i = 2; i <= n+1; ++i){
		dp[i] = INF;
		for(auto [v, j] : st){
			if(a[j] > a[i]) break;
			if(a[j] < a[i] && cnt_between(a[j], a[i]) >= i-j-1) dp[i] = min(dp[i], dp[j] + i-j-1); 
		}
		st.insert({a[i], i});
	}

	cout << min(dp[n], dp[n+1]) << '\n';

	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1844 KiB
2Time limit exceeded691ms3016 KiB
subtask25/5
3Accepted96ms7696 KiB
4Accepted101ms9636 KiB
5Accepted96ms11672 KiB
subtask310/10
6Accepted2ms8732 KiB
7Accepted1ms8720 KiB
8Accepted2ms8748 KiB
subtask415/15
9Accepted4ms8784 KiB
10Accepted4ms8804 KiB
11Accepted3ms8760 KiB
12Accepted4ms8824 KiB
subtask55/5
13Accepted351ms9548 KiB
14Accepted381ms9552 KiB
15Accepted351ms8156 KiB
subtask65/5
16Accepted331ms8248 KiB
17Accepted509ms8348 KiB
18Accepted495ms8456 KiB
19Accepted312ms8528 KiB
subtask70/10
20Time limit exceeded620ms7352 KiB
21Time limit exceeded607ms7392 KiB
22Time limit exceeded638ms8820 KiB
23Accepted354ms8568 KiB
24Time limit exceeded683ms7736 KiB
subtask80/25
25Accepted128ms23272 KiB
26Accepted118ms25148 KiB
27Accepted118ms27156 KiB
28Accepted1ms14100 KiB
29Time limit exceeded694ms16876 KiB
30Time limit exceeded694ms18896 KiB
31Time limit exceeded677ms20756 KiB
32Time limit exceeded676ms22740 KiB
33Time limit exceeded694ms24704 KiB
34Time limit exceeded691ms26612 KiB
35Time limit exceeded665ms28656 KiB
36Time limit exceeded693ms30416 KiB
37Time limit exceeded694ms32432 KiB
38Time limit exceeded693ms34328 KiB
39Time limit exceeded694ms36300 KiB
40Time limit exceeded685ms38424 KiB
41Time limit exceeded676ms37728 KiB
42Time limit exceeded693ms32464 KiB
43Time limit exceeded694ms34976 KiB
44Time limit exceeded697ms37060 KiB
45Time limit exceeded694ms38804 KiB
subtask90/25
46Time limit exceeded694ms44208 KiB
47Time limit exceeded691ms48140 KiB
48Time limit exceeded670ms52012 KiB
49Time limit exceeded677ms52420 KiB
50Time limit exceeded694ms48344 KiB
51Time limit exceeded661ms52332 KiB
52Time limit exceeded670ms55256 KiB
53Time limit exceeded693ms59000 KiB
54Time limit exceeded642ms60536 KiB
55Time limit exceeded609ms41544 KiB