12592022-03-29 11:42:12vandrasNövekvő Ödön és a Másoló Varázslócpp14Time limit exceeded 15/100697ms82668 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({-1, 0});
	st.insert({a[0], 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
1Accepted2ms1824 KiB
2Time limit exceeded606ms3016 KiB
subtask25/5
3Accepted90ms7672 KiB
4Accepted93ms9604 KiB
5Accepted93ms11548 KiB
subtask30/10
6Accepted1ms8692 KiB
7Wrong answer1ms8692 KiB
8Accepted1ms8696 KiB
subtask40/15
9Accepted4ms8720 KiB
10Wrong answer2ms8728 KiB
11Accepted4ms8744 KiB
12Wrong answer2ms8752 KiB
subtask55/5
13Accepted300ms9432 KiB
14Accepted59ms9556 KiB
15Accepted307ms9616 KiB
subtask65/5
16Accepted223ms9748 KiB
17Accepted518ms9924 KiB
18Accepted503ms10036 KiB
19Accepted266ms10004 KiB
subtask70/10
20Time limit exceeded694ms8892 KiB
21Time limit exceeded697ms9008 KiB
22Accepted428ms10360 KiB
23Accepted300ms10152 KiB
24Wrong answer451ms10480 KiB
subtask80/25
25Accepted119ms24844 KiB
26Accepted119ms26744 KiB
27Accepted122ms28672 KiB
28Accepted1ms15680 KiB
29Time limit exceeded694ms18520 KiB
30Time limit exceeded674ms20596 KiB
31Time limit exceeded694ms22312 KiB
32Time limit exceeded689ms24416 KiB
33Time limit exceeded643ms26416 KiB
34Time limit exceeded623ms28156 KiB
35Time limit exceeded611ms19868 KiB
36Time limit exceeded654ms21808 KiB
37Time limit exceeded620ms23676 KiB
38Time limit exceeded623ms25640 KiB
39Time limit exceeded606ms27632 KiB
40Time limit exceeded615ms29604 KiB
41Time limit exceeded630ms31536 KiB
42Time limit exceeded674ms32192 KiB
43Time limit exceeded694ms34580 KiB
44Time limit exceeded690ms36464 KiB
45Time limit exceeded690ms38480 KiB
subtask90/25
46Time limit exceeded688ms42944 KiB
47Time limit exceeded681ms38660 KiB
48Time limit exceeded694ms42584 KiB
49Time limit exceeded675ms46584 KiB
50Time limit exceeded685ms50348 KiB
51Time limit exceeded693ms54720 KiB
52Time limit exceeded689ms57152 KiB
53Time limit exceeded697ms61120 KiB
54Time limit exceeded693ms65056 KiB
55Wrong answer275ms82668 KiB