Codeforces Round #481 (Div. 3) G. Petya's Exams【贪心】

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.

思路:

由于数据范围比较小,可以暴力判断每一天优先复习哪一门功课。首先选择的这门功课的s要<=i,并且d>i,且剩余的复习天数大于0。贪心策略为优先取考试日期比较靠前的,考试日期靠后的可以向后推。如果到最后还有功课的c值大于0,那么输出-1.

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
int n,m,dis[105];
struct node
{
    int s,d,c;
}p[105];
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
        scanf("%d%d%d",&p[i].s,&p[i].d,&p[i].c);
    memset(dis,0,sizeof dis);
    for(int i=1;i<=n;i++)
    {
        int tmp=-1,minn=n+1;
        for(int j=0;j<m;j++)
        {
            if(p[j].d==i)
            {
                dis[i]=m+1;
                break;
            }
            if(p[j].s<=i && p[j].d>i && p[j].d<minn && p[j].c>0)
            {
                minn=p[j].d;
                tmp=j;
            }
        }
        if(dis[i]==0 && tmp!=-1) dis[i]=tmp+1,p[tmp].c--;
    }
    for(int i=0;i<m;i++)
    {
        if(p[i].c>0)
        {
            printf("-1\n");
            exit(0);
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(i>1) printf(" ");
        printf("%d",dis[i]);
    }
    printf("\n");
    return 0;
}



猜你喜欢

转载自blog.csdn.net/u013852115/article/details/80315492