【Codevs】3289 花匠 --2013年NOIP全国联赛提高组 贪心

20 篇文章 0 订阅
11 篇文章 0 订阅

贪心:
寻找拐点,也就是单调子序列。
我的做法是 找到一个序列就按照他的单调性走,直到单调性改变,这就是一个单调序列,也就是拐点。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int MAXN = 100000 + 10;
const int INF = 0x3f3f3f3f;
int num[MAXN];

int main()
{
    int n, ans = 0;
    cin >> n;
    num[0] = num[n+1] = INF;
    for(int i = 1; i <= n; i ++)
        scanf("%d",&num[i]);
    int i = 1;
    while(i < n)
    {
        int Run1 = 0, Run2 = 0;
        while(num[i] <= num[i+1] && i+1 <= n)
        {
            Run1 = 1;
            i ++;
        }
        while(num[i] >= num[i+1] && i+1 <= n)
        {
            Run2 = 1;
            i ++;
        }
        if(Run1)
            ans ++;
        if(Run2)
            ans ++;
    }
    cout << ans+1 << endl;
    return 0;
}

暴力:
枚举子集,判断。
优化:如果当前长度的子集已经不符合要求了,那就不在此基础上增加长度了。
30分暴力代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int MAXN = 100000 + 10;
int n, cnt = 0, ans = 0;
int h[MAXN], g[MAXN];
bool t[MAXN];

bool check()
{
    bool x = true, y = true;
    for(int i = 1; i <= cnt/2; i ++)
        if(g[2*i] < g[2*i-1])
            x = false;
    for(int i = 1; i < cnt/2; i ++)
        if(g[2*i] < g[2*i+1])
            x = false;
    for(int i = 1; i <= cnt/2; i ++)
        if(g[2*i] > g[2*i-1])
            y = false;
    for(int i = 1; i < cnt/2; i ++)
        if(g[2*i] > g[2*i+1])
            y = false;
    if(cnt == 1)
    {
        if(x && y)
        {
            ans = max(ans,cnt);
            return 1;
        }
    }
    else if(x || y)
    {
        ans = max(ans,cnt);
        return 1;
    }
    return 0;
}

void dfs(int x)
{
    if(x > n)
    {
        //优化1; 
    /*  if(cnt <= ans)
            return;*/
        check();
        return;
    }
    // 优化2;
    if(!check())
        return;
    else{
        for(int i = 0; i <= 1; i ++)
        {
            if(i)
            {
                g[++cnt] = h[x];
                dfs(x+1);
                g[cnt--] = 0;
            }
            else dfs(x+1);

        }
    }
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i ++)
        scanf("%d",&h[i]);
    dfs(1);
    cout << ans << endl;
    return 0;
}

几组数据:

/*
5
1 3 9 5 10

4

5 
5 3 2 1 2

3

10
1 83 31 100 68 53 40 81 66 0

7
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值