51nod 1163 highest reward

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1163&judgeId=598136

1163 highest reward 

Base time limit: 1 second space limit: 131072 KB score: 20 Difficulty: Level 3 algorithm problem

Collection

Concern

There are N tasks, each task has a latest end time and a corresponding reward. Finish the task before the end time, and you will get the corresponding reward. The time required to complete each task is 1 unit time. Sometimes it's impossible to complete all tasks, because there may be conflicts in time, which requires you to choose between them. Ask for the highest reward you can get.

Input

Line 1: a number N, indicating the number of tasks (2 < = N < = 50000)
Line 2 - N + 1, 2 numbers per line, separated by spaces, indicating the latest end time E [i] of the task and the corresponding reward w [i]. (1 <= E[i] <= 10^9,1 <= W[i] <= 10^9)

Output

Output the highest reward you can get.

Input example

7
4 20
2 60
4 70
3 40
1 30
4 50
6 10

Output example

230

I can see that a has a lot of people. I'll come to the question a.... I didn't expect to get stuck. I'm not familiar with the container. I really want to lose a lot of money... I've been thinking about how to choose the biggest sum when the time is fixed... No way to sort it, because it's not a dynamic one. Look at it on the Internet. The priority queue woc

#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
#define maxn  50005
#define ll long long
using namespace std;
struct node
{
    int time;
    int p;
}ac[maxn];
bool  cmp(node a,node b)
{
    if(a.time==b.time)
        return a.p>b.p;
    return a.time<b.time;
}
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>ac[i].time>>ac[i].p;
    }
   sort(ac,ac+n,cmp);
   ll ans=0;
   priority_queue<int,vector <int> ,greater <int> >que;
   for(int i=0;i<n;i++)
   {
       int q=ac[i].p;
       if(ac[i].time>que.size())
       {
            ans+=q;
            que.push(q);
       }
       else
       {
           ans+=q;
           que.push(q);
           ans-=que.top();
           que.pop();
       }
   }
   cout<<ans<<endl;
    return 0;
}

The priority queue has been written out. Looking at other people's code on the Internet, we finally found a one that doesn't use the priority queue. emmmm actually uses and checks the set. What's more, it's the quality of the priority queue that I didn't think about before I tried to use it, and then it's constantly updated. It's really powerful.

#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
#define maxn  50005
#define ll long long
using namespace std;
struct node
{
    int time;
    int p;
}ac[maxn];
int par[maxn];     //The latest i seconds can be completed in j seconds
bool  cmp(node a,node b)
{
    return a.p>b.p;
}
int find(int x)
{
    if(x<=0) return -1;
    else if(x==par[x])
    {
        par[x]=x-1;
    }
    else  return par[x]=find(par[x]);//Path compression
}
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>ac[i].time>>ac[i].p;
        par[i]=i;
    }
   sort(ac,ac+n,cmp);
   ll ans=0;
   for(int i=0;i<n;i++)
   {
        int t=ac[i].time;
        if(find(t)>=0)
        {
            ans+=ac[i].p;
        }
   }
   cout<<ans<<endl;
    return 0;
}

 

Posted by xterra on Sat, 04 Jan 2020 21:33:06 -0800