"华为杯" 武汉大学21级新生程序设计竞赛 D题非官方题解

"华为杯" 武汉大学21级新生程序设计竞赛 D题题解

不想做了(我太菜了。。

https://ac.nowcoder.com/acm/contest/31620#question

D.和谐之树

思路:披着线段树外皮的二分。把区间逐层二分。注:要统计能到达的深度,深度相同时优先走右边(比赛的时候就是因为这个没过)

代码

#include <iostream>
#include <algorithm>

using namespace std;
typedef long long ll;
int t;
ll n, num, ans;

ll count (ll x) {
    ll tt = 1;
    while (x > 1) {
        x = (x + 1) / 2;
        tt ++;
    }
    return tt;
}

void dfs (ll l, ll r, ll num) {
    ans = max (ans, num);
    if (l == r)
        return ;
    ll mid = l + r  >> 1;
    if (count(mid - l + 1) > count(r - mid )) {
        dfs (l, mid, num << 1);
    }
    else {
        dfs (mid + 1, r, num << 1 | 1);
    }

}


int main () {
    cin >> t;
    
    while (t --) {
        cin >> n;
        ans = 0;
        dfs (1, n, 1);
        cout << ans << endl;
        

    }
    
        
}

H. wwhhuu

思路

等价于已知n,找三个非负整数a,b,c,使其和为n,求abc积的最大值。由于均值不等式可得,三者数量接近时有最大可能出现

代码

#include <iostream>
#include <algorithm>

using namespace std;

int main () {
    int n;
    cin >> n;
    
    int x, y, z;
    if (n % 3 == 0) {
        cout << (n / 3) * (n / 3) * (n / 3) << endl;
        return 0;
    }

    if (n % 3 == 1) {
        x = n / 3 + 1, y = n / 3, z = n / 3;
        cout << x * y  * z << endl;
        return 0;
    }

    x = y = n / 3 + 1;
    z = n - x - y;
    cout << x * y  * z << endl;
}

呜呜呜小马宝莉救救我

posted @ 2022-04-04 17:32  Sakana~  阅读(39)  评论(0编辑  收藏  举报