「CF1187C」Vasya And Array【构造】

探讨构造符合特定条件的数组问题,需满足单调性描述,解析算法思路及实现细节,确保数组既满足非减子数组也包含非单调区间的复杂要求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Vasya And Array

time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output

Vasya has an array a1,a2,…,ana_1,a_2,…,a_na1,a2,,an.
You don’t know this array, but he told you mmm facts about this array. The i−thi-thith fact is a triple of numbers tit_iti, lil_ili and rir_iri (0≤ti≤1,1≤li&lt;ri≤n)(0\leq t_i\leq1,1\leq l_i&lt;r_i\leq n)(0ti1,1li<rin) and it means:

if ti=1t_i=1ti=1 then subbarray ali,ali+1,…,aria_{l_i},a_{l_{i+1}},…,a_{r_i}ali,ali+1,,ari is sorted in non-decreasing order;
if ti=0t_i=0ti=0 then subbarray ali,ali+1,…,aria_{l_i},a_{l_{i+1}},…,a_{r_i}ali,ali+1,,ari is not sorted in non-decreasing order. A subarray is not sorted if there is at least one pair of consecutive elements in this subarray such that the former is greater than the latter.
For example if a=[2,1,1,3,2]a=[2,1,1,3,2]a=[2,1,1,3,2] then he could give you three facts: t1=1,l1=2,r1=4t_1=1,l_1=2,r_1=4t1=1,l1=2,r1=4 (the subarray [a2,a3,a4]=[1,1,3][a_2,a_3,a_4]=[1,1,3][a2,a3,a4]=[1,1,3] is sorted),t2=0,l2=4,r2=5t_2=0,l_2=4,r_2=5t2=0,l2=4,r2=5 (the subarray [a4,a5]=[3,2][a_4,a_5]=[3,2][a4,a5]=[3,2] is not sorted), and t3=0,l3=3,r3=5t_3=0,l_3=3,r_3=5t3=0,l3=3,r3=5 (the subarray [a3,a5]=[1,3,2][a_3,a_5]=[1,3,2][a3,a5]=[1,3,2] is not sorted).

You don’t know the array ?. Find any array which satisfies all the given facts.

Input

The first line contains two integers nnn and mmm (2≤n≤1000,1≤m≤1000)(2\leq n\leq1000,1\leq m \leq1000)(2n1000,1m1000).
Each of the next ? lines contains three integers ti,li and ri(0≤ti≤1,1≤li&lt;ri≤n)t_i, l_i\ and\ r_i (0\leq t_i\leq1,1\leq l_i&lt;r_i\leq n)ti,li and ri(0ti1,1li<rin).
If ti=1t_i=1ti=1 then subbarray ali,ali+1,…,aria_{l_i},a_{l_{i+1}},…,a_{r_i}ali,ali+1,,ari is sorted. Otherwise (if ti=0t_i=0ti=0) subbarray ali,ali+1,…,aria_{l_i},a_{l_{i+1}},…,a_{r_i}ali,ali+1,,ari is not sorted.

Output

If there is no array that satisfies these facts in only line print NONONO (in any letter case).

If there is a solution, print YESYESYES (in any letter case). In second line print nnn integers a1,a2,…,an(1≤ai≤109)a_1,a_2,…,a_n (1\leq a_i\leq10^9)a1,a2,,an(1ai109) — the array aaa, satisfying all the given facts. If there are multiple satisfying arrays you can print any of them.

Examples

input
7 4
1 1 3
1 2 5
0 5 6
1 6 7
output
YES
1 2 2 3 5 4 4
input
4 2
1 1 4
0 2 3
output
NO

题意

  • 就是构造一个长度为nnn的数组,然后给出mmm条描述条件,分为两种描述,一种是描述区间[l,r][l,r][l,r]内的数单调不减,另一种描述[l,r][l,r][l,r]内的数不是单调不减

题解

  • 计需要构造的数组为aaa,用mark[i]mark[i]mark[i]表示a[i+1]a[i+1]a[i+1]是否需要大于等于a[i]a[i]a[i],当给出第一种描述时,位置[l,r−1][l,r-1][l,r1]内的位置的markmarkmark值都要标记为111,存下第二种描述,处理完第一种后,对于每一个第二种描述,检查[l,r−1][l,r-1][l,r1]内的markmarkmark是否有000的情况,显然,如果没有,这种描述是无解的,checkcheckcheck所有第二种描述都没问题之后,另a[1]=na[1]=na[1]=n,然后对于1≤i&lt;n1\leq i&lt;n1i<n,如果mark[i]=1mark[i]=1mark[i]=1,不妨令a[i+1]=a[i]a[i+1]=a[i]a[i+1]=a[i],否则令a[i+1]=a[i]−1a[i+1]=a[i]-1a[i+1]=a[i]1;
  • 感觉典型CFCFCF题特点啊,想清楚代码很短,不想好直接写FSTFSTFSTHackedHackedHacked,还贼长贼长,这题列表全挂了
#include<bits/stdc++.h>

using namespace std;
const int maxn=1005;

int a[maxn][3],mark[maxn];

int main()
{
	int n,m;scanf("%d %d",&n,&m);
	for(int i=1,t,l,r;i<=m;i++){
		scanf("%d %d %d",&a[i][0],&a[i][1],&a[i][2]);
		if(a[i][0]) for(int j=a[i][1];j<a[i][2];j++) mark[j]=1;
	}
	for(int i=1;i<=m;i++){
		bool ok=false;
		if(!a[i][0]){
			for(int j=a[i][1];j<a[i][2];j++){
				if(!mark[j]) {ok=true;break;}
			}
			if(!ok) return printf("NO\n"),0;
		}
	}
	int ans=n+1;
	printf("YES\n");
	for(int i=1;i<=n;i++) printf("%d%c",mark[i-1]?ans:--ans,i==n?'\n':' ');
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值