Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <map>
- #include <array>
- #include <set>
- using namespace std;
- typedef long long ll;
- int v[205];
- int n;
- int dp[205][205][205];
- int rek(int at, int dec, int inc) {
- if(at > n) {
- return 0;
- }
- if(dp[at][dec][inc] != -1) {
- return dp[at][dec][inc];
- }
- int result = 2e9;
- if((dec == 0 or v[at] < v[dec]) and (inc == 0 or v[at] > v[inc])) {
- result = min(result, rek(at + 1, at, inc));
- result = min(result, rek(at + 1, dec, at));
- result = min(result, rek(at + 1, dec, inc) + 1);
- }
- else if(dec == 0 or v[at] < v[dec]) {
- result = min(result, rek(at + 1, at, inc));
- result = min(result, rek(at + 1, dec, inc) + 1);
- }
- else if(inc == 0 or v[at] > v[inc]) {
- result = min(result, rek(at + 1, dec, at));
- result = min(result, rek(at + 1, dec, inc) + 1);
- }
- else {
- result = min(result, rek(at + 1, dec, inc) + 1);
- }
- return dp[at][dec][inc] = result;
- }
- int main()
- {
- while(true) {
- cin >> n;
- if(n == -1) {
- return 0;
- }
- for(int i = 1; i <= n; i++) {
- cin >> v[i];
- }
- for(int i = 0; i < 205; i++) {
- for(int j = 0; j < 205; j++) {
- for(int k = 0; k < 205; k++) {
- dp[i][j][k] = -1;
- }
- }
- }
- cout << rek(1, 0, 0) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement