一开始想了很多种贪心的方式,比如按 di 升序、ai 降序、ai−di 降序,结果都被自己 hack 掉了。
于是我们考虑换一种思考方式:按照 di 与 ai 的大小关系来分 2 种情况贪心。
若 ai≥di,那么就说明打败这些怪物生命值就会增加,按照 di 升序来打这些怪物就一定是最优的;
若 ai<di,说明打败这些怪物生命值会降低,此时我们按照 ai 降序来打是最理想的,因为我们肯定是先打回血最多的怪物方案最优。
这道题就做完了。
#include <bits/stdc++.h>
#define DEBUG fprintf(stderr, "Passing [%s] line %d\n", __FUNCTION__, __LINE__)
#define File(x) freopen(x".in","r",stdin); freopen(x".out","w",stdout)
using namespace std;
typedef long long LL;
typedef pair <int, int> PII;
typedef pair <int, PII> PIII;
inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f * x;
}
inline LL gl()
{
LL f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f * x;
}
const int INF = 0x3f3f3f3f, N = 1000003;
LL n, m, tot1, tot2;
LL z;
struct Monster
{
LL d, a, id;
} x[N], y[N];
vector <LL> ansid;
inline bool cmp(Monster x, Monster y)
{
return x.a > y.a;
}
inline bool cmp1(Monster x, Monster y)
{
return x.d < y.d;
}
int main()
{
//File("");
n = gl(), z = gl();
for (int i = 1; i <= n; i+=1)
{
LL d = gl(), a = gi();
if (d <= a) x[++tot1] = (Monster){d, a, i};
else y[++tot2] = (Monster){d, a, i};
}
sort(x + 1, x + 1 + tot1, cmp1);
sort(y + 1, y + 1 + tot2, cmp);
for (int i = 1; i <= tot1; i+=1)
if (z <= x[i].d) {puts("NIE"); return 0;}
else z += x[i].a - x[i].d, ansid.push_back(x[i].id);
for (int i = 1; i <= tot2; i+=1)
if (z <= y[i].d) {puts("NIE"); return 0;}
else z += y[i].a - y[i].d, ansid.push_back(y[i].id);
puts("TAK");
for (auto oo : ansid) printf("%lld ", oo);
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 本地部署 DeepSeek:小白也能轻松搞定!
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 从 Windows Forms 到微服务的经验教训
· 李飞飞的50美金比肩DeepSeek把CEO忽悠瘸了,倒霉的却是程序员
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee