#include <stdio.h> #define MAX_BEA (30000000*32) #define MAX_SIZE 10000 typedef struct _GEM { int price, beauti; double efficience; }GEM; typedef struct _BRANCHBOUND { int level, price, beauti, bound; }BB; BB heap[MAX_SIZE]; int heapSize = 0; int TC, N, gemCnt, X, K, have; GEM gem[32]; int calcBound( int level, int price, int b) { for (level; level < N && b+ gem[level].beauti <= X; level++) { price += gem[level].price; b += gem[level].beauti; } if (level < N && b < X && b + gem[level].beauti >= X) { price += (X - b) / gem[level].efficience; b = X; } if (b < X) return -1; return price; } void Swap( int i, int j) { BB temp; temp.beauti = heap[i].beauti; temp.price = heap[i].price; temp.bound = heap[i].bound; temp.level = heap[i].level; heap[i].beauti = heap[j].beauti; heap[i].price = heap[j].price; heap[i].bound = heap[j].bound; heap[i].level = heap[j].level; heap[j].beauti = temp.beauti; heap[j].price = temp.price; heap[j].bound = temp.bound; heap[j].level = temp.level; } void heapPush(BB branch) { heap[heapSize].level = branch.level; heap[heapSize].beauti = branch.beauti; heap[heapSize].bound = branch.bound; heap[heapSize].price = branch.price; int current = heapSize; while (current > 0 && heap[current].bound < heap[(current - 1) / 2].bound) { Swap(current, (current - 1) / 2); current = (current - 1) / 2; } heapSize = heapSize + 1; } void heapPop(BB* pop) { if (heapSize <= 0) { return ; } pop->beauti = heap[0].beauti; pop->bound = heap[0].bound; pop->level = heap[0].level; pop->price = heap[0].price; heapSize = heapSize - 1; heap[0] = heap[heapSize]; int current = 0; while (current * 2 + 1 < heapSize) { int child; if (current * 2 + 2 == heapSize) { child = current * 2 + 1; } else { child = heap[current * 2 + 1].bound < heap[current * 2 + 2].bound ? current * 2 + 1 : current * 2 + 2; } if (heap[current].bound < heap[child].bound) { break ; } Swap(child, current); current = child; } } void gemSwap( int i, int j) { GEM temp; temp.beauti = gem[i].beauti; temp.price = gem[i].price; temp.efficience = gem[i].efficience; gem[i].beauti = gem[j].beauti; gem[i].price = gem[j].price; gem[i].efficience = gem[j].efficience; gem[j].beauti = temp.beauti; gem[j].price = temp.price; gem[j].efficience = temp.efficience; } void init() { heapSize = 0; gemCnt = 0; have = 0; scanf ( "%d" , &N); for ( int i = 0; i < N; i++) scanf ( "%d" , &gem[i].price); for ( int i = 0; i < N; i++) { scanf ( "%d" , &gem[i].beauti); gem[i].efficience = gem[i].beauti / ( double )gem[i].price; } scanf ( "%d%d" , &X, &K); for ( int i = 0; i < K; i++) { int temp; scanf ( "%d" , &temp); have += gem[temp - 1].price; } for ( int i = 0; i < N - 1; i++) { double max = 0; int maxIdx = 0; for ( int j = i; j < N; j++) { if (gem[j].efficience > max) { max = gem[j].efficience; maxIdx = j; } } gemSwap(i, maxIdx); } } int main() { scanf ( "%d" , &TC); for ( int tc = 1; tc <= TC; tc++) { init(); BB current, add, nonAdd; current.beauti = 0; current.level = 0; current.price = 0; current.bound = calcBound(0, 0, 0); heapPush(current); int min = MAX_BEA; if (current.bound == -1) { heapSize = 0; min = -1; } while (heapSize) { heapPop(¤t); if (current.bound == -1 || current.bound >= min) continue ; if (current.beauti >= X && current.price < min) { min = current.price; continue ; } add.beauti = current.beauti + gem[current.level].beauti; add.level = current.level + 1; add.price = current.price + gem[current.level].price; add.bound = calcBound(add.level, add.price, add.beauti); if (add.bound != -1 && add.bound <min) heapPush(add); nonAdd.beauti = current.beauti; nonAdd.level = current.level + 1; nonAdd.price = current.price; nonAdd.bound = calcBound(nonAdd.level, nonAdd.price, nonAdd.beauti); if (nonAdd.bound != -1 && nonAdd.bound <min) heapPush(nonAdd); } printf ( "#%d " , tc); if (min == -1) printf ( "-1\n" ); else { min -= have; if (min <= 0) min = 0; printf ( "%d\n" , min); } } return 0; } |
||||||||||||
|
||||||||||||
|
카테고리 없음
[H1733] 보석상 홍준
반응형
반응형