一、分析
最後三個點,q和n的最大值都是20萬,乘起來就是400億。如果直接枚舉會超時。所以可採用二分的方法。
二、代碼
#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];
for(int i = 1; i <= n; i++)
{
cin >> pos[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];
Time[i] = s * 1.0 / v;
}
int query;
cin >> query;
while(query--)
{
int t;
cin >> t;
if(Time[0] > t)
{
cout << 0;
}
else if(Time[n] <= t)
{
cout << -1;
}
else
{
cout << upper_bound(Time + 1, Time + n + 1, t) - Time; //二分
}
cout << endl;
}
return 0;
}
瞭解信息學奧賽請加微信307591841(QQ同號)