1456 Supermarket

1456 Supermarket

問題

解答方針

「その日に売れるもので一番高いものを売る」という戦略を,10000日目から1日目まで,逆順に適用していく.

解答例

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            if(n==0) System.out.println("0");
            else{
                Vector<Integer>[]prodset = (Vector<Integer>[])(new Vector<?>[10000]);
                for(int i=0;i<10000;i++) prodset[i] = new Vector<Integer>();
                
                for(int i=0;i<n;i++){
                    int profit = sc.nextInt();
                    int dead = sc.nextInt()-1;
                    prodset[dead].add(profit);
                }
                Solver sol = new Solver(prodset);
                System.out.println(sol.solve());
            }
        }
    }
}

class Solver{
    Vector<Integer>[] prodset;
    
    Solver(Vector<Integer>[] prodset){
        this.prodset = prodset;
    }
    
    public int solve(){
        int sum = 0;
        PriorityQueue<RevInteger> q = new PriorityQueue<RevInteger>();
        for(int i=9999;i>=0;i--){
            if(prodset[i].size()!=0){
                for(int j=0;j<prodset[i].size();j++){
                    q.offer(new RevInteger(prodset[i].get(j)));
                }
            }
            if(!q.isEmpty()){
                sum += q.poll().i;
            }
        }
        return sum;
    }
}

class RevInteger implements Comparable<RevInteger>{
    int i;
    RevInteger(int x){
        i = x;
    }
    public int compareTo(RevInteger r){
        return -(i-r.i);
    }
}

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

最終更新:2006年04月17日 01:42
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。