zoukankan      html  css  js  c++  java
  • Supermarket

    Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    Submit Status

    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.
    意思就是:有n个商品,每个商品都只能在对应天数前卖掉,获得对应profit,不然就不能卖。问最多可以获利多少。当然是谁的利润多先卖谁,but一天只能卖一个东西,就有的东西不能卖,profit就得不到,怎么判断是否可以得到这个profit呢,并查集专题,肯定并查集。最开始第一个profit肯定是可以得到的,把当前时间点的根节点置为前一天,如此下去,如果压缩之后是0,说明这个时间点之前的天数都已经卖出去过东西了,这个东西就不能卖了。profit就得不到。。
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    
    using namespace std;
    
    #define N 100005
    
    int f[N];
    
    struct point
    {
        int p, d;
    }P[N];
    
    bool cmp(point a, point b)
    {
        return a.p > b.p;
    }
    int found(int x)
    {
        if(f[x] == -1)
            return x;
        return f[x] = found(f[x]);  
    }
    int main()
    {
        int n;
    
        while(cin >> n)
        {
            memset(f, -1, sizeof(f));
    
            for(int i = 0; i < n; i++)
                cin >> P[i].p >> P[i].d;
    
            sort(P, P+n, cmp);
    
            int sum  = 0;
    
            for(int j = 0; j < n; j++)
            {
                int t = found(P[j].d);  //  寻找这个时间点之前最晚哪一天没有卖东西
    
                if(t > 0)  // 如果时间==0,说明这个东西限制天数之前都已经卖出去比它还值钱的东西了,这个东西profit就得不到
                {
                    sum += P[j].p;
                    f[t] = t-1;  // 如果这个东西可以卖出去,就把它根节点置为前一天,说明前一天还可以卖东西,这一天不能再卖了
                }
            }
            cout << sum << endl;
        }
        return 0;
    }
    让未来到来 让过去过去
  • 相关阅读:
    JavaScript 简介
    数据库的链接语句
    sqlserver 大杂烩
    数据库操作
    Filter Big Picture
    struts2配置有命名空间的Action: 解决No configuration found for the specified action错误
    Firefox的Firebug调试
    jQuery判断元素显示或隐藏, is 函数
    Jsp Tag
    java revisit
  • 原文地址:https://www.cnblogs.com/Tinamei/p/4678070.html
Copyright © 2011-2022 走看看