本文已参与「新人创作礼」活动, 一起开启掘金创作之路。
【CCPC】2022威海站 E. Python Will be Faster than C++ | 数学、模拟
题目链接
题目
题目大意
给定长度为 的数组 ,其中 表示当 时 版本的 Python 运行某代码需要花费的时间。给定 表示 C++ 运行该代码需要花费的时间。
预测当 时 版本的 Python 运行该代码需要花费的时间
请输出预计最低哪个版本的 Python 运行该代码需要的时间小于 。输入数据保证 。
思路
显然只与整个数组的最后两项有关。令 ,则从 开始各个版本的 Python 运行该代码需要的时间为
化简得
即 版本的 Python 运行该代码需要的时间为 。问题转化为求满足 的最小的 。
如果 ,则 ,上述方程无解,输出 Python will never be faster than C++。否则解得 ,则最小的运行时间小于 的Python版本 ,输出答案 Python 3.{ans} will be faster than C++ 即可。
由于数据范围很小也没有多组数据,在发现如果 则无解排除这种情况之后,也可以暴力计算每个版本运行某代码需要的时间去和 进行比较。
代码
#include <iostream>
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
using LL=long long;
const int N=500001;
const LL mod=1000000007;
//const LL mod=998244353;
LL a[N];
LL solve()
{
int n;
LL k;
scanf("%d",&n);
scanf("%lld",&k);
for (int i=1;i<=n;++i) scanf("%lld",&a[i]);
LL x=a[n-1];
LL y=a[n];
if (y-x>=0) printf("Python will never be faster than C++\n");
else printf("Python 3.%d will be faster than C++\n",n+(k-y)/(y-x)+1);
return 0;
}
int main()
{
int T=1;
while (T--) solve();
return 0;
}