题目来源
https://codeforces.ml/problemset/problem/1490/G
题意分析
在一个可以无限延伸的前缀和中,寻找出第一个大于等于询问数字的数。有则输出位置,没有则输出-1。
思路分析
[待补充]
code
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn = 2e5 + 7;
ll a[maxn], b[maxn];
ll pre[maxn];
ll ans[maxn];
vector <ll> v, vp;
int main(){
int t; scanf("%d", &t);
while (t --){
v.clear();
vp.clear();
int n, m; scanf("%d%d", &n, &m);
for (int i=1; i<=n; i++) scanf("%lld", &a[i]);
for (int i=1; i<=m; i++) scanf("%lld", &b[i]);
ll sum = 0, mmax = -INF;
for (int i=1; i<=n; i++){
sum += a[i];
pre[i] = pre[i-1] + a[i];
mmax = max(mmax, pre[i]);
}
ll pmax = -INF;
for (int i=1; i<=n; i++){
if (pmax >= pre[i]) continue;
pmax = pre[i];
v.push_back(pre[i]);
vp.push_back(i);
}
// int l1 = (int)v.size();
// for (int i=0; i<l1; i++) cout << v[i] << " ";
// cout << endl;
// for (int i=0; i<l1; i++) cout << vp[i] << " ";
// cout << endl;
// cout << "---------------------" << endl;
for (int o=1; o<=m; o++){
if (b[o] > mmax && sum <= 0){
ans[o] = -1; continue;
}
ll res = 0;
if (sum <= 0) res = 0;
else res = (b[o] - mmax + sum - 1) / sum;
if (res < 0) res = 0;
ll tmp = b[o] - res * sum;
// cout << "tmp: " << tmp << " res: " << res << endl;
int p = lower_bound(v.begin(), v.end(), tmp) - v.begin();
// cout << "p: " << p << endl;
ans[o] = vp[p] + res * n - 1;
}
for (int i=1; i<=m; i++) printf("%lld ", ans[i]);
printf("\n");
}
return 0;
}