2023-CSPJ
T1. 小苹果
考虑第一问,注意到每次去走一些数字后会变成另一个子问题,且 n′≤23nn'\leq \frac{2}{3}nn′≤32n,故暴力维护即可。
对于第二问,用同样的办法,暴力维护 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 =