首页 > 其他分享 >D - Reindeer and Sleigh

D - Reindeer and Sleigh

时间:2023-12-31 10:13:11浏览次数:24  
标签:begin rsum LL abc334 Reindeer 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;
}

 

标签:begin,rsum,LL,abc334,Reindeer,Sleigh
From: https://www.cnblogs.com/lightsong/p/17937237

相关文章