CCPC-Wannafly Winter Camp Day1 (Div2, onsite) J Indiana Jones

Category : Enumeration Greed
Portal: Indiana Jones https://www.zhixincode.com/contest/1/problem/J?problem_id=21
Similar topic : codeforce#round div2 C.Election

## ideas##

1. Why use a vector instead of an array directly?
Because each person's item has a different value, you can't just take it ++ -
We also have items from small to large according to the size of the person's items. Take, use vector to facilitate sorting
2. So when do you want to take it?
We have to take items from more people than we have in our hands.
If it is not enough, the remaining quantity will be taken from small to large
(1.2. above is to take items greedily)
3. But how do we know the optimal what's the result? ? ?
Then we enumerate, what happens when I have i items in my hand.
In this way, we can get the optimal situation when I hold various numbers in my hand; the optimal situation in these situations is the global optimal

AC code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1005;
struct node
{
    ll id,cost;
};
int cmp(node a ,node b)
{
    return a.cost<b.cost;
}
vector<node>all;
vector<node>man[maxn];
int vis[maxn];
int n,m;
int main()
{
    cin>>n>>m;
    int a,b;
    for(int i=0;i<m;i++)
    {
        cin>>a>>b;
        all.push_back({i,a});
        man[b].push_back({i,a});
    }
    sort(all.begin(),all.end(),cmp);
    for(int i=1;i<=n;i++)
    {
        sort(man[i].begin(),man[i].end(),cmp);
    }
    ll maxx=1e15;
    //枚举所有情况
    for(ll k=1;k<=m;k++)
    {
        ll ans=0,num=0;
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
        {
            if(man[i].size()>=k)
            {
                for(int j=0;j<=man[i].size()-k;j++)
                {
                    ans+=man[i][j].cost;
                    num++;
                    vis[man[i][j].id]=1;
                }
            }
        }
        for(ll i=0;i<all.size() && num<k ;i++)
        {
            if(!vis[all[i].id])
            {
                ans+=all[i].cost;
                num++;
            }
        }
        maxx=min(maxx,ans);
    }
    cout<<maxx<<endl;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325687188&siteId=291194637