码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3671 Dining Cows (DP,LIS, 暴力)

时间:2016-08-09 20:39:00      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

题意:给定 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\n", 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\n", ans);
    }
    return 0;
}

 

POJ 3671 Dining Cows (DP,LIS, 暴力)

标签:

原文地址:http://www.cnblogs.com/dwtfukgv/p/5754365.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!