您的位置:首页 > 其它

uva_11384_Help is needed for Dexter(貪心)

2013-04-09 20:59 441 查看
題意:
給你一個序列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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: