C. Dreamoon Likes Coloring ------------------------------------- Thinking

Insert picture description here
Insert picture description here

Title:
Given a square of length n, there are m colors, and each color can be dyed with a length of li. Ask what the starting point of each color is m colors. In the end, all the squares are dyed, and there are m different color
analysis:
the starting point of each color dyeing should not be less than the square corresponding to its own order.

There are comments on the code

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+1000;
int n,m;
int a[N];
int b[N];
int main()
{
	scanf("%d %d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		scanf("%d",&a[i]);
	}
	int pos=n+1;
	for(int i=m;i>=1;i--)
	{
		int k=max(pos-a[i],i);
		if(k<i||k>n-a[i]+1) //如果k<I说明肯定存在颜色被覆盖的情况,如果k>n-a[i]+1 肯定存在没有被染色的情况 所以都不行
		{
			cout<<-1<<endl;
			return 0;
		}
		b[i]=k;
		pos=k;
	}
	if(pos>1)
	{
		cout<<-1<<endl;
		return 0;
	}
	for(int i=1;i<=m;i++)
	{
		cout<<b[i]<<" ";
	}
	cout<<endl;
}

Published 572 original articles · praised 14 · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_43690454/article/details/105334090
Recommended