BoxesDiv2——SRM 622 DIV2

1495 단어 topcoderdiv2
import java.util.*;
public class BoxesDiv2{
    public     BoxesDiv2(){}


    public int findSize(int[] candyCounts)
    {
        Arrays.sort(candyCounts);
        for(int i=0;i<candyCounts.length;i++){
            int v = candyCounts[i];
            int j=1;
            while(j<v){
                j<<=1;
            }
            candyCounts[i]=j;
        }
        PriorityQueue<Integer> pq = new PriorityQueue<Integer>(candyCounts.length);
        for(int i=0;i<candyCounts.length;i++){
            pq.add(candyCounts[i]);
        }
        while(!pq.isEmpty()){
            Integer v1 = pq.peek();
            pq.poll();
            if(pq.isEmpty()){
                return v1;
            }
            Integer v2=		pq.peek();
            pq.poll();
            int v3 = v1+v2;
            int j=1;
            while(j<v3){
                j<<=1;
            }
            pq.add(j);
        }
        return 1;
    }
}

아이디어:
상향 2의 지수 멱을 정돈하여
즉 1-> 1;2--》2;3,4--》4;5,6,7,8--》8
(1) 모든 원소를 2의 지수 멱에 따라 정돈한다.
(2) 더미를 사용하여 모든 원소를 더미에 넣는다
(3) 더미에 원소가 하나만 있으면 퇴적하고 더미 꼭대기 원소를 되돌려줍니다
(4) 그렇지 않으면 두 개의 원소를 취하고 덧붙인 후 위로 2의 지수 멱을 취하여 정돈하고 입적한다.
이 단계는 실제로 이 두 수의 큰 원소에 2를 곱하기만 하면 된다.
 
 

좋은 웹페이지 즐겨찾기