文章

18

粉丝

0

获赞

39

访问

1.3k

头像
组队刷题 题解:按照每个专题的效率排序,一个一个刷过去就行
P1307
发布于2026年2月10日 18:47
阅读数 63

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;

struct node{
  int m;
  int w;
  double r;//每题所需精力
};
int main(){
  int zjl;//总精力
  int zt;//专题数
  while(cin>>zjl>>zt){
    if(zjl==-1&&zt==-1) break;//!
    vector<node> arr(zt);  // 提前分配 zt 个元素,后面才能用 arr[i]
    int currentM=zjl;
    double resW=0;
    for(int i=0;i<zt;i++){
      cin >> arr[i].w >> arr[i].m;
     
      // 强制把分子或者分母转成 double,避免整型相除被截断
      //两数相除只要有一个是double就可以保留小数
      arr[i].r = (double)arr[i].m / arr[i].w;
    }
    // 按每题消耗精力从小到大排序
    sort(arr.begin(),arr.end(),[](const node &a, const node &b){
      return a.r<b.r;
    });
    
    for(int i=0;i<zt;i++){
      if(currentM<=0)break;//精力用完,退出
      //当前专题所需精力小于剩余精力,直接刷完
      if(arr[i].m<=currentM){
        resW+=arr[i].w;
        currentM-=arr[i].m;
      }
      //剩余精力比当前专题全部所需小,那只能刷一部...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发