CodeForces 1082 C.Multi-Subject Competition (prefix + and thinking)

Meaning of the questions:

There are n individuals, m projects, everyone has the optional subjects si, and the ability to value ri,
the largest sum of the capacity of each project now either selected or not selected, choose the project requires the same number, ask your project is selected number
n, m <= 2e5

Ideas:

Give each subject easy to think of descending order, and then select the number of enumeration

ANS is represented by (x) of x number of answers
is calculated ANS (x) need to traverse each class m class, and a class of prefix if the SUM (x) is greater than 0 accumulation
i.e. ans (x) = sum1 (x ) + sum2 (x) ...
need to iterate again accumulated data range for this question is obviously too much

Think may be reversed, all sum (x) was added to ans (x)
when the calculated current prefix and the prefix and if the sum (k) is greater than 0, the answer to the accumulation ans (k), eliminating the need to traverse the layer of
the final All ans take max is the answer

code:

#include<bits/stdc++.h>
using namespace std;
const int maxm=1e5+5;
vector<int>g[maxm];
int ans[maxm];
bool cmp(int a,int b){
    return a>b;
}
signed main(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        int s,v;
        cin>>s>>v;
        g[s].push_back(v);
    }
    int malen=0;
    for(int i=1;i<=m;i++){
        sort(g[i].begin(),g[i].end(),cmp);
        int len=g[i].size();
        malen=max(malen,len);
        int sum=0;
        for(int j=0;j<len;j++){
            sum+=g[i][j];
            if(sum>0)ans[j+1]+=sum;
            else break;
        }
    }
    int res=0;
    for(int i=1;i<=malen;i++){
        res=max(res,ans[i]);
        if(ans[i]==0)break;
    }
    cout<<res<<endl;
    return 0;
}
Published 430 original articles · won praise 36 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_44178736/article/details/104401231