1. 程式人生 > >湫湫系列故事——消滅兔子

湫湫系列故事——消滅兔子

while 一個 node ans 定義 ble onos 價格 clas

湫湫系列故事——消滅兔子

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2985 Accepted Submission(s): 984


Problem Description   湫湫減肥
  越減越肥!
  
  最近,減肥失敗的湫湫為發泄心中郁悶,在玩一個消滅免子的遊戲。
  遊戲規則很簡單,用箭殺死免子即可。
  箭是一種消耗品,已知有M種不同類型的箭可以選擇,並且每種箭都會對兔子造成傷害,對應的傷害值分別為Di(1 <= i <= M),每種箭需要一定的QQ幣購買。
  假設每種箭只能使用一次,每只免子也只能被射一次,請計算要消滅地圖上的所有兔子最少需要的QQ幣。

Input 輸入數據有多組,每組數據有四行;
第一行有兩個整數N,M(1 <= N, M <= 100000),分別表示兔子的個數和箭的種類;
第二行有N個正整數,分別表示兔子的血量Bi(1 <= i <= N);
第三行有M個正整數,表示每把箭所能造成的傷害值Di(1 <= i <= M);
第四行有M個正整數,表示每把箭需要花費的QQ幣Pi(1 <= i <= M)。

特別說明:
1、當箭的傷害值大於等於兔子的血量時,就能將兔子殺死;
2、血量Bi,箭的傷害值Di,箭的價格Pi,均小於等於100000。

Output 如果不能殺死所有兔子,請輸出”No”,否則,請輸出最少的QQ幣數,每組輸出一行。

Sample Input 3 3 1 2 3 2 3 4 1 2 3 3 4 1 2 3 1 2 3 4 1 2 3 1

Sample Output 6 4 http://acm.hdu.edu.cn/showproblem.php?pid=4544 這是一道用優先隊列來求解的題,我一開始沒有想到,自定義了一個sort結果時間超限,一直哇,然後仔細想如何優化 才勉強寫出來,自己找bug還找了半天。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4
#include <algorithm> 5 #include <queue> 6 #define N 100005 7 using namespace std; 8 struct Node { 9 int a, b; 10 } node[N]; 11 int m[N]; 12 int x, y; 13 bool cmp(Node x1, Node x2) { 14 return x1.a < x2.a; 15 } 16 priority_queue<int, vector<int>, greater<int> > q; 17 18 int main() { 19 20 while (scanf("%d%d", &x, &y) != EOF) { 21 while (!q.empty()) 22 q.pop(); 23 24 for (int i = 0; i < x; i++) 25 scanf("%d", &m[i]); 26 for (int i = 0; i < y; i++) 27 scanf("%d", &node[i].a); 28 for (int i = 0; i < y; i++) 29 scanf("%d", &node[i].b); 30 31 if (x > y) { 32 printf("No\n"); 33 continue; 34 } 35 sort(m, m + x); 36 sort(node, node + y, cmp); 37 int ans = y - 1; 38 long long int cnt = 0; 39 bool pi = true; 40 for (int i = x - 1; i >= 0; i--) { 41 while (ans >= 0 && node[ans].a >= m[i]) { 42 q.push(node[ans].b); 43 ans--; 44 } 45 if (q.empty()){ 46 pi = false; 47 break; 48 } 49 cnt += q.top(); 50 q.pop(); 51 } 52 if (pi) 53 printf("%lld\n", cnt); 54 else 55 printf("No\n"); 56 } 57 return 0; 58 }

湫湫系列故事——消滅兔子