B3872 [GESP202309 五级] 巧夺大奖 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
贪心:在有限时间内优先考虑奖励最高的游戏,这样获得的增益最大。
做法:按照价值降序排序,如果该游戏当前没有被做(即空闲),那么就去做它(标记为做过),然后加上它的价值。
#include<bits/stdc++.h>
using namespace std;
int ans;
struct game
{
int time, price;
}a[510];
int f[510];
bool cmp(game x, game y)
{
return x.price > y.price;
}
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int n; cin >> n;
for (int i = 1; i <= n; i++)cin >> a[i].time;
for (int i = 1; i <= n; i++)cin >> a[i].price;
sort(a + 1, a + n + 1, cmp);
//先完成价值较大的游戏
for (int i = 1; i <= n; i++)
{
for (int j = a[i].time; j > 0; j--)
{
if (!f[j]) //如果该时间段该游戏没被完成
{
ans += a[i].price; //那就完成它
f[j] = 1; //标记为完成
break;
}
}
}
cout << ans << endl;
return 0;
}