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;
}

  

posted @ 2018-03-18 08:48  Xzavieru  阅读(176)  评论(0编辑  收藏  举报