这一题,简单的贪心就OK了。
下面是AC的代码:
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; class data { public: int price, height; }; int M, N; data Date[1005]; int cmp(const data &a, const data &b) { return a.price < b.price; } int main() { int T, i; scanf("%d", &T); while(T--) { scanf("%d%d", &N, &M); for(i = 0; i < M; i++) { scanf("%d%d", &Date[i].price, &Date[i].height); } double ans = 0.0; sort(Date, Date + M, cmp); //价格从小到大排 for(i = 0; i < M; i++) { if(N >= Date[i].price * Date[i].height) { ans += Date[i].height; N = N - Date[i].price * Date[i].height; } else { ans += (N * 1.0) / Date[i].price; break; } } printf("%.2lf\n", ans); } return 0; }
时间: 2024-11-05 16:01:55