BNUOJ 33535 Final Exam Arrangement

In Zhejiang University, there are N different courses labeled from 1 to N. Each course has its own time slot during the week. We can represent the time slot of a course by an left-closed right-open interval [s, t).

Now we are going to arrange the final exam time of all the courses.

The final exam period will contain multiple days. In each day, multiple final exams will be held simultaneously. If two courses‘ time slots are not overlapped, there may be students who are attending both of them, so we cannot arrange their final exams at the same day.

Now you‘re to arrange the final exam period, to make the total days as small as possible.

Input

There are multiple test cases separated by blank lines.

For each ease, the 1st line contains one integer N(1<=N<=100000).

Then N lines, the i+1th line contains s and t of the interval [s, t) for the ith course.(0<=s<t<=231-1)

There is a blank line after each test case.

Output

For each case, the 1st line contains the days P in the shortest final exam period.

Next P lines, the i+1th line contains the numbers of courses whose final exam is arranged on the ith day separated by one space.

Output a blank line after each test case.

Sample Input

4
0 1
1 2
2 3
3 4

4
0 2
1 3
2 4
3 5

4
0 4
1 5
2 4
3 6

Sample Output

4
1
2
3
4

2
1 2
3 4

1
1 2 3 4

Source

ZOJ Monthly, June 2013

Author

CUI,Tianyi

【题意】:

  就是说一个课程有l,r,其范围是【l,r);如果课程与一个集合里面的所有元素都存在交集,就可以丢到一个集合里面

 问最少要几个集合?

【思路】:

  先按照l升序排序,再按照r降序排序,然后按照一个一个去取,那么每次取的时候更新temp的l,r。r取min值,l取max值,

就是每次能找到的最小集合区间。如果不能满足,那么久更新temp的值为新的区间,再往下寻找。这样最后就能统计出结果

  为什么这样贪心具有合理性呢?

假设有一个区间,那么我们取出符合区间的一段l最小,r最大的去寻找,寻找出来的子集有可能不是最多的,但是是合理的。如果

你去寻找最多的话,那么肯定不在这个区间,那么这个区间就得另外独立出来,一样的就会多出一个集合,那么这样分类都是无所谓的

我们只需要,选择l最小r最大这样一直下去,所有的子状态肯定都是满足的

附上代码

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
/**
简直有毒,行末空格wa一天
 **/
