51 nod 1163 最高奖励

 

              orz网上大神并查集做法...之前确实想到类似的想法,不过复杂度有点高= =!,看了题解,给跪了!!

            (不过网上好多并查集的代码有点错误,题目中数据范围是1e9,可能大神们看错了,没处理下?

             在我这里用min(总的活动数量,当前所需的时间)预处理了下。

             不多说,上代码orz

 

           

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5+100;

struct fuck {
int val;
int endtime;
}cnm[maxn];
int fa[maxn];
bool cmp(fuck a,fuck b)
{
return a.val>b.val;
}
int fin(int x)
{
if(x<1)return -1;
if(x==fa[x])return fa[x]=x-1;
else return fa[x]=fin(fa[x]);
}
int main() {
int n;
scanf("%d",&n);
for(int i = 1;i<=n;i++)
{
scanf("%d%d",&cnm[i].endtime,&cnm[i].val);
cnm[i].endtime=min(n,cnm[i].endtime);
fa[i]=i;
}
sort(cnm+1,cnm+1+n,cmp);
long long int res = 0;
for(int i = 1;i<=n;i++)
{
int t = cnm[i].endtime;
if(fin(t)>=0)
{
//printf("%d\n",cnm[i].val);
res+=cnm[i].val;
}
}
printf("%lld\n",res);
return 0;
}

 (2)还有一种做法是用优先队列,蛮好理解的,直接上代码了

        

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#pragma warning(disable:4996)
using namespace std;

int n;
struct no
{
int val;
int end_time;

friend bool operator<(no n1, no n2)
{
return n1.val < n2.val;
}
}node[50005];

bool cmp(no n1, no n2)
{
if (n1.end_time == n2.end_time)
return n1.val > n2.val;
else
return n1.end_time > n2.end_time;
}

int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout);

int i, j, temp1, temp2;
long long res;
priority_queue<no>q;

scanf("%d", &n);

for (i = 0; i < n; i++)
{
scanf("%d%d", &temp1, &temp2);
node[i].end_time = min(n, temp1);
node[i].val = temp2;
}
sort(node, node + n, cmp);

res = 0;
j = 0;
for (i = n; i >= 1; i--)
{
while (node[j].end_time >= i&&j < n)
{
q.push(node[j]);
j++;
}
if (!q.empty())
{
no ntemp = q.top();
q.pop();

res = res + ntemp.val;
}
}
printf("%lld\n", res);
//system("pause");
return 0;
}

  

转载于:https://www.cnblogs.com/DreamKill/p/8594183.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值