題意:
給你一個序列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;
}
uva_11384_Help is needed for Dexter(貪心)
最新推荐文章于 2017-08-07 14:15:46 发布