Petya's Exams CodeForces - 978G (贪心)

G. Petya's Exams
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Petya studies at university. The current academic year finishes with nn special days. Petya needs to pass mm exams in those special days. The special days in this problem are numbered from 11 to nn.

There are three values about each exam:

  • sisi — the day, when questions for the ii-th exam will be published,
  • didi — the day of the ii-th exam (si<disi<di),
  • cici — number of days Petya needs to prepare for the ii-th exam. For the ii-th exam Petya should prepare in days between sisi and di1di−1, inclusive.

There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the ii-th exam in day jj, then sij<disi≤j<di.

It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days.

Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible.

Input

The first line contains two integers nn and mm (2n100,1mn)(2≤n≤100,1≤m≤n) — the number of days and the number of exams.

Each of the following mm lines contains three integers sisididicici (1si<din,1cin)(1≤si<di≤n,1≤ci≤n) — the day, when questions for the ii-th exam will be given, the day of the ii-th exam, number of days Petya needs to prepare for the ii-th exam.

Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given.

Output

If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print nn integers, where the jj-th number is:

  • (m+1)(m+1), if the jj-th day is a day of some exam (recall that in each day no more than one exam is conducted),
  • zero, if in the jj-th day Petya will have a rest,
  • ii (1im1≤i≤m), if Petya will prepare for the ii-th exam in the day jj (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it).

    Assume that the exams are numbered in order of appearing in the input, starting from 11.

    If there are multiple schedules, print any of them.

Examples
input
Copy
5 2
1 3 1
1 5 1
output
Copy
1 2 3 0 3 
input
Copy
3 2
1 3 1
1 2 1
output
Copy
-1
input
Copy
10 3
4 7 2
1 10 3
8 9 1
output
Copy
2 2 2 1 1 0 4 3 4 4 
Note

In the first example Petya can, for example, prepare for exam 11 in the first day, prepare for exam 22 in the second day, pass exam 11 in the third day, relax in the fourth day, and pass exam 22 in the fifth day. So, he can prepare and pass all exams.

In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.

题意:现在,有n天,m场考试编号1~m,每场考试给出 s 题库发布的时间(即代表从此时起可以开始复习这门科目)d 这门科目考试的时间,t 需要复习的时间。现在,每天你可以选择复习,或者休息,只能选其中一样!如果是考试的时间,则必须去考试!问,你是否可以复习完所有科目并通过考试,如果可以,输出安排表,1~m代表复习功课,0表示休息,m+1表示考试。如果不能,输出-1/。

思路:了解过贪心的小伙伴肯定知道有这样的一个贪心经典题,给出开始工作和工作结束的时间,求在一段时间内,最多能完成几样工作!这个贪心策略就是先选择工作结束时间早的工作!那么,同理,在这里,我们也优先选择考试结束时间早的科目先复习即可。但是,需要注意的是,复习时间不一定要连续,所以,我们不是一次性取一段区间,而是一天一天的安排!!但是安排策略不变。(我直接取区间wa了一发。。。)

#include "iostream"
#include "algorithm"
using namespace std;
int ans[105];
struct Text//使用结构体记录信息
{
    int Beg,End,time;
    int index;
};
Text text[105];
bool cmp(Text a,Text b)//按结束时间排序
{
    return a.End<b.End;
}
int main()
{
    ios::sync_with_stdio(false);
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        cin>>text[i].Beg>>text[i].End>>text[i].time;
        text[i].index=i;
        ans[text[i].End]=m+1;//如果有考试,直接安排为考试时间
    }
    sort(text+1,text+1+m,cmp);
    for(int i=1;i<=n;i++){
        if(ans[i]) continue;//如果是考试时间,直接跳过
        int flag=0;
        for(int j=1;j<=m;j++){
            if(text[j].time&&text[j].Beg<=i&&i<text[j].End){//判断是否可以安排复习
                ans[i]=text[j].index;
                text[j].time--;
                flag=1;
            }
            if(flag){//安排好这一天后,直接退出
                break;
            }
        }
    }
    for(int i=1;i<=m;i++)
        if(text[i].time){//如果有科目没复习完,则不行
            cout<<-1;
            return 0;
        }
    for(int i=1;i<=n;i++)
        cout<<ans[i]<<' ';
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41874469/article/details/80560312