• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
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\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;
}

 

posted on 2016-08-09 19:08  dwtfukgv  阅读(229)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3