2023-CSPJ 题解

2023-CSPJ

T1. 小苹果

考虑第一问,注意到每次去走一些数字后会变成另一个子问题,且 n′≤23nn'\leq \frac{2}{3}nn32n,故暴力维护即可。

对于第二问,用同样的办法,暴力维护 nnn 的标号即可。

Code 100pts

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
inline int read()
{
   
   
    int x = 0, f = 1;
    char s = getchar();
    while (s < '0' || s > '9')
    {
   
   
        if (s == '-')
            f = -f;
        s = getchar();
    }
    while (s >= '0' && s <= '9')
    {
   
   
        x = (x << 3) + (x << 1) + (s ^ 48);
        s = getchar();
    }
    return x * f;
}

int main()
{
   
   
    // freopen("in.txt", "r", stdin);   //输入重定向,目录下的in.txt文件中读取
    // freopen("out.txt", "w", stdout); //输出重定向,目录下的out.txt文件中

    int n = read();

    int cnt = 0,  res = 0;

    while (n)
    {
   
   
        cnt++;
        int k = (n - 1) % 3;
        if (res == 0 && k == 0)
            res = cnt;
        n = n - 1 - (n - 1) / 3; // 每次减少的数量。
    }
    cout << cnt << ' ' << res;

    return 0;
}

T2.公路

考虑贪心,对于每个加油站,找到下一个比他便宜的油站,每次在这个位置加尽可能少的油,使得能顺利开到到下一个比他便宜的油站,一路贪心下去就可以了。注意开long long

拓展一下: 题目中的车的油桶是无限的。 如果现限制油桶容积这需要考虑新的算法。 用单调栈。

Code 100pts

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const int N = 2e5 + 10;
int n, d;

LL v[N], a[N]; // v[i] 代表i-i+1 的距离, a[i] 代表第i个站点的油价a[i]

inline int read()
{
   
   
    int x = 0, f = 1;
    char s = getchar();
    while (s < '0' || s > '9')
    {
   
   
        if (s == '-')
            f = 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值