码迷,mamicode.com
首页 > 其他好文 > 详细

Supermarket POJ - 1456

时间:2020-03-21 20:56:28      阅读:60      评论:0      收藏:0      [点我收藏+]

标签:else   amp   return   stream   include   while   ring   clu   ++   

并查集+贪心

https://vjudge.net/contest/362147#problem/C

#include <iostream>
#include <cstdio>
#include <cstring>
#include <limits>
#include <algorithm>
#define endl ‘\n‘
#define _for(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const int N = 1e4+5;
typedef long long ll;
int n,P[N]; 
struct Node{
    int pi,di;
    bool operator < ( const Node &o )const{
        if( pi!=o.pi )
            return pi>o.pi;
        return di<o.di;
    }
}a[N];
int find(int p){
    if( P[p]== p ) return p;
    return P[p]= find( P[p] );
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    while(cin>>n){
        ll res = 0;
        int pos = 0;
        _for(i,1,n+1){
            cin>>a[i].pi>>a[i].di;
            pos = max(pos,a[i].di);
        }
        pos = min(pos,(int)1e4);
        sort(a+1,a+n+1);
        _for(i,0,pos+1) P[i]= i;
        _for(i,1,n+1){
            int apos = a[i].di;
            if( P[apos] ==apos ){
                res += a[i].pi;
                P[apos] = apos-1;
            }
            else{
                apos = find(apos);
                if( apos > 0 ){
                    res += a[i].pi;
                    P[apos] = apos-1;
                }
            }
        }
        cout<<res<<endl;
    }
    return 0;
}

 

Supermarket POJ - 1456

标签:else   amp   return   stream   include   while   ring   clu   ++   

原文地址:https://www.cnblogs.com/SunChuangYu/p/12541747.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!