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

题目链接:http://poj.org/problem?id=1456

题意是现有n个物品,每个物品有一个保质期和一个利润,现在每天只能卖一个商品,问最大的利润是多少,商品如果过期了就不能卖了;

暴力的方法:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
#define PI 4*atan(1.0)
#define N 10550
#define met(a, b) memset(a, b, sizeof(a))

struct node
{
    int p, d;
}a[N];
int cmp(node p, node q)
{
    if(p.p != q.p)
        return p.p > q.p;
    return p.d > q.d;
}
int vis[N];

int main()
{
    int n;
    while(scanf("%d", &n)!=EOF)
    {
        met(vis, 0);
        met(a, 0);

        for(int i=1; i<=n; i++)
            scanf("%d %d", &a[i].p, &a[i].d);

        sort(a+1, a+n+1, cmp);

        int ans = 0;
        for(int i=1; i<=n; i++)
        {
            for(int j=a[i].d; j>=1; j--)
            {
                if(!vis[j])
                {
                    ans+=a[i].p;
                    vis[j] = 1;
                    break;
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

并查集优化:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
#define PI 4*atan(1.0)
#define N 10500
#define met(a, b) memset(a, b, sizeof(a))

struct node
{
    int p, d;
}a[N];
int cmp(node p, node q)
{
    if(p.p != q.p)
        return p.p > q.p;
    return p.d > q.d;
}

int f[N];
int Find(int x)
{
    if(f[x]!=x)
        f[x] = Find(f[x]);
    return f[x];
}

int main()
{
    int n;
    while(scanf("%d", &n)!=EOF)
    {
        for(int i=0; i<N; i++)
            f[i] = i;
        met(a, 0);

        for(int i=1; i<=n; i++)
            scanf("%d %d", &a[i].p, &a[i].d);

        sort(a+1, a+n+1, cmp);

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

时间: 2024-10-09 20:14:29

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

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

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 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 ——贪心(并查集优化)

题目链接 题意: 给你n个商品,每个商品都有两个参数 p d ,p为该商品卖出后的利润,d表明该商品只能在这个期限之前卖出,一天只能卖出一件商品. 问你这批商品最多能获得多少利润 题解: 贪心!!! 按照利润从大到小排序,如果利润相同就按照期限从大到小排序,这样才能保证在一定期限内卖更多的商品获得更大的利润 排序完成后,枚举每个商品的结束的时间,然后向前暴力(找到离这个期限最近的且可占用的时间),如果当前时间可以卖出即没被vis数组标记,就可以再在当前时间卖出 代码: #include<iost

POJ 1456 Supermarket(贪心+并查集)

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 precise

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]

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的矩阵,判断顶部和底部是否连通就是渗透问题. 下图中左侧的矩阵能渗透,右侧矩阵不能渗透. 渗透问题在电学.流体力学.社会交际中都有应用. 在游戏中可能需要生成一张地图,但是作为地图