HDU multi-school third Find the answer

This is a problem on the original title of the original cf, but for some modification of the data range, but still good to think

The meaning of problems: Given an array of length N, for each position in the array, and the current is smaller than the minimum cost to meet the need to remove the M

Analysis: whether the current need to remove some values, you can take the greedy method for each join in the number of sort, whether the current need to remove some of the value depends on the value of all current and, when more than M, greedy removal the largest, but the current situation needs to be removed to the number of elements to join, but taking into account the impact on the back of elements, so get rid of the elements is not necessarily before, we really want to get rid of that value after the elements are added.

The following reasons: When the added value is smaller than the stored maximum current value, when taking into account the maximum value of the elements behind certain preferred delete operation if necessary, remove the current added value than the value currently stored deleted, it is necessary to back elements removed. When the added value of the stored maximum current value is large than or equal to anyone be omitted, so at this time to ensure the correctness of the answer.

Specific use mutiset to maintain, multiset is <set> library a very useful type, it can be seen as a sequence, insert a number, delete a number can be completed in time O (logn), so it is good to use it is also very convenient

Specific code:

#include<bits/stdc++.h>
using namespace std;

const int maxn=2e5+7;
int a[maxn];
multiset<int>ss;

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ss.clear();
        long long int n,m;
        scanf("%lld%lld",&n,&m);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        long long int sum=0;
        int tem=0;
        for(int i=0;i<n;i++)
        {
            long long int suma=sum;
            int jishu=0;
            if(suma+a[i]>m)
            {
                auto j=ss.end();
                while(suma+a[i]>m)
                {
                    j--;
                    suma-=*j;
                    jishu++;
                }
            }
            printf("%d ",jishu+tem);
            ss.insert(a[i]);
            auto j=ss.end();
            sum+=a[i];
            while(sum>m)
            {
                j--;
                sum-=*j;
                ss.erase(ss.find(*j));
                tem++;
            }
        }
        printf("\n");
    }
}
View Code

 

Guess you like

Origin www.cnblogs.com/plys/p/11266762.html