zoukankan      html  css  js  c++  java
  • NYOJ 208 Supermarket (模拟+并查集)

    题目链接

    描述

    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.

    输入

    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.

    输出

    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.

    样例输入

    4 50 2 10 1 20 2 30 1

    7 20 1 2 1 10 3 100 2 8 2

    5 20 50 10

    样例输出

    80

    185

    分析:

    给定一些商品本身的价值和保质期,只要在保质期内的任意商品都可以出售,但是如果超过保质期就不能够出售了,要求的就是这些商品说能获得的最大的价值。

    既然要求最大价值肯定就用到了贪心的思想,原先就是只用贪心写,时间超。这还要加入一个并查集,其主要作用就是能够快速的找出当前商品的出售日期(这样比for循环一个一个往下找要快)。

    商品排序的话肯定就是按照价值从达到小排序了,并不用管他的保质期,因为只要把价值最大的商品都出售了,当然也就获得最大的价值了。

    代码:

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    int parent[10009];
    int Max=-1;
    struct Node
    {
        int value;
        int day;
    } node[10009];
    
    bool cmp(Node a,Node b)
    {
        return a.value>b.value;
    }
    
    void init( )///parent数组初始化
    {
        for(int i=0; i<=10005; i++)
            parent[i]=i;
    }
    
    int Find(int x)///找当保质期限为x的物品,应该在第几天卖出
    {
        if(x==parent[x])
            return x;
        else
            return parent[x]=Find(parent[x]);
    }
    int main()
    {
        int n;
        while(~scanf("%d",&n))
        {
            for(int i=0; i<n; i++)
            {
                scanf("%d%d",&node[i].value,&node[i].day);
            }
            sort(node,node+n,cmp);
            init();
            long long int sum=0;
            int a;
            for(int i=0;i<n;i++)
            {
                a=Find(node[i].day);///当前这个物品应该在第几天卖
                {
                    if(a!=0)///它可以在保质期之内卖出去
                    {
                        sum+=node[i].value;
                        parent[a]=Find(a-1);///也就相当于其他的保质期为a的物品不能在第a天卖出去了,要提前一天卖
                    }
                }
            }
            printf("%lld
    ",sum);
        }
        return 0;
    }
  • 相关阅读:
    ASP.Net Core "The type initializer for 'Gdip' threw an exception"
    ERROR 1698 (28000): Access denied for user 'root'@'localhost'
    彻底卸载Xubuntu Kubuntu
    Ubuntu MariaDB PhpMyAdmin
    VMware虚拟机复制后Linux无法上网
    Visual Studio 项目依赖
    Windows 10 关闭Hyper-V
    一个用python写的比特币均线指标
    关于PHP连接上MySQL但不能插入数据
    【原创】关于pyinstaller打包的程序执行出错问题,pyinstaller3.5只支持matplotlib3.0.2已经解决
  • 原文地址:https://www.cnblogs.com/cmmdc/p/6758969.html
Copyright © 2011-2022 走看看