1 条题解

  • 0
    @ 2023-6-11 12:20:59

    C :

    #include<stdio.h>
    int m,n,a[1100][2],ans;
    int main()
    {
    	scanf("%d %d",&m,&n);
    	for(int i=1;i<=n;i++)
    	scanf("%d %d",&a[i][0],&a[i][1]);
    	for(int i=1;i<n;i++)
    	for(int j=i+1;j<=n;j++)
    	if(a[i][0]>a[j][0])
    	{
    		int t1=a[i][0],t2=a[i][1];
    		a[i][0]=a[j][0];
    		a[j][0]=t1;
    		a[i][1]=a[j][1];
    		a[j][1]=t2;
    	}
    	for(int i=1;i<=n&&m>0;i++)
    		if(a[i][1]>=m)
    		{
    			ans+=m*a[i][0];
    			m=0;
    		}
    		else
    		{
    			ans+=a[i][1]*a[i][0];
    			m-=a[i][1];
    		}
    		printf("%d",ans);
    		return 0;
    }
    

    Java :

    
    import java.util.*;
    
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int m, n;
            m = sc.nextInt();
            n = sc.nextInt();
            List<Map<String, Integer>> shopList = new ArrayList<>();
            for (int i = 1; i <= n; i++) {
                Map<String, Integer> shop = new HashMap<>();
                shop.put("price", sc.nextInt());
                shop.put("count", sc.nextInt());
                shopList.add(shop);
            }
            //容器排序
            Collections.sort(shopList, new Comparator<Map<String, Integer>>() {
                @Override
                public int compare(Map<String, Integer> o1, Map<String, Integer> o2) {
                    //如果前一个 price > 后一个 price,则交换这两个map
                    return o1.get("price") - o2.get("price");
                }
            });
            //总共需要购买 m 个贺卡,m 还需要购买的贺卡个数
            int money = 0;
            for (int i = 0; i < shopList.size(); i++) {
                //第 i 间商铺
                Map<String, Integer> shop = shopList.get(i);
                if( shop.get("count") >= m){
                    //不需要这么多了
                    money = money + m * shop.get("price");
                    m = 0;
                    break;
                }else{
                    money = money + shop.get("count") * shop.get("price");
                    m = m - shop.get("count");
                }
            }
            System.out.print(money);
            sc.close();
        }
    }
    
    

    Python :

    '''
    stus = []
    class Student:
        def __init__(self,id,data,ave):
            self.id = id
            self.data= data
            self.ave=ave
    n=int(input())
    for i in range(n):
      t=list(map(int,input().split()))
      x=t[0:1]
      y=t[1:]
      y=sorted(y)
      stus.append(Student(id=x[0],data=y,ave=sum(y[1:4])/3))
    stulist = sorted(stus, key=lambda zz:(-zz.ave, zz.id))
    for i in range(3):
      print("%d %.3lf"%(stulist[i].id,stulist[i].ave))
    
    
    
        
    '''  
    
    
    class Hk:
      def __init__(self,price,num):
        self.price=price
        self.num=num
    hks=[]
    
    total,n=list(map(int,input().split()))
    for i in range(n):
      price,num=list(map(int,input().split()))
      hks.append(Hk(price,num))
    
    hklist = sorted(hks,key=lambda t:(t.price))
    hksum=0
    pricesum=0
    i=0
    
      
    while hksum+hklist[i].num<total:
      hksum=hksum+hklist[i].num
      pricesum=pricesum+hklist[i].price*hklist[i].num
      i=i+1
    else:
      pricesum=pricesum+hklist[i].price*(total-hksum)
    print(pricesum)
     
    
    
    
    • 1

    信息

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