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 di−1di−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 si≤j<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.
The first line contains two integers nn and mm (2≤n≤100,1≤m≤n)(2≤n≤100,1≤m≤n) — the number of days and the number of exams.
Each of the following mm lines contains three integers sisi, didi, cici (1≤si<di≤n,1≤ci≤n)(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.
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 (1≤i≤m1≤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.
题意:还剩下n天,要猪呢比m场考试,每天要么准备考试要么考试,要么休息,输出能通过全部考试的方案,没有为-1,每场考试有三个信息,1:第几天知道有这个考试,2:第几天考试,3:需要准备多少天才能通过考试。
题解:用结构体存一下,然后排序(排序方法很重要,先di再si),遍历一遍结构体数组,用一个数组记录(贪心)一下每天做的事,要考试的那天考试,从已知有考试的那天往下安排复习往下推知道到考试那天截止,准备好了则休息,不能满足终止输出-1
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
int n,m;
struct Exam
{
int s, d, c,i;
}exam[110];
int out[110];
int cmp(const Exam &a, const Exam &b)
{
if(a.d==b.d)
return a.s<b.s;
else
return a.d<b.d;
}
int main()
{
cin>>n>>m;
{
int p = 1;
int k=1;
memset(out,0,sizeof(out));
for (int i = 1; i <= m; i++)
{
int s1, d1, c1;
//cin >> exam[i].s >> exam[i].d >> exam[i].c;
//s1 = exam[i].s; d1 = exam[i].d; c1 = exam[i].c;
cin>>s1>>d1>>c1;
exam[i].s = s1, exam[i].d= d1, exam[i].c = c1;
exam[i].i = i;
}
sort(exam+1,exam+1+m,cmp);
for(int i=1;i<=m;i++)
{
//cout<<exam[i].s <<" "<<exam[i].d<<endl;
if(exam[i].d-exam[i].s==exam[i].c)
{
if(out[exam[i].d]!=0)
p =0;
}
if (exam[i].d - exam[i].s < exam[i].c)
{
p = 0;
}
else
{
out[exam[i].d] = m + 1;
k = exam[i].s;
for(int j=0;j<exam[i].c;j++)
{
while(out[k]!=0)
k++;
if(k>=exam[i].d)
p=0;
out[k++] = exam[i].i;
}
}
}
if (n < m * 2)
p = 0;
if (p == 0)
cout << -1 << endl;
else
{
for (int i = 1; i <= n; i++)
{
cout << out[i] << " ";
}
}
}
}