个人代码,仅供参考
小苹果
模拟。 n个苹果 (1) 每次可以拿 (n-1)/3+1 个
(2) 当满足 n%3=1 时,最后一个苹果可以拿到
注意 : 在满足第二个条件时,要取第一次满足条件时的天数,
因为后面尽管能拿到最后一个,但不是编号为n的
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,day=0,ans,f=1;
cin>>n;// 苹果数
while(n){
day++;
if(f&&n%3==1){
ans=day;
f=0;// 修改f值
}
n-=((n-1)/3+1);
}
cout<<day<<" "<<ans;
return 0;
}
公路
模拟。 油价越低越好。不断寻找油价更低的站点。
注意油可以剩余,可以保存油箱剩余可以行驶的距离数来简化运算
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=100009;
ll v,a[maxx],sum[maxx];
ll ans;
int main() {
// freopen("input.txt","r",stdin);
int n,d;
cin>>n>>d;
for(int i=2; i<=n; i++) {
cin>>v; // sum[i] : 到达i站点时的距离
sum[i]=sum[i-1]+v; // 前缀和
}
for(int i=1; i<=n; i++)
cin>>a[i]; // 油价
int pos=1;
ll f=0; // f:总加油量
for(int i=2; i<=n; i++) {
if(a[i]<a[pos]||i==n) {
ans+=(sum[i]-f*d+d-1)/d*a[pos];// 仍需补充的油量
// 由于只能出售整数升,加 d-1向上取整
// sum[i]-f*d 油箱剩的油还能跑的距离
f+=(sum[i]-f*d+d-1)/d;
pos=i;
}
}
cout<<ans<<endl;
return 0;
}