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;
    }

     

  • 相关阅读:
    检索通讯录,根据输入的电话号码的每一位下拉显示检索结果
    获取手机的具体型号 及 iOS版本号
    在iOS中使用ZBar扫描二维码
    iOS沙盒路径的查看和使用
    ios打开通讯录及点击通讯录时提取相关信息
    获取倒计时距离某一时间点的时间,判断身份证,电话号码格式是否正确的简单封装
    iOS 获取手机的型号,系统版本,软件名称,软件版本
    第三天战略会议
    第二天站略会议总结
    第一天站略会议总结
  • 原文地址:https://www.cnblogs.com/cytus/p/8998885.html
Copyright © 2011-2022 走看看