UVA 11384 Help is needed for Dexter

来源:互联网 发布:2017教师网络培训平台 编辑:程序博客网 时间:2024/05/16 13:54

给定序列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;}



原创粉丝点击