Cattle-off practice match 50C-tokitsukaze and Soldier- (+ sorted priority queue)

Links: https://ac.nowcoder.com/acm/problem/50439
Source: Cattle passenger network

in a game, tokitsukaze need to select some of the soldiers in the n soldiers make up a group to play copy.
Combat power of the i-th soldier to v [i], combat power groups are fighting force of all the soldiers in the regiment and.
But these soldiers have a special request: If you choose the i-th soldiers, the soldiers hope the number of groups of no more than s [i]. (If you do not choose the i-th soldier, do not have this limitation.)
Tokitsukaze wondered combat power groups up to much.

Ideas:

The tentatively s [i] is called tolerance.

If you do not s [i] This property, that is simple greed.

For tolerance descending order, from front to back traversal of traversal to the soldiers to see the number of soldiers in the queue is less than his tolerance, if it is greater than pop, pop soldiers pop is certainly low combat capability, reserved high combat capability, so the queue with a priority queue. First traversal definitely better than the tolerance value after traverse to the large values ​​of tolerance, so every year just to see the value of tolerance soldiers, the number of elements in the queue to meet the tolerance value of the current queue of soldiers soldier inevitably meet.

import java.util.*;

public class Main{
    static Node[] a=new Node[100005];
    
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int n=scan.nextInt();
        long maxx=-1;
        long sum=0;
        for(int i=0;i<n;i++) {
            a[i]=new Node();
            a[i].v=scan.nextLong();
            A [I] .s = scan.nextInt (); 
        } 
        Arrays.sort (A, 0, n-, new new Comparator <the Node> () {
             public  int Compare (the Node P1, the Node P2) {
                 return ( int ) (P2. p1.s-S); // array, a large discharge tolerance front 
            } 
        }); 
        PriorityQueue <the Node> Q = new new PriorityQueue <the Node> ( new new Comparator <the Node> () {
             public  int Compare (the Node P1, the Node P2) {
                 return ( int ) (p1.v-p2.v); // combat power to put in front of a small
            }
        }); 
        for(int i=0;i<n;i++) {
            while(q.size()>=a[i].s) {
                sum-=q.poll().v;
            }
            q.add(a[i]);
            sum+=a[i].v;
            maxx=Math.max(maxx, sum);
        }
        System.out.println(maxx);
    }
}

class Node{
    long v;
    int s;
}

 

Guess you like

Origin www.cnblogs.com/shoulinniao/p/12623127.html