zoukankan      html  css  js  c++  java
  • Evanyou Blog 彩带

      题目传送门

    Supermarket
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 15192   Accepted: 6855

    Description

    A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an integral number of time units starting from the moment the sale begins. Each product takes precisely one unit of time for being sold. A selling schedule is an ordered subset of products Sell ≤ Prod such that the selling of each product x∈Sell, according to the ordering of Sell, completes before the deadline dx or just when dx expires. The profit of the selling schedule is Profit(Sell)=Σx∈Sellpx. An optimal selling schedule is a schedule with a maximum profit. 
    For example, consider the products Prod={a,b,c,d} with (pa,da)=(50,2), (pb,db)=(10,1), (pc,dc)=(20,2), and (pd,dd)=(30,1). The possible selling schedules are listed in table 1. For instance, the schedule Sell={d,a} shows that the selling of product d starts at time 0 and ends at time 1, while the selling of product a starts at time 1 and ends at time 2. Each of these products is sold by its deadline. Sell is the optimal schedule and its profit is 80. 

    Write a program that reads sets of products from an input text file and computes the profit of an optimal selling schedule for each set of products. 

    Input

    A set of products starts with an integer 0 <= n <= 10000, which is the number of products in the set, and continues with n pairs pi di of integers, 1 <= pi <= 10000 and 1 <= di <= 10000, that designate the profit and the selling deadline of the i-th product. White spaces can occur freely in input. Input data terminate with an end of file and are guaranteed correct.

    Output

    For each set of products, the program prints on the standard output the profit of an optimal selling schedule for the set. Each result is printed from the beginning of a separate line.

    Sample Input

    4  50 2  10 1   20 2   30 1
    
    7  20 1   2 1   10 3  100 2   8 2
       5 20  50 10
    

    Sample Output

    80
    185

    Hint

    The sample input contains two product sets. The first set encodes the products from table 1. The second set is for 7 products. The profit of an optimal schedule for these products is 185.

      传送门(用堆的解法)  
      分析:首先思考,肯定是先卖价值更高的商品赚得利润更多,那么就可以按照价值先排个续,每次尽可能的将商品晚一点卖;那么问题就在于如何记录已售货的天数,直接开数组什么的肯定是别想了。这里用的方法是并查集,建立一个天数的并查集,每次只要在某一天售出了物品,那么就将它并入前一天的并查集,以方便每一件物品都是尽可能晚售出,且不会有前面的空闲天数剩下。正确性易证。
      Code:
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<iomanip>
    #include<algorithm>
    #define Fi(i,a,b) for(int i=a;i<=b;i++)
    using namespace std;
    const int N=1e4+7;
    int n,fa[N],ans;
    struct Node{int v,d;}a[N];
    bool cmp(Node x,Node y)
    {return x.v>y.v;}
    inline int find(int x)
    {return fa[x]==x?x:fa[x]=find(fa[x]);}
    inline void work()
    {
      Fi(i,1,n)cin>>a[i].v>>a[i].d;
      Fi(i,1,N-1)fa[i]=i;ans=0;
      sort(a+1,a+n+1,cmp);
      Fi(i,1,n){int f=find(a[i].d);if(!f)continue;
        ans+=a[i].v;fa[f]=f-1;}
      cout<<ans<<"
    ";
    }
    int main()
    {
      ios::sync_with_stdio(false);
      while(cin>>n)work();
      return 0;
    }

     

  • 相关阅读:
    uni-app中showModel会阻碍 uni.navigateBack跳转
    vue中使用Bus
    vue中class动态绑定值拼接字符串
    使用moment格式化
    sublimit中智能提示插件的安装
    element-table
    全球十大顶级俱乐部
    java的几种对象(PO,VO,DAO,BO,POJO)解释
    软件工程术语(上)
    职场秘笈:聪明人离职后必做的5件事
  • 原文地址:https://www.cnblogs.com/cytus/p/8998885.html
Copyright © 2011-2022 走看看