ACM学习历程—HihoCoder1309任务分配(排序 && 贪心)

http://hihocoder.com/problemset/problem/1309

题目大意是给定n个任务的起始时间,求问最少需要多少台机器。

有一个贪心的策略就是,如果说对于一个任务结束,必然接一个开始时间最接近这个的比较合算。我们假想一个任务池,那么任务池中最早结束的那个,必然接剩余任务中最早开始的比赛合算(相同开始时间最早结束),而且假设这个任务是P,那么对于一个结束时间晚于P的任务,显然后面的一段时间是浪费的,同样对于一个开始时间晚于P的任务,前面一段是浪费的;

关键在于某个开始时间晚于P,但是结束时间早于P的任务Q,假设接上Q后,还能接一个X,但是接上P后,无法接上X。

首先,对于接P的情况,会导致Q和X,无法接上,或许需要重开一个机器,或者是任务池中存在另一个任务可以接上。对于接Q的情况,会导致P,无法接上,或许需要重开一个机器,或许任务池中存在一个任务可以接上。

对于需要重开一个机器的情况,两者的效果是等价的。那么假设,接Q的情况,任务池中存在另一个任务可以接P,必然,对于接P的情况,存在同样的任务可以接Q,因为P的开始时间早于Q,那么此情况下,两者亦是等价的。

所以,综上,剩余任务中最早开始的比赛合算(相同开始时间最早结束)。

于是只需要开始对所有任务按开始时间正序,次按结束时间正序排序。然后用一个优先队列(堆)维护任务池即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>
#define LL long long

using namespace std;

const int maxN = 100005;
struct node
{
    int s, e;
}a[maxN];

bool cmp(node x, node y)
{
    if (x.s != y.s) return x.s < y.s;
    else return x.e < y.e;
}

int n;

void work()
{
    priority_queue<int, vector<int>, greater<int> > q;
    q.push(0);
    int k;
    for (int i = 0; i < n; ++i)
    {
        k = q.top();
        q.pop();
        if (k <= a[i].s) q.push(a[i].e);
        else q.push(a[i].e), q.push(k);
    }
    printf("%d\n", q.size());
}

int main()
{
    //freopen("test.in", "r", stdin);
    //freopen("test.out", "w", stdout);
    while (scanf("%d", &n) != EOF)
    {
        for (int i = 0; i < n; ++i)
            scanf("%d%d", &a[i].s, &a[i].e);
        sort(a, a+n, cmp);
        work();
    }
    return 0;
}

时间: 2024-11-05 11:16:18

ACM学习历程—HihoCoder1309任务分配(排序 && 贪心)的相关文章

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

ACM学习历程—UESTC 1226 Huatuo&#39;s Medicine(数学)(2015CCPC L)

题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目就是构造一个对称的串,除了中间的那个只有1个,其余的两边都是对称的两个,自然答案就是2*n-1. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #

ACM学习历程—FZU 2144 Shooting Game(计算几何 &amp;&amp; 贪心 &amp;&amp; 排序)

Description Fat brother and Maze are playing a kind of special (hentai) game in the playground. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.) But as they don’t like using repellent while playing this kind of special (hent

ACM学习历程—HDU5265 pog loves szh II(策略 &amp;&amp; 贪心 &amp;&amp; 排序)

Description Pog and Szh are playing games.There is a sequence with $n$ numbers, Pog will choose a number A from the sequence. Szh will choose an another number named B from the rest in the sequence. Then the score will be $(A+B)$ mod $p$.They hope to

ACM学习历程——NOJ1113 Game I(贪心 || 线段树)

Description 尼克发明了这样一个游戏:在一个坐标轴上,有一些圆,这些圆的圆心都在x轴上,现在给定一个x轴上的点,保证该点没有在这些圆内(以及圆上),尼克可以以这个点为圆心做任意大小的圆,他想知道自己做多可以与多少个给定的圆相交(相切也算,包含不算). Input 输入有多组数据 输入到文件尾 每一组数据有一个整数n(1<=n<=100000),表示总共有n个圆. 接下是n行,每行两个整数xi,ri表示该圆的圆心坐标和半径. 接下来一行为一个整数x,表示尼克选取点的位置. x xi的范

ACM学习历程——POJ 2376 Cleaning Shifts(贪心)

Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), t

ACM学习历程—HDU5269 ZYB loves Xor I(位运算 &amp;&amp; dfs &amp;&amp; 排序)(BestCoder Round #44 1002题)

Problem Description Memphis loves xor very musch.Now he gets an array A.The length of A is n.Now he wants to know the sum of all (lowbit(Ai xor Aj) (i,j∈[1,n]) We define that lowbit(x)=2^k,k is the smallest integer satisfied ((x and 2^k)>0)Specially,

ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)

Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● a i ∈ [0,n] ● a i ≠ a j( i ≠ j ) For sequence a and sequence b, the integrating degree t is defined as follows(“?” denotes exclus