HDU、POJ贪心之旅

1、Doing Homework again(HDU 1789)

活动安排问题
题干: 一共n个作业 完成每个作业都需要一天。但是每个作业有截止日期 如果超过了截止日期 就会扣分 。
给出 n个作业的截止日期 和 如果超过截止日期要扣的分,现在要计算 怎么设计做作业的顺序扣分最少,计算出这个最少的扣分值
贪心策略: 优先让分数大的排在前面,当分数相同时,让截至日期小的排在前面。排完序后,依次完成,当这一天没有被占用,则可以完成,如果,这一天被占用,则提前1天完成,若这一天还是被占用,则在提前,直到找到空闲天,若没找到,则就完不成。
ac代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#include <iomanip>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
bool vis[1005];
struct game
{
    int day, cj;
} data[1005];
bool cmp(game a, game b)
{
    if (a.cj != b.cj)
        return a.cj > b.cj;
    return a.day < b.day;
}
int main()
{
    int t, n;
    scanf("%d", &t);
    while (t--)
    {
        memset(data, 0, sizeof(data));
        memset(vis, 0, sizeof(vis)); //标记天数是否被占用
        scanf("%d", &n);
        int ans = 0;
        for (int i = 1; i <= n; i++)
            scanf("%d", &data[i].day);
        for (int i = 1; i <= n; i++)
            scanf("%d", &data[i].cj);
        sort(data + 1, data + 1 + n, cmp); //结构体排序
        for (int i = 1; i <= n; i++)
        {
            bool flag = 0;
            if (vis[data[i].day] == 0)
                vis[data[i].day] = 1;
            else
            {
                for (int j = data[i].day - 1; j >= 1; j--)
                {
                    if (vis[j] == 0)
                    {
                        flag = 1;
                        vis[j] = 1;
                        break;
                    }
                }
                if (!flag)
                    ans += data[i].cj;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

2、Saruman’s Army POJ 3069

贪心最小覆盖问题
利用优先队列+贪心进行解决
题干: 一个直线上有N个点。点i的距离是Xi。从这些点中选取若干个加上标记。要求:对于每个点,与其距离为R的范围内必有做标记的点(包括自身)。求至少标记多少点才能满足要求。
贪心策略: 先将Xi进入队列中,寻找左右的端点。先找top能到达的最右端tmp,然后找tmp能到达的最右端,这是一个循环。
ac代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#include <iomanip>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
int main()
{
    int r, n, x;
    priority_queue < int , vector<int>, greater<int> > q;
    while (~scanf("%d%d", &r, &n))
    {
        if (r == -1 && n == -1)
            break;
        while (!q.empty())
            q.pop();
        while (n--)
        {
            scanf("%d", &x);
            q.push(x);
        }
        int ans = 0, tmp;
        while (q.size() > 0)
        {
            int mid = q.top();
            while (q.size() > 0) //L
            {
                if (mid + r >= q.top())
                {
                    tmp = q.top();
                    q.pop();
                }
                else
                {
                    mid = tmp;
                    break;
                }
            }
            while (q.size() > 0) //R
            {
                if (mid + r >= q.top())
                    q.pop();
                else
                    break;
            }
            ans++;
        }
        printf("%d\n", ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值