一、分析
最后三个点,q和n的最大值都是20万,乘起来就是400亿。如果直接枚举会超时。所以可采用二分的方法。
upper_bound(a, a + n, num)是用二分的方法求第一个大于num的元素的地址。
二、代码
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxN = 200000 + 5;
double Time[maxN];
int main()
{
freopen("endless.in", "r", stdin); //输入重定向
freopen("endless.out", "w", stdout);//输出重定向
int n, L, v;
cin >> n >> L >> v; //魔法种类、山坡长度、速度
long long pos[n + 1];//下标从1开始,数组大小是n+1
for(int i = 1; i <= n; i++)
{
cin >> pos[i]; //第i个魔法的位置
}
sort(pos + 1, pos + 1 + n, greater<int>());//从大到小排序
long long s = L;
Time[0] = s * 1.0 / v; //不使用魔法所需的时间
for(int i = 1; i <= n; i++)
{
s += pos[i]; //加上使用第i个魔法的距离
Time[i] = s * 1.0 / v;//使用第i个魔法后所需的时间
}
int query;
cin >> query;
while(query--)
{
int t;
cin >> t;
if(Time[0] > t)
{
cout << 0; //不需要使用魔法
}
else if(Time[n] <= t)
{
cout << -1; //所有魔法都用了,仍不能使时间大于t
}
else
{
cout << upper_bound(Time + 1, Time + n + 1, t) - Time; //二分求需要使用几次魔法
}
cout << endl;
}
return 0;
}
了解信息学奥赛请加微信307591841(QQ同号)