uva_11384_Help is needed for Dexter(貪心)

7 篇文章 0 订阅
題意:
給你一個序列S = {1, 2, 3, ..., n}, 每次可以從中選擇一些元素刪除特定的數字,當然所刪除的數字必須小於等於取出來的元素,最後使得S = {0},的最小刪除次數
分析:
不難想出一個DP方法:設狀態f[1,2,3...,n]爲所求
當 n = 1 時,f[1] = 1.
當 n = 2 時,f[1,2] = max(f[1])+1
當 n = 3 時,f[1,2,3] = max(f[1,2], f[1])+1
...
狀態轉移:f[1,2,3,...,n] = max(f[1,2,3,...,n-1], f[1,2,3,...,n-2], f[1,2,3,...,n-3] ... f[1,2,3,...,n/2])+1
當然拉,n越大所需要的次數越多,再加上這裏n特別大(10^9),DP無論於時間還是空間上都不符合要求, 所以得出f[1,2,3...n] = f[1,2,3,...n/2]+1的貪心算法
Code:
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;


#define DIR     4
#define DIM     2
#define STATUS  2
#define MAXN    1000 + 10
#define MAXM    100000 + 10
#define oo      (~0u)>>1
#define INF     0x3F3F3F3F
#define REPI(i, s, e)   for(int i = s; i <= e; i ++)
#define REPD(i, e, s)   for(int i = e; i >= s; i --)


static const double EPS = 1e-5;


inline int cal(int n)
{
        if( 1 == n ) {
                return 1;
        }
        return cal(n>>0x1)+1;
}


int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
        freopen("test.in", "r", stdin);
#endif
        int n;
        while( ~scanf("%d", &n) ) {
                printf("%d\n", cal(n));
        }
        return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值