zoukankan      html  css  js  c++  java
  • POJ 3671 Dining Cows (DP,LIS, 暴力)

    题意:给定 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;
    }
    
  • 相关阅读:
    无编译/无服务器,实现浏览器的 CommonJS 模块化
    程序员如何在工作中自我增值
    软件架构被高估,清晰简单的设计被低估
    为什么会产生微服务架构?
    版本管理Git和SVN的介绍及其优缺点
    13.递归第一次
    12.二叉树的序遍历
    12.二叉树的序遍历
    10.十进制转m进制
    10.十进制转m进制
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5754365.html
Copyright © 2011-2022 走看看