using namespace std;
const int MAXN = 100000 + 5;
typedef long long ll;
inline void Read(ll &x)
{
    x=0;
    static char ch=getchar();
    while (ch<‘0‘||ch>‘9‘)
        ch=getchar();
    while (ch>=‘0‘&&ch<=‘9‘)
    {
        x=x*10+ch-‘0‘;
        ch=getchar();
    }
}
typedef  struct NODE
{
    int num;
    ll l, r;
}node;
node arr[MAXN];
inline bool cmp(const node &a, const node &b)
{
    if(a.l != b.l)
    {
        return a.l < b.l;
    }
    else
    {
        return a.r > b.r;
    }
    return -1;
}
inline bool judge(const node &a, const node &b)
{
    if(b.l < a.r)
        return true;
    else
        return false;
}
vector<int>vec[MAXN];
int n;
int main()
{
    //freopen("test.in"  ,"r",stdin);
    //freopen("test1.out","w",stdout);
    int sum, num;
    while(~scanf("%d", &n))
    {
        sum = num = 0;
        for(int i = 0; i < n; i ++)
        {
            vec[i].clear();
        }
        memset(arr, 0, sizeof(arr));
        for(int i = 0; i < n; i ++)
        {
            //scanf("%lld%lld", &arr[i].l, &arr[i].r);
            Read(arr[i].l);
            Read(arr[i].r);
            arr[i].num = i + 1;
        }
        sort(arr, arr + n, cmp);
        node temp = arr[0];
        int num, sum;
        num = sum = 0;
        for(int i = 0; i < n; i ++)
        {
            if(judge(temp, arr[i]))
            {
                temp.l = max(temp.l, arr[i].l);
                temp.r = min(temp.r, arr[i].r);
                vec[num].push_back(arr[i].num);
            }
            else
            {
                temp = arr[i];
                sum ++;
                num ++;
                vec[num].push_back(arr[i].num);
            }
        }
        printf("%d\n", sum + 1);
        for(int i = 0; i < num + 1; i ++)
        {
            for(int j = 0; j < vec[i].size(); j ++)
            {
                if(j != vec[i].size() - 1)
                printf("%d ", vec[i][j]);
                else
                    printf("%d", vec[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/qq136155330/p/10201159.html

时间: 2024-11-05 21:47:49

BNUOJ 33535 Final Exam Arrangement的相关文章

Final Exam Arrangement

题目链接 题意: 输入n个左闭右开的线段,如果两个线段有重叠部分,那么这两个线段必然不能在一组.求,最少分几组,并且输出每组都有谁 分析: 将一个线段拆开成左右端点,排序.从左向右扫描,如果遇到的是左端点,那么直接加入到集合中,此时集合中的这些线段两两有重合部分,所以是可以分到同一组的:如果遇到的是右端点,那么当前线段之后将和当前线段没有重合点,必然不能放到一组.这样贪心的将每一个线段尽可能的分到一个组中(分到哪个组无所谓,只要分到了一个组中,答案就能减少),就可以保证答案是最少的.当一个线段不

Math 353/DSC 301, Spring 2019, Final Exam

Math 353/DSC 301, Spring 2019, Final ExamPlease remember to write down your name and student ID. This is the take-home portion whichhas four problems. Only independently-finished and fully-justified answer will receive full credit.1. [11 = 3 + 2 + 4

STA 471 Final Exam

STA 471 Due: 5/15/2019Final ExamWhen compiling your answers to the following questions, follow all guidelines for homeworkassignments listed in the syllabus. A hard copy of your work is to be turned in to my office(Kimball 810) by 5:00 PM on the due

ECON4016 - FINAL EXAM

ECON4016 - FINAL EXAMThe final exam consists 4 small projects. You can choose 2 of them to finish and send meyour report. For each of the small projects you choose, you should perform data analysisusing the data I provide to you and the techniques we

2019 Multi-University Training Contest 7 Kejin Player Final Exam

Kejin Player 期望DP 题意: 初始等级为1,每一级有四个参数 r , s , x , a . 每一级有一个概率p=r/s花费a的代价升级到下一级,失败可能会倒退到x级 设从 l 到 r 的期望为 g(l, r), 这种期望满足减法 g(l, r) = g(1, r) − g(1, l). 因为升级只能一级一 级升, 所以要从 1 升级到 r, 必然要经过 l. 求一个前缀和 sum[i+1]=sum[i]+ai * si / ri + (sum[i]-sum[x])*(si-ri)

oj--cs10116 final exam

http://cs101.openjudge.cn/practice/12560/ 样例的结果是对的,但是WA,还没找到原因 #include<cstdio> int map[105][105]; bool issur[105][105]; int main(){ int n,m; scanf("%d %d",&n,&m); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ scanf("%d&

有感于Java Final Exam

秋季学期的java已经结课了,当看到教务平台贴出的通知说考试形式为单选题时,心中喜忧参半. 可喜的是这种考试形式还是比较得民心,毕竟除了判断题,最好做的也就是单选题了. 然而期中考试选择题50%的命中率还历历在目,不免心生忧虑.然后后来的事实证明,果然我的忧虑不是毫无道理的,再次被虐,而且被虐的毫无怨言. 考试完后把还有印象的几道题目在eclipse上跑了跑,结果也是不堪回首.... 然后我决定把它们记下来,以期在不远的将来能够一雪前耻. #1.对,你没有看错,就是这道题,我当时靠着卓越的逆向思

Algorithm lesson final exam

1.algorithm analysis O B/W/AV/AMOR,混入其他问题,设计+分析 2.传统算法(肯定要考) 1)divide and conquer master therem. recursive tree.分析复杂度.递归树加起来得到最终结果 2)greedy algorithm example sort->select 拟阵 独立系统的贪心模板,直接得到近似比 3)dynamic programming sub-optimal structrue.编辑距离 3.graph a

BNUOJ 1268 PIGS

PIGS Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID: 114964-bit integer IO format: %lld      Java class name: Main Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pig