zoukankan      html  css  js  c++  java
  • 外卖的撕‘哔’大战 CSU 1559

    Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu
     

    Description

    “订外卖就上XXX,满X减Y,满X减Y...”这样的声音老回荡在我们耳旁。发传单,拉条幅的宣传手段也屡见不鲜。外卖的撕‘哔’大战充满血雨腥风,不过作为消费者,我们的问题是:“已知N种类似满X减Y的优惠,请问你想点M次外卖,最少出多少钱呢?”。(P.S:各优惠不能叠加,外卖不能拼单拆单。)
     

    Input

    多组数据,第一行有一个整数T,表示有T组数据。(T<=100

    以下每组数据第一行有两个整数N和M,表示外卖网站的优惠种数和你想点的外卖个数。(1<=N,M<=100) 

    然后接下来N行,每行两个整数ai,bi,表示一种优惠为满ai元可减bi元。(ai>=bi)

     最后一行是M个整数,表示你每次点的外卖的价格。

    所有的数据不会超过int。

    Output

    每组数据输出一行,为一个整数,是你在所有外卖上的花销。
     
    Sample Input
    2
    3 3
    5 3
    10 6
    20 8
    5 10 20
    3 3
    5 5
    10 10
    20 20
    6 10 20

    Sample Output

    18
    1

    题解:先排序然后暴力 ,struct,建立两个数组a,b,通过b进行从大到小排序,外卖价格从小到大排序,取最大优惠。

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;

     struct t    
    {
      int a,b;//两个数组
    }

    sale[110];

    bool cmp(t x,t y)
    {
      return x.b>y.b;
    }

    int price[110];

    int main()
    {
      int t;
      scanf("%d",&t);
    while(t--)
    {
      int n,m;
      scanf("%d%d",&n,&m);

    for(int i=0;i<n;i++)
      scanf("%d%d",&sale[i].a,&sale[i].b);

    int before=0; //统计没优惠时的总金额
     for(int i=0;i<m;i++)
    {
      scanf("%d",&price[i]);
      before+=price[i];
    }

     sort(sale,sale+n,cmp);
     sort(price,price+m);

       int after=0; //统计优惠的总金额
      for(int i=m-1;i>=0;i--)
    {
      for(int j=0;j<n;j++)
      if(price[i]>=sale[j].a) { after+=sale[j].b; break; } // k是一个小小的优化
    }

      printf("%d ",before-after);
    }
    }

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    struct t
    {
        int a,b;
    }
    sale[110];
    
    bool cmp(t x,t y)
    {
        return x.b>y.b;
    }
    
    int price[110];
    
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int n,m;
            scanf("%d%d",&n,&m);
    
            for(int i=0;i<n;i++)
                scanf("%d%d",&sale[i].a,&sale[i].b);
    
            int before=0;   //统计没优惠时的总钱数
            for(int i=0;i<m;i++)
               {
                    scanf("%d",&price[i]);
                    before+=price[i];
               }
    
            sort(sale,sale+n,cmp);
            sort(price,price+m);
    
            int after=0;  //统计能优惠的总金额
            
            for(int i=m-1;i>=0;i--)
            {
                for(int j=0;j<n;j++)
                    if(price[i]>=sale[j].a)  {  after+=sale[j].b;  break; }   
            }
    
            printf("%d
    ",before-after);
        }
    }


  • 相关阅读:
    英语4月测试题
    Hadoop---集群的时间同步
    Hadoop---日志服务器
    Hadoop---桥接集群的搭建
    HDFS
    虚拟机类加载机制
    Hadoop---集群的搭建(仅主机模式)
    YARN
    垃圾收集算法和垃圾收集器
    Hadoop---HDFS读写流程
  • 原文地址:https://www.cnblogs.com/hfc-xx/p/4655044.html
Copyright © 2011-2022 走看看