Supermarket ——贪心(并查集优化)

题目链接

题意:

给你n个商品,每个商品都有两个参数 p d ,p为该商品卖出后的利润,d表明该商品只能在这个期限之前卖出,一天只能卖出一件商品。

问你这批商品最多能获得多少利润

题解:

贪心!!!

按照利润从大到小排序,如果利润相同就按照期限从大到小排序,这样才能保证在一定期限内卖更多的商品获得更大的利润

排序完成后,枚举每个商品的结束的时间,然后向前暴力(找到离这个期限最近的且可占用的时间),如果当前时间可以卖出即没被vis数组标记,就可以再在当前时间卖出

代码:

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
int vis[maxn];
int n;
struct node
{
    int p,d;
    bool operator < (const node &a)const
    {
        if(p==a.p)return d>a.d;
        return p>a.p;
    }
}a[maxn];
int main()
{
    while(~scanf("%d",&n))
    {
        memset(vis,0,sizeof vis);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i].p,&a[i].d);
        }
        sort(a+1,a+n+1);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            int tmp=a[i].d;
            for(int j=tmp;j>=1;j--)
            {
                if(!vis[j])
                {
                    vis[j]=1;
                    ans+=a[i].p;
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}

对于贪心的情况,可以用并查集进行优化  找到离这个期限最近的且可占用的时间

f【】数组存放当前期限,找到一个商品 f【x】=x  ,那么当前时间可以使用,然后让f【x】=x-1,表示x期限被标记,然后将他的期限x-1(这个时间用过之后,离x时间期限最近的时间就是x-1)如果 f【x】= 0 则表示没有位置了,就不再记录了

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
int f[maxn];
int n;
struct node
{
    int p,d;
    bool operator < (const node &a)const
    {
        if(p==a.p)return d>a.d;
        return p>a.p;
    }
}a[maxn];
int Find(int x)
{
    return f[x]==x?x:f[x]=Find(f[x]);
}
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i<maxn;i++)f[i]=i;

        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i].p,&a[i].d);
        }
        sort(a+1,a+n+1);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            int tmp=Find(a[i].d);
            if(tmp>0)
            {
                f[tmp]=tmp-1;
                ans+=a[i].p;
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}

原文地址:https://www.cnblogs.com/j666/p/11613802.html

时间: 2024-08-19 08:27:02

Supermarket ——贪心(并查集优化)的相关文章

poj 1456 Supermarket (贪心+并查集)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int fa[10010]; struct node { int p; int d; }; struct node a[10010]; bool cmp(node a1,node a2)//利润从大到小 { return a1.p>a2.p; } int find(int x) { if(fa[x]

Supermarket poj 1456 贪心+并查集优化

Language: Default Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9512   Accepted: 4096 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

POJ 1456——Supermarket——————【贪心+并查集优化】

Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1456 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

(贪心 + 并查集优化) poj 1456

Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9452   Accepted: 4067 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 int

Supermarket---poj456(贪心并查集优化)

题目链接:http://poj.org/problem?id=1456 题意是现有n个物品,每个物品有一个保质期和一个利润,现在每天只能卖一个商品,问最大的利润是多少,商品如果过期了就不能卖了: 暴力的方法: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <stack> #i

poj 1827 A Bunch Of Monsters 贪心(并查集优化)

Description Background Jim is a brave explorer. One day, he set out for his next destination, a mysterious hill. When he arrived at the foot of the hill, he was told that there were a bunch of monsters living in that hill, and was dissuaded from cont

POJ 1456 (贪心+并查集) Supermarket

有n件商品,每件商品有它的利润和售出的最后期限,问能够得到的最大利润是多少 这道题和 HDU 1789 Doing Homework again 几乎一模一样,只不过这个是求最的扣分,本题是求最大利润 朴素的做法是: 按照每件商品的利润从大到小排序,有一个busy数组记录那天是否有东西卖出.对于每件商品,从它的截止日期开始遍历,如果那天有东西卖就看看前一天是否有卖东西,直到有一天没有东西卖或者前面的天数都有卖的. 1 //#define LOCAL 2 #include <iostream>

普林斯顿公开课 算法1-10:并查集-优化的快速合并方法

应用 渗透问题 游戏中会用到. 动态连接 最近共同祖先 等价有限状态机 物理学Hoshen-Kopelman算法:就是对网格中的像素进行分块 Hinley-Milner多态类型推断 Kruskai最小生成树 Fortran等价语句编译 形态学开闭属性 Matlab中关于图像处理的bwlabel函数 渗透问题 一个N×N的矩阵,判断顶部和底部是否连通就是渗透问题. 下图中左侧的矩阵能渗透,右侧矩阵不能渗透. 渗透问题在电学.流体力学.社会交际中都有应用. 在游戏中可能需要生成一张地图,但是作为地图

贪心 + 并查集 之 CODE[VS] 1069 关押罪犯 2010年NOIP全国联赛提高组

/* 贪心 + 并查集 之 CODE[VS] 1069 关押罪犯  2010年NOIP全国联赛提高组 两座监狱,M组罪犯冲突,目标:第一个冲突事件的影响力最小. 依据冲突大小,将M组罪犯按从大到小排序,按照排序结果,依次把每组罪犯分开放入两个监狱, 直到当前这组罪犯已经在同一个监狱中了,此时即为答案. 实现: 1)通过不在同一个监狱的罪犯,推断出在同一个监狱的罪犯.(依据:一共就两个监狱)      ftr[b] = a+n   // a和b是在不同监狱 ftr[c] = a+n   // a和