今日练习

今日练习

UVA 10795 AC(HALF COPY);

LA 3971

UVA 10795

//
// Created by 21122 on 2022/5/23/0023.
//
#include<iostream>
using namespace std;

int start[65], finish[65];


long long func(int *p, int i, int final)
{   //func():把1,2,3, ......i,全部移动到 final 所需要的步数
     if(i == 0)
     {
         return 0;
     }
     if(p[i] == final)
     {
         return func(p, i - 1, final);
     }

     return func(p, i - 1, 6 - p[i] - final) + (1LL << (i - 1));

}

int main()
{
    int n;
    int Case = 1;
    while(cin >> n)
    {
        if(n == 0)
        {   //n 为 0 是输入结束标志,跳出;
            break;
        }

        for(int i = 1; i <= n; i++)
        {   //起始状态,数组存储;
            cin >> start[i];
        }

        for(int i = 1; i <= n; i++)
        {   //目标状态,数组存储;
            cin >> finish[i];
        }

        int k = n;
        while(k >= 1 && start[k] == finish[k])
        {
            k --;
        }

        long long result = 0;
        if(k >= 1)
        {
            int other = (1 + 2 + 3) - start[k] - finish[k];
            result = func(start, k - 1, other) + func(finish, k - 1, other) + 1;

        }

        cout << "Case " << Case ++ << ": " << result << endl;


    }

    return 0;
}

LA 3971

P1873

//
// Created by 21122 on 2022/5/24/0024.
//
#include<iostream>
#include<algorithm>
using namespace std;

int arr[1000005];
int N, M;

bool is_enough(int height)
{   //准备切的树木 注意树的高度,用 LL 来接
    long long sum = 0;
    for(int i = 1; i <= N; i++)
    {
        if(arr[i] > height)
        {
            sum += arr[i] - height;
        }

    }
    //cout <<"sum is:" <<sum<< endl;
    if(sum >= M)      //注意这里的等号
    {
        return true;
    }
    return false;

}


int find_length(int highest)
{
    int left = 0;
    int right = highest * 5 + 1;    //再切也不能比最高的树高啦!
    //cout << "highest is:" << highest << endl;
    while(right - left > 1)
    {   //二分答案法,
        int mid = (left + right) / 2;
        //cout << "mid is:" << mid << endl;
        if(is_enough(mid))
        {   //符合或过于符合,即多切了树木
            left = mid;
        }

        else
        {   //高度过高,不满足需求
            right = mid;
        }
    }

    return left;

}

int main()
{
    //int N, M;
    cin >> N >> M;
    for(int i = 1; i <= N; i++)
    {
        cin >> arr[i];
    }

    int max = *max_element(arr + 1, arr + 1 + N);
    //cout << "max is :" << max;


    int ans = find_length(max);
    cout << ans << endl;

    return 0;
}