1490G - Old Floppy Drive 1900

题目来源

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值