codeforces 269B Greenhouse Effect (贪心+dp)

题解:

给出n棵树,每颗树都属于某种品种,品种按照1-m标号。现在给出了每棵树的品种和位置,问如何用最小的移动次数使得这n棵树的品种按照递增的序号牌号。

题解:

一开像这样做:dp[i][2] dp[i][0]表示以i这棵树前面树的品种序号比他大的个数,dp[i][1]相反。然后从1-n找出dp[i][1]+dp[i][0]的最小值,但是想了下第三个测试实例,果然推翻。

突然灵光一动发现一个规律,用贪心来分析发现,只要尽量让序列中品种数成上升的个数最多,那么就是找着序列中非递减子序列(可以有相同的),然后n-这个最长的非递减子序列就是我们要一动树的个数!!1a

#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<map>
using namespace std;
typedef long long lld;
const int oo=0x3f3f3f3f;
const lld OO=1e18;
const int Mod=1000000007 ;
const int maxn=5000+5;
int dp[maxn];
struct Tree
{
    int spe;
    double pos;
}a[maxn];

bool cmp(Tree t1,Tree t2)
{
    return t1.pos<t2.pos;
}

int main()
{
    int n,m,ans;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%d %lf",&a[i].spe,&a[i].pos);
        sort(a+1,a+1+n,cmp);
        memset(dp,0,sizeof dp);
        a[0].spe=-oo;
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<i;j++)
            {
                if(a[j].spe<=a[i].spe&&dp[j]+1>dp[i])
                    dp[i]=dp[j]+1;
            }
        }
        ans=oo;
        for(int i=1;i<=n;i++)
            ans=min(ans,n-dp[i]);
        printf("%d\n",ans);
    }
    return 0;
}
/**
3 2
2 1
1 2.0
1 3.100
3 3
1 5.0
2 5.5
3 6.0
6 3
1 14.284235
2 17.921382
1 20.328172
3 20.842331
1 25.790145
1 27.204125
*/




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值