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;
}
时间: 2024-12-25 02:08:36

POJ 3671 Dining Cows (DP,LIS, 暴力)的相关文章

POJ 3671 Dining Cows (DP)

题目大意:给你一串只有1,2的数字,让你改变最少的次数,让这个序列变成非递减的. 思路:动态规划,判断分界点,开一个dp[30010][2]的数组,其中dp[i][j]表示把第i个数改成j最少要花多少次 那么状态转移方程就列出来了: 令a=1 j!=a[i] 0 j==a[i] 那么dp[i][1]=dp[i-1][1]+a; dp[i][2]=min(dp[i-1][1],dp[i-1][2])+a; #include <iostream> using namespace std; int

poj 1836 Alignment(dp,LIS)

链接:poj 1836 题意:士兵站成一行,求最少要多少的士兵出列, 使得每个士兵都能至少看到一个最边上的士兵 中间某个人能看到最边上的士兵的条件是: 该士兵的身高一定强大于他某一边(左边或右边)所有人的身高, 身高序列可以是: 1  2  3   4   5   4   3   2   1  或者 1   2   3   4   5   5   4   3   2   1 分析:要求最少出列数,就是留队士兵人数最大, 即左边的递增序列人数和右边的递减序列人数之和最大 因而可转化为求"最长升序子

POJ 2181 Jumping Cows DP

题意:n<=2e5件物品 按照1~n顺序选择物品,每件物品可以拿或者不拿,奇数次拿物品分数+a[i],偶数次拿分数-a[i] 问最大得分?显然每件物品有2种状态,第奇数次拿,第偶数次拿(不拿话 ans就不统计第i个)设dp[i][1,2] 前i件物品 最后一件状态i为1,2;dp[i][1]=max(dp[j][2])+a[i] 1<=j<=i(j~i不拿) 保持前缀i中最大的dp[j][2]即可 #include <iostream> #include <algori

F-Dining Cows(POJ 3671)

Dining Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7584   Accepted: 3201 Description The cows are so very silly about their dinner partners. They have organized themselves into two groups (conveniently numbered 1 and 2) that ins

POJ 3670 &amp;&amp; POJ 3671 (dp)

最长不下降子序列的应用嘛.两题都是一样的. POJ 3670:求给定序列按递增或递减排列时,所需改变的最小的数字的数目. POJ 3671:求给定序列按递增排列时,所需改变的最小的数字的数目. 思路就是求最长不下降子序列,然后剩下的就是需要改变的字母. 最长不下降子序列:(我之前有写过,不懂请戳)http://blog.csdn.net/darwin_/article/details/38360997 POJ 3670: #include<cstdio> #include<cstring

poj 3186 Treats for the Cows dp

#include <cstdio> #include <algorithm> using namespace std; #define maxn 2100 int dp[maxn][maxn]; int val[maxn]; int n; int main() { while(scanf("%d",&n)!=EOF) { int i,j; for(i=1;i<=n;i++) { scanf("%d",&val[i]);

POJ 3671 (n log n,LIS)

POJ 3671 题意:要使序列形成非递减的序列,最少改变几个数: 思路:ans=n-lis(); #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<vector> #include<cmath> using namespace std; int n; int cow[30010]; vector<int> len

poj 3281 Dining(最大流)

poj 3281 Dining Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their prefer

POJ 3281 Dining(网络最大流)

http://poj.org/problem?id=3281 Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9121   Accepted: 4199 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.