Hamster to play games (greedy)

 

Simplification usual meaning of the questions:

0:00 n tasks, each task has a weight and a disappearance time, will be completed before disappearing get value, maximum demand

 

solution:

Konjac actually want to start on dp. . .

Training stuff, I forgot, it seems not digested ah. . .

The last thing YY out a similar positive solutions but I do bounced off ....

First, the complete disappearance of things at each point, and then find the largest completed in idle point.

Looks right ..... (at least hack date have passed)

Then Positive Solutions:

In the words of courseware is concerned, that is:

  • Back in time
  • With a heap maintenance

Much easier to understand. .

First sort

Then stuffed into a large root heap maximum (if not more than time, then disappear)

If you exceed the current time, not exceeding a maximum time before looking into the big pile of roots

Then last statistical answer.

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
inline int read()
{
    int x=0,f=1;char s=getchar();
    while(s>'9'||s<'0'){if(s=='-')f=-1;s=getchar();}
    while(s<='9'&&s>='0'){x=x*10+s-'0';s=getchar();}
    return x*f;
}
struct node
{
    int t,p;
}a[maxn];
 
bool operator<(node a,node b)
{
    return a.t<b.t;
}
int n,now=0,ans;
priority_queue < int , vector < int > , greater < int > > q;
int main()
{
    n=read();
    for(int i=1;i<=n;i++)
    {
        a[i].t=read();
    }
    for(int i=1;i<=n;i++)
    {
        a[i].p=read();
    }
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)
    {
        if(now<a[i].t)
        {
            now++;
            q.push(a[i].p);
            ans+=a[i].p;
        }
        else
        {
            int t=q.top();
            if(t>a[i].p)
            continue;
            else
            {
                ans=ans-t+a[i].p;
                q.pop();
                q.push(a[i].p);
            }
        }
    }
    printf("%d",ans);
    return 0;
}

 

(Finish)

 

Guess you like

Origin www.cnblogs.com/ajmddzp/p/11493389.html