12.4题解

· · 个人记录

A. Medium Number

题意

给定三个互不相同的整数 a,b,c,输出其中的中位数。

解题思路

将 a,b,c 三个整数排序,输出中间的数即可。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[4];
int t;
signed main() {
    cin>>t;
    while(t--){
        for(int i=1;i<=3;i++){
            cin>>a[i];
        }
        sort(a+1,a+4);
        cout<<a[2]<<'\n';

    }
}

B. Challenging Valleys

题意

给定一个长度为 n 的数组,判断该数组是否为一个 \texttt{valley}。

如果数组满足以下条件,则称其为 \texttt{valley}:

解题思路

  1. 去重:
    由于问题的核心在于连续相等的数字构成的区间,我们可以先去掉数组中所有连续相同的元素,只保留相邻不同的元素,形成一个去重数组。

  2. 统计 \texttt{valley} 区间:
    遍历去重后的数组,检查满足以下条件的元素 i 是否唯一:

  1. 判断:
    如果满足 \texttt{valley} 条件的元素数量为 1,则输出 \texttt{YES},否则输出 \texttt{NO}。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[200004];
int t;
signed main() {
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int temp=0;
        int now=0;
        for(int i=1;i<=n;i++){
            cin>>temp;
            if(temp!=a[now]){
                now++;
                a[now]=temp;
            }
        }
        int f=0;
        for(int i=1;i<=now;i++){
            if(now==1){
                f++;
            }else if(i==1&&a[i]<a[i+1]){
                f++;
            }else if(i==now&&a[i-1]>a[i]){
                f++;
            }else if(a[i]<a[i-1]&&a[i]<a[i+1]){
                f++;
            }
        }
        if(f==1){
            cout<<"YES"<<'\n';
        }else cout<<"NO"<<'\n';
    }
}

C. Quests

题意

有 n 个任务。完成第 i 个任务可以获得 a_{i} 个金币。

你需要判断以下情况:

  1. 给定 d 天获取 c 个金币的目标,找到最大的冷却时间 k,使得在 d 天内能至少获得 c 个金币。
  2. 如果不存在这样的 k,输出 \texttt{Impossible}。
  3. 如果冷却时间 k 可以任意大且仍然能满足目标,输出 \texttt{Infinity}。

解题思路

奖励排序

将任务的奖励金币按照降序排列,这样可以优先选择最有价值的任务来满足目标。

边界条件判断

  1. \texttt{Infinity}:
    如果每天不重复地完成一个收益最高的任务,如果能够获得的总金币数 \texttt{sum} 满足:sum \geq c,则输出 \texttt{Infinity},因为冷却时间可以任意大,不影响结果。

  2. \texttt{Impossible}:
    如果在最理想情况下,即每天都完成收益最高的任务(忽略冷却时间)持续 d 天,仍然无法获得至少 c 个金币,则输出 \texttt{Impossible},因为无论冷却时间如何调整,目标都无法达成。

二分查找最大 k

使用二分查找,找到能够满足目标的最大冷却时间 k。

使用辅助函数 \texttt{check}(k) 计算冷却时间为 k 时的金币获取情况:

前 d 天中,每 k+1 天重复从收益最高的任务开始选择,计算所能获得的金币总数。 如果累计的金币数量达到 c,返回 \texttt{true};否则返回 \texttt{false}。

更新搜索区间: 如果当前冷却时间 k 可以满足目标,记录答案并尝试更大的 k。 如果当前冷却时间 k 不满足目标,尝试更小的 k。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[200004];
int t;
int n,c,d;
int l,r,mid;
bool cmp(int x,int y){
    return x>y;
}
bool check(int k){
    int result=0;
    for(int i=0;i<d;i++){
        result+=a[i%(k+1)];
        if(result>=c)
        return true;
    }
    return false;
}
signed main() {
    cin>>t;
    while(t--){
        memset(a,0,sizeof(a));
        cin>>n>>c>>d;
        for(int i=0;i<n;i++){
            cin>>a[i];
        }sort(a,a+n,cmp);

        int maxx=0;
        for(int i=0;i<min(n,d);i++){
            maxx+=a[i];
        }
        if(maxx>=c){
            cout << "Infinity" << '\n';
            continue;
        }
        if(a[0]*d<c){
            cout << "Impossible" << '\n';
            continue;
        }
        int ans;
        l=0,r=d;
        while(l<=r){
            mid=(l+r)/2;
            if(check(mid)){
                ans=mid;
                l=mid+1;
            }else
            r=mid-1;
        }
        cout<<ans<<'\n';
    }
}