1 条题解

  • 0
    @ 2025-9-9 23:45:56

    C++ :

    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    typedef struct tagSupply
    {
    	int m_price;
    	int m_amount;
    } Supply;
    
    int main()
    {
    	int comp(const Supply& a, const Supply& b);
    	int n = 0, m = 0;
    	int nSum = 0;
    	Supply* p = NULL;
    
    	cin >>n >>m;
    	p = new Supply[m];
    	for (int i = 0; i < m; ++i)
    	{
    		cin >>p[i].m_price >>p[i].m_amount;
    	}
    	sort(p, p+m, comp);
    	
    	int i = 0;
    	while (n > 0)
    	{
    		if (n >= p[i].m_amount)
    		{
    			nSum += p[i].m_amount * p[i].m_price;
    			n -= p[i].m_amount;
    		}
    		else
    		{
    			nSum += n * p[i].m_price;
    			n = 0;
    		}
    		++i;
    	}
    	
    	cout <<nSum;
    	
    	delete []p;
    	return 0;
    }
    
    int comp(const Supply& a, const Supply& b)
    {
    	return a.m_price < b.m_price;
    }
    
    • 1

    信息

    ID
    816
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者