zoukankan      html  css  js  c++  java
  • Supermarket POJ

    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. 

    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.
     
     
    题目:每个物品有一个价值px,保质期dx,在这保质期之前都可以出售。一天只能出售一个物品,问出售这些物品价值总和最大是多少。
    思路:我们可以把物品按照价值排序,我们知道,这题可以贪心,从最大的价值开始判断可不可以取。我们需要快速查询该物品保质期之前最近的哪一天,在那天购买该物品。我们可以用并查集来进行查询操作,我们进行fa[i] = i。
    rt = Find(i)
    ① fa[rt] != 0 则说明前面有一天没有买东西,则我们把fa[rt] = rt - 1,说明我们把那天使用。这样我们也可以串联起零散的并查集树。
    ② fa[rt] == 0,说明前面所有天都被使用,则无法购买该物品。
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <queue>
     5 #include <vector>
     6 #include <cmath>
     7 
     8 using namespace std;
     9 
    10 #define ll long long
    11 #define pb push_back
    12 #define fi first
    13 #define se second
    14 
    15 const int N = 10010;
    16 struct node
    17 {
    18     int id, v;
    19 
    20     bool friend operator<(const node& a, const node& b){
    21         return a.v > b.v;
    22     }
    23 };
    24 int fa[N];
    25 
    26 int Find(int x){
    27     return fa[x] == x ? x : fa[x] = Find(fa[x]);
    28 }
    29 
    30 void solve()
    31 {   
    32     int n;
    33     while(~scanf("%d", &n)){ 
    34         vector<node > vn;
    35         
    36         for(int i = 1; i <= n; ++i){
    37             int id, v;
    38             scanf("%d%d", &v, &id);
    39             vn.pb({id, v});
    40         }
    41 
    42         sort(vn.begin(), vn.end());
    43         // for(auto vv : vn) cout << vv.v << " ";
    44         // cout << endl;
    45         for(int i = 0; i < N; ++i) fa[i] = i;
    46 
    47         int profits = 0;
    48         for(int i = 0; i < n; ++i){
    49             int rt = Find(vn[i].id);    
    50             if(rt != 0){
    51                 profits += vn[i].v;
    52                 fa[rt] = rt - 1;
    53             }
    54         }
    55 
    56         //printf("profits = %d
    ", profits);
    57         printf("%d
    ", profits);
    58     }
    59 }
    60 
    61 int main()
    62 {
    63 
    64     solve();
    65 
    66     return 0;
    67 }
  • 相关阅读:
    Bug测试报告--在线考试系统--金州勇士
    Bug测试报告--食物链教学工具--奋斗吧兄弟
    Jquery对象和dom对象获取html的方法
    mysql中常见的存储引擎和索引类型
    转:spring MVC HTTP406 Not Acceptable
    Mybatis动态建表
    ssm框架插入mysql数据库中文乱码问题解决
    Maven环境下Poi的使用
    【转】Mybatis传多个参数(三种解决方案)
    【译文】用Spring Cloud和Docker搭建微服务平台
  • 原文地址:https://www.cnblogs.com/SSummerZzz/p/13276663.html
Copyright © 2011-2022 走看看