1. 程式人生 > 實用技巧 >一本通 1.1 例 5【智力大沖浪】

一本通 1.1 例 5【智力大沖浪】

題目linkhttps://loj.ac/problem/10004

首先根據貪心,容易得出應該儘可能的不失去扣錢數多的遊戲,因此先按照扣錢數進行排序。隨後從後往前列舉時間,能完成就完成,因為有可能出現扣錢數多的遊戲但時間寬裕、扣錢數相對少但時間緊的情況,因為答案要求最大,所以儘量每個遊戲的時間都向後安排,因為前面的時間是所有遊戲都最可能用的,而後面的時間一定沒有前面的時間更優。$O(n$2$)$

 1 #include <bits/stdc++.h>
 2 #define INF 0x3f3f3f3f
 3 using namespace std;
 4 int n, m, choose[510
]; 5 struct Str {int t, c;}stu[510]; 6 int cmp(Str a, Str b) {return a.c > b.c;} 7 int main() 8 { 9 scanf("%d %d", &m, &n); for(int i = 1; i <= n; ++i) scanf("%d", &stu[i].t); for(int i = 1; i <= n; ++i) scanf("%d", &stu[i].c); 10 sort(stu + 1, stu + n + 1, cmp);
11 for(int i = 1; i <= n; ++i) 12 { 13 int time = stu[i].t; 14 while(choose[time]) --time; 15 if(time == 0) m -= stu[i].c; 16 else choose[time] = 1; 17 } 18 printf("%d", m); 19 return 0; 20 }