Codeforces Round #827 (Div. 4) E. Scuza

Codeforces Round #827 (Div. 4) E. Scuza

  • 前缀和 前缀和前缀和 + 二分 二分二分
  • Let’s compute the prefix sums of the array a aa: let b i b_ibi= a 1 + ⋯ + a i a_1+⋯+a_ia1++ai. Rephrasing the problem: for each question containing an integer k kk, we need to find the largest ai such that a 1 , … , a i a_1,…,a_ia1,,ai are all at most k kk, and then output b i b_ibi. In other words, m a x ( a 1 , … , a i ) ≤ k max(a_1,…,a_i)≤kmax(a1,,ai)k.
  • Let’s make the prefix maximums of the array: l e t m i = m a x ( a 1 , … , a i ) let m_i=max(a_1,…,a_i)letmi=max(a1,,ai). Then we need to find the largest i ii such that m i ≤ k m_i≤kmik, which is doable using binary search, since the array m is non-decreasing. Once we find the index i ii, we simply need to output b i b_ibi.
  • The time complexity is O ( n l o g n ) O(nlogn)O(nlogn) per testcase.
  • 所有数字都比当前数字大就意味着不能上任意一阶台阶,所以输出0。
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;

int main()
{
    int T;cin>>T;
    while (T--)
    {
        int n,q;cin>>n>>q;
        vector<LL> a(n),s(n);
        vector<LL> k(q+1);
        for(int i=0;i<n;i++) cin>>a[i],s[i]=a[i];
        for(int i=0;i<q;i++) cin>>k[i];

        for(int i=1;i<n;i++) a[i]=max(a[i],a[i-1]);
        for(int i=1;i<n;i++) s[i]+=s[i-1];

        for(int i=0;i<q;i++)
        {
            int x=upper_bound(a.begin(),a.end(),k[i])-a.begin();
            if(x==0) cout<<"0 ";
            else cout<<s[x-1]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

版权声明:本文为qq_52792570原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。