Question 29 (Queue): Whack the Gopher

Description

The great 2320 senior especially likes the game of whack-a-mole. After the game starts, some gophers will appear on the floor. You can use a hammer to hit these gophers. After each gopher is hit, the number of gophers will increase. corresponding game points. However, all gophers only appear on the ground for a short period of time (and never appear again after disappearing), each gopher emerges at time 0, but may stay for a different time, and each gopher is knocked The added game value after hitting may also be different.

Recently, 2320 seniors have played this game so often that it only takes 1 second to tap each gopher. He was thinking about how to tap to maximize the total.

Input

The input consists of 3 lines, the first line contains an integer n (1<=n<=100000) indicating that there are n gophers emerging from the ground, and the second line contains n space-separated integers indicating that each gopher emerges After the stay time (Maxt<=50000), the third line of n integers separated by spaces represents the score v (v<=1000) that each gopher will increase after being tapped. The ith number in each line represents the information of the ith gopher.

Output

an integer representing the maximum total score

Sample Input

5
5 3 6 1 4
7 9 2 1 5

Sample Output

24

data range:

30% of the data guarantee n<=100, t<=500, v<=50

60% of the data guarantee n<=10000, t<=3000, v<=500

100% data guarantee n<=100000, t<=5000, v<=1000

AC CODE:
#include<bits/stdc++.h>
using namespace std;
struct mouse{
    int t,v;
}a[100001];
inline int read(){
    char ch;
    int ret=0;
    while((ch=getchar())<'0'||ch>'9');
    while(ch>='0'&&ch<='9'){
        ret = ret * 10 + (ch-'0 ');
        ch=getchar();
    }
    return ret;
}
int n,nt=0,ans=0;
bool cmp(mouse a,mouse b){
    return a.t<b.t;
}
priority_queue<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].v=read();
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++){
        if(nt<a[i].t){
            nt++;
            ans+=a[i].v;
            q.push(-a[i].v);
        }
        else{
            int tmp=0-q.top();
            if(tmp>=a[i].v) continue;
            ans=ans-tmp+a[i].v;
            q.pop();
            q.push(-a[i].v);
        }
    }
    printf("%d",ans);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324847518&siteId=291194637
29
Recommended