poj 1836
最长递增子序列的变种,计算两次即可
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <cstdlib> 5 #include <cmath> 6 #include <map> 7 #include <algorithm> 8 #include <list> 9 #include <ctime> 10 #include <set> 11 #include <cstring> 12 #include <queue> 13 #include <cstdio> 14 #define CLR(arr, what) memset(arr, what, sizeof(arr)) 15 const int MAX = 100005; 16 const int maxc = 1500; 17 using namespace std; 18 void get_inc(vector<double>& data, vector<int>& res) { 19 int sz = data.size(); 20 int j; 21 vector<double> cur(maxc, -1); 22 for (int i = 0; i < sz; i++) { 23 for (j = 0; j < i + 1; j++) { 24 if (cur[j] == -1 || (cur[j] >= data[i])) { 25 cur[j] = data[i]; 26 res[i] = j; 27 break; 28 } 29 } 30 } 31 } 32 int main() { 33 int n; 34 double t; 35 while (scanf("%d", &n) != EOF) { 36 vector<double> data; 37 for (int i = 0; i < n; i++) { 38 cin >> t; 39 data.push_back(t); 40 } 41 vector<int> incc(n, 0); 42 vector<int> decc(n, 0); 43 get_inc(data, incc); 44 reverse(data.begin(), data.end()); 45 get_inc(data, decc); 46 reverse(decc.begin(), decc.end()); 47 int res = 10000; 48 for (int i = 0; i < n; i++) { 49 int t = decc[i] + incc[i] + 1; 50 res = min(res, n - t); 51 for (int j = i + 1; j < n; j++) { 52 t = decc[j] + incc[i] + 2; 53 res = min(n - t, res); 54 } 55 } 56 cout << res << endl; 57 } 58 return 0; 59 }