Stay Hungry,Stay Foolish!

D - Reindeer and Sleigh

D - Reindeer and Sleigh

https://atcoder.jp/contests/abc334/tasks/abc334_d

 

思路

将所有雪橇的耗费的鹿数量,进行排序

然后对排序数组做前缀和,

在前缀和数组中,利用upper_bound做二分查找。

 

Code

https://atcoder.jp/contests/abc334/submissions/48935041

LL n, q;
vector<LL> r;
LL x;

int main()
{
    cin >> n >> q;
    
    for(LL i=0; i<n; i++){
        LL ri;
        cin >> ri;
        r.push_back(ri);
    }

    sort(r.begin(), r.end());

    // prefix of r
    vector<LL> rsum;
    rsum.push_back(r[0]);

    for(LL i=1; i<n; i++){
        rsum.push_back(rsum[i-1] + r[i]);
    }

    for(LL i=0; i<q; i++){
        cin >> x;
        LL pos = upper_bound(rsum.begin(), rsum.end(), x) - rsum.begin();
        cout << (pos) << endl;
    }
    
    return 0;
}

 

posted @ 2023-12-31 09:57  lightsong  阅读(3)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel