贪心,普通贪心两层循环TLE了,然后用优先级队列维护内层。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <queue> 6 #include <algorithm> 7 using namespace std; 8 9 #define MAXN 100005 10 11 typedef struct arrow_t { 12 int p, d; 13 bool operator < (const arrow_t &x) const { 14 return p > x.p; 15 } 16 } arrow_t; 17 18 int B[MAXN]; 19 arrow_t arrows[MAXN]; 20 int n, m; 21 22 bool comp(arrow_t a, arrow_t b) { 23 return a.d < b.d; 24 } 25 26 int main() { 27 int i, j, k; 28 __int64 ans; 29 arrow_t arr; 30 bool flag; 31 32 #ifndef ONLINE_JUDGE 33 freopen("data.in", "r", stdin); 34 #endif 35 36 while (scanf("%d %d", &n, &m) != EOF) { 37 for (i=0; i<n; ++i) 38 scanf("%d", &B[i]); 39 for (i=0; i<m; ++i) 40 scanf("%d", &arrows[i].d); 41 for (i=0; i<m; ++i) 42 scanf("%d", &arrows[i].p); 43 44 sort(B, B+n); 45 sort(arrows, arrows+m, comp); 46 ans = 0; 47 priority_queue<arrow_t> Q; 48 k = m-1; 49 flag = true; 50 for (i=n-1; i>=0; --i) { 51 while (k>=0 && arrows[k].d>=B[i]) { 52 Q.push(arrows[k]); 53 --k; 54 } 55 if (Q.empty()) { 56 flag = false; 57 break; 58 } 59 arr = Q.top(); 60 Q.pop(); 61 ans += arr.p; 62 } 63 64 if (flag) { 65 printf("%I64d\n", ans); 66 } else { 67 printf("No\n"); 68 } 69 } 70 71 return 0; 72 }
时间: 2024-11-09 00:38:15