C. Vasya And Array
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-thi−th fact is a triple of numbers tit_iti, lil_ili and rir_iri (0≤ti≤1,1≤li<ri≤n)(0\leq t_i\leq1,1\leq l_i<r_i\leq n)(0≤ti≤1,1≤li<ri≤n) 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)(2≤n≤1000,1≤m≤1000).
Each of the next ? lines contains three integers ti,li and ri(0≤ti≤1,1≤li<ri≤n)t_i, l_i\ and\ r_i (0\leq t_i\leq1,1\leq l_i<r_i\leq n)ti,li and ri(0≤ti≤1,1≤li<ri≤n).
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(1≤ai≤109) — 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,r−1]内的位置的markmarkmark值都要标记为111,存下第二种描述,处理完第一种后,对于每一个第二种描述,检查[l,r−1][l,r-1][l,r−1]内的markmarkmark是否有000的情况,显然,如果没有,这种描述是无解的,checkcheckcheck所有第二种描述都没问题之后,另a[1]=na[1]=na[1]=n,然后对于1≤i<n1\leq i<n1≤i<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题特点啊,想清楚代码很短,不想好直接写FSTFSTFST,HackedHackedHacked,还贼长贼长,
这题列表全挂了
#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':' ');
}