Description
A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck's fuel tank. The truck now leaks one unit of fuel every unit of distance it travels.Input
* Line 1: A single integer, NOutput
* Line 1: A single integer giving the minimum number of fuel stops necessary to reach the town. If it is not possible to reach the town, output -1.Sample Input
4 4 4 5 2 11 5 15 10 25 10
Sample Output
2
Hint
INPUT DETAILS:Source
USACO 2005 U S Open Gold#include#include #include #include #define maxn 10010 struct Node { int pos, val; } A[maxn]; bool cmp(Node a, Node b) { return a.pos > b.pos; } int main() { // freopen("stdin.txt", "r", stdin); int N, L, P, i, cnt; while(scanf("%d", &N) == 1) { for(i = 0; i < N; ++i) scanf("%d%d", &A[i].pos, &A[i].val); scanf("%d%d", &L, &P); std::sort(A, A + N, cmp); std::priority_queue pq; cnt = i = 0; while(i < N && P >= L - A[i].pos) pq.push(A[i++].val); while(P < L && !pq.empty()) { ++cnt; P += pq.top(); pq.pop(); while(i < N && P >= L - A[i].pos) pq.push(A[i++].val); } if(P < L) cnt = -1; printf("%d\n", cnt); } return 0; }
無語的WA:
#include#include #include #include #define maxn 10010 struct Node { int pos, val; } A[maxn]; bool cmp(Node a, Node b) { return a.pos > b.pos; } int main() { // freopen("stdin.txt", "r", stdin); int N, L, P, i, cnt; while(scanf("%d", &N) == 1) { for(i = 0; i < N; ++i) scanf("%d%d", &A[i].pos, &A[i].val); scanf("%d%d", &L, &P); std::sort(A, A + N, cmp); std::priority_queue pq; cnt = 0; for(i = 0; P < L && i < N; ++i) { if(L - A[i].pos <= P) { pq.push(A[i].val); } else if(!pq.empty()) { P += pq.top(); pq.pop(); ++cnt; --i; } else break; } // int k = 0; // while(k < N && L - A[k].pos <= P) // pq.push(A[k++].val); // while(P < L && !pq.empty()){ // ++cnt; // P += pq.top(); pq.pop(); // while(k < N && L - A[k].pos <= P) // pq.push(A[k++].val); // } if(P < L) cnt = -1; printf("%d\n", cnt); } return 0; }