您的位置:首页 > 其它

UVA 11384 Help is needed for Dexter

2013-05-31 12:32 621 查看
给定序列1...n,每次可以选中序列几个数,然后同时减去相同到一个数,求把序列全部变成0所需要到最小次数.

做法是每次选择序列中最中间到位置到那个数,然后中间到最右边的区间到数全部减去这个值,如 1 2 3 4 5 6 选择4 减掉后得 1 2 3 0 1 2 这个序列其实跟1 2 3 是一样的.

一直这样到操作直到n变成0,复杂度为lgn

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

int n;
int main(){
while(~scanf("%d", &n)){
int res = 0;
while(n){
n /= 2;
res++;
}
printf("%d\n", res);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: