寒假刷題6:Help is needed for Dexter(UVa11384)

題目鏈接:
Help is needed for Dexter

題目解析:
這個題看起來蠻水的…不過想要快速想出正解還是有一定難度。
核心思想是每次操作後,使儘可能多的數字等價。
舉個栗子:1 2 3 4 5 6這六個數字,把4,5,6同時減去min(4,5,6)=4得0,1,2.等價於 1,2,3。換句話說,f(6)=f(3)+1.
這樣就得到了一個遞歸關係:f(n)=f(n/2)+1,f(1)=1.
總感覺有一道區域賽題跟這道題思路很相似然鵝我一時想不起來

AC代碼 :

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define ll long long
ll n,ans;
ll dfs(ll x)
{
    if(x==1) return 1;
    return dfs(x/2)+1;
}
int main()
{
    while(scanf("%lld",&n)!=EOF)
        printf("%lld\n",dfs(n));
}

發佈了56 篇原創文章 · 獲贊 22 · 訪問量 6313
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章