首页 > 其他 > 详细

点头(1163)

时间:2014-10-01 22:59:41      阅读:473      评论:0      收藏:0      [点我收藏+]

 

题目链接:here~~~

 

 

本题巧妙运用并查集记录每个点的前驱,更快查找出来目标状态(而且排序更加巧妙)代码某位大牛所写ORZ

bubuko.com,布布扣
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
pair<int,int> p[50005];
int father[50005];
int find(int x){
    if(x <= 0) return -1;
    return father[x] = (x == father[x])? x - 1 : find(father[x]);
}
int main()
{
    int n;
    cin >> n;
    for(int i = 0;i < n; i++){
        cin>>p[i].second>>p[i].first;
        p[i].first = - p[i].first;
        if(p[i].second > n) p[i].second = n;
    }
    for(int i = 0;i <= n;i++) father[i] = i;
    sort(p,p+n);
    long long ans = 0;
    for(int i = 0;i < n;i ++)
    {
        int t = find(p[i].second);
        if(t >= 0) ans -= p[i].first;
    }
    cout<<ans<<endl;
}
View Code

 

点头(1163)

原文:http://www.cnblogs.com/zsj-93/p/4003604.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!