题意:给定 n 个数,让你修改最少的数,使得这是一个不下降序列。
析:和3670一思路,就是一个LIS,也可以直接暴力,因为只有两个数,所以可以枚举在哪分界,左边是1,右边是2,更新答案。
代码如下:
#include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> using namespace std ; typedef long long LL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f3f; const double eps = 1e-8; const int maxn = 3e4 + 5; const int dr[] = {0, 0, -1, 1}; const int dc[] = {-1, 1, 0, 0}; int m, n; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } int a[maxn], b[maxn]; int solve(){ fill(b, b+n, INF); for(int i = 0; i < n; ++i) *upper_bound(b, b+n, a[i]) = a[i]; return lower_bound(b, b+n, INF) - b; } int main(){ while(scanf("%d", &n) == 1){ for(int i = 0; i < n; ++i) scanf("%d", &a[i]); int ans = n - solve(); printf("%d ", ans); } return 0; }
#include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> using namespace std ; typedef long long LL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f3f; const double eps = 1e-8; const int maxn = 30000 + 5; const int dr[] = {0, 0, -1, 1}; const int dc[] = {-1, 1, 0, 0}; int m, n; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } int a[maxn]; int main(){ while(scanf("%d", &n) == 1){ int cnt1 = 0, cnt2 = 0; for(int i = 0; i < n; ++i){ scanf("%d", &a[i]); if(a[i] == 1) ++cnt1; else ++cnt2; } int ans = INF; int cnt11 = 0, cnt22 = 0; for(int i = 0; i < n; ++i){ ans = min(ans, cnt22 + cnt1-cnt11); if(a[i] == 1) ++cnt11; else ++cnt22; } ans = min(ans, cnt22 + cnt1-cnt11); ans = min(ans, cnt1); ans = min(ans, cnt2); printf("%d ", ans); } return 0; }