就是要求灭掉所有怪兽时,受到的最小伤害
听说这叫贪心,我不是很懂 ......... = =!
假设有两只怪A跟B,设他们的ATK和HP分别为a1,h1 和 a2, h2, 可求他们从被打到死的时间(即次数),b1, b2.
先打A怪,再打B怪受到的伤害s1 = b1 * (a1 + a2) + a2 * b2,先打B怪,再打A怪受到的伤害s2 = b2 * (a1 + a2) + a1 * b1
所以先打A怪的条件为s1 > s2 即 b1 * a2 < b2 * a1。
所以对这些怪进行排序,条件就是上面那个公式,然后开始打怪吧。。
#include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #include <cstring> #include <cctype> #include <iostream> #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> using namespace std; #define INIT(a,val) memset(a, val, sizeof(a)) #define MAX(a,b) ((a)>(b)? (a): (b)) #define MIN(a,b) ((a)<(b)? (a): (b)) #define sqr(x) ((x)*(x)) #define INF 100000000 #define EPS 1e-8 #define N 10005 const double pi = acos(-1.0); typedef long long LL; // typedef struct node { int a, h; } node; int n, m; node x[N]; inline bool cmp(const node& n1, const node& n2) { return n1.h * n2.a < n1.a * n2.h; } void solve () { int i, v, u; scanf("%d%d", &n, &m); v = 0; for (i = 0; i < n; ++i) { scanf("%d%d", &x[i].h, &x[i].a); if (x[i].h % m) x[i].h = x[i].h / m + 1; else x[i].h = x[i].h / m; v += x[i].a; } sort(x, x + n, cmp); LL ans = 0; //用int会溢出 u = 0; for (i = 0; i < n; ++i) { ans += (LL) x[i].h * (v - u); u += x[i].a; } printf("%I64d\n", ans); } int main() { #ifndef ONLINE_JUDGE freopen("f://0.in", "r", stdin); freopen("f://0.out", "w", stdout); #endif int cas = 0; int t; scanf("%d", &t); while (t--) { //while (scanf("%d", &n) != EOF) { printf("Case #%d: ", ++cas); solve(); } return 0; }
fzu 2051 Monster (贪心)
最新推荐文章于 2020-08-18 15:18:52 发布