2 条题解

  • 0
    @ 2023-7-31 19:19:37

    yasuo👀️

    这题跟采购牛奶那题一样呀666

    #include <iostream>
    #include <algorithm>
    struct node{
        int v,m;
    }a[1005];
    bool yasuo(node x, node y){return x.v<y.v;}
    int main(){
        int t,n,sum=0;std::cin>>t>>n;
        for(int i=0;i<n;i++) std::cin>>a[i].v>>a[i].m;
        std::sort(a,a+n,yasuo);
        for(int i=0;i<n;i++){
            if(a[i].m<=t){
                sum+=a[i].m*a[i].v;t-=a[i].m;}
            else{
                sum+=t*a[i].v;break;}}
        std::cout<<sum;return 0;}
    
    • 0
      @ 2023-7-27 18:26:09

      STL 里的vector

      #include <cstdio>
      #include <vector> // 向量
      #include <algorithm>
      using namespace std;
      
      struct QWQ{
      	int a , b;
      };
      vector <QWQ> qwq; //声明结构体向量
      int m , n , ans;
      
      bool cmp(QWQ a , QWQ b){ //比较
      	return a.a < b.a;
      }
      
      int main(void){
      	scanf("%d%d" , &m , &n);
      	while(n--){
      		int a , b;
      		scanf("%d%d" , &a , &b);
      		qwq.push_back((QWQ){a , b}); // 单价与数量加入qwq
      	}
      	sort(qwq.begin() , qwq.end() , cmp); //排序,单价低的排前面
      	for(int i = 0;i < qwq.size() && m > 0;i++){ //计算价钱,优先去单价低的商店
      		if(m > qwq[i].b){
      			ans += qwq[i].a * qwq[i].b;
      			m -= qwq[i].b;	
      		}
      		else{
      			ans += qwq[i].a * m;
      			m = 0;
      		}
      	}
      	printf("%d\n" , ans);
      }
      
      
      • 1

      信息

      ID
      300
      时间
      1000ms
      内存
      128MiB
      难度
      2
      标签
      递交数
      103
      已通过
      61
      上传者