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.


题意:输入n,m,n天,m门课,下面有m行,每行有三个数字,第一个数这门课的起始时间,第二个数是这门课的考试时间,第三个数表示这个门课要学的天数,看看能不能把所有的课程学完,要是能,请你输出一个合理的方案, 看看那一天学那门课,要是不能清你输出-1;

思路: 先说结论吧:按照这门课的结束时间(考试时间)从小到大排序,结束时间靠前的一定先结束,所以如果这一天还没有用的话,那么就是让考试时间靠前的用;这个不用管开始时间;我刚开始卡过了一个代码,是先按起始时间从小到大排序,要是有相等,按结束时间从小到大排,卡到42组数据上,我看着这组数据,我有先按他们的差值从小到大排序,要是相等的再按上面的排序,卡到了62组数据,看到了数据,我有想到了办法,就是看他们相交的区间,要是这个位置,只有自己能用的话,你就优先用这个位置,要是有交叉,就是按照排序的顺序来,最终是卡过了;

卡过之后,我有想到了,之间按结束时间从小到大排序就行了

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#include<vector>

#define Max 110
struct node
{
	int star,end,limit;
	int p;
}stu[Max];

int n,m;
int a[Max];
int cmp(node a,node b)
{
	return a.end<b.end;
}

int main()
{
	int i,j;
	while(~scanf("%d%d",&n,&m))
	{
		memset(a,0,sizeof(a));
		for(i = 1;i <= m;i++)
		{
			scanf("%d%d%d",&stu[i].star,&stu[i].end,&stu[i].limit);
			stu[i].p = i;
		}
		sort(stu+1,stu+(m+1),cmp);
		
		int flag = 1;
		for(i=1;i<=m;i++)
		{
			if(a[stu[i].end]==0)
				a[stu[i].end] = m+1; 
			else flag = 0;
		}
		for(i =  1;i<=m;i++)
		{
			for(j = stu[i].star;j<=stu[i].end;j++)
			{
				if(a[j]==0)
				{
					a[j] = stu[i].p;
					stu[i].limit--;
				}
				if(stu[i].limit==0)
					break;
			}
			if(j>stu[i].end)
			{
				flag = 0;
			}
		}
		if(flag==0) printf("-1\n");
		else
		{
			for(i = 1;i<=n;i++)
			{
				if(i!=1) printf(" ");
				printf("%d",a[i]);
			}
			printf("\n");
		}
	}
	return 0;
}

我卡过的代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#include<vector>

#define Max 110
struct node
{
	int star,end,limit;
	int cha;
	int p;
}stu[Max];

int n,m;
int a[Max],book[Max];
int cmp(node a,node b)
{
	if(a.cha!=b.cha)
	{
		return a.cha<b.cha;    // 差值 
	}
	else if(a.star==b.star)
		return a.end<b.end;
	return a.star<b.star;
}

int main()
{
	int i,j;
	while(~scanf("%d%d",&n,&m))
	{
		memset(a,0,sizeof(a));
		memset(book,0,sizeof(book));
		for(i = 1;i <= m;i++)
		{
			scanf("%d%d%d",&stu[i].star,&stu[i].end,&stu[i].limit);
			stu[i].cha = stu[i].end - stu[i].star+1;
			//printf("%d\n",stu[i].cha);
			stu[i].p = i;
		}
		sort(stu+1,stu+(m+1),cmp);
		
		int flag = 1;
		for(i=1;i<=m;i++)
		{
			if(a[stu[i].end]==0)
				a[stu[i].end] = m+1; 
			else flag = 0;
		}
	
		for(i = 1;i<=m;i++)
		{
			for(j = stu[i].star;j<=stu[i].end;j++)
				book[j] ++;
		}
		for(i=1;i<=n;i++)
		{
			if(book[i])
				book[i]--;
		}
		for(i =  1;i<=m;i++)
		{
			for(j = stu[i].star;j<=stu[i].end;j++)
			{
				if(!book[j]&&a[j]==0)   // 优先有只有自己能用的的天; 
				{
					a[j] = stu[i].p;
					stu[i].limit--;
					book[j] = 1;
				}
				if(stu[i].limit==0)
					break;
			}
			for(j = stu[i].star;j<=stu[i].end;j++)
			{
				if(stu[i].limit==0)
					break;
				if(a[j]==0)
				{
					a[j] = stu[i].p;
					stu[i].limit--;
				}
				if(stu[i].limit==0)
					break;
			}
			if(j>stu[i].end)
			{
				flag = 0;
			}
				
		}
		if(flag==0) printf("-1\n");
		else
		{
			for(i = 1;i<=n;i++)
			{
				if(i!=1) printf(" ");
				printf("%d",a[i]);
			}
			printf("\n");
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/obsorb_knowledge/article/details/80318107