题目:
Vasya has an array a1,a2,…,an.
You don’t know this array, but he told you m facts about this array. The i-th fact is a triple of numbers ti, li and ri (0≤ti≤1,1≤li<ri≤n) and it means:
if ti=1 then subbarray ali,ali+1,…,ari is sorted in non-decreasing order;
if ti=0 then subbarray 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] then he could give you three facts: t1=1,l1=2,r1=4 (the subarray [a2,a3,a4]=[1,1,3] is sorted), t2=0,l2=4,r2=5 (the subarray [a4,a5]=[3,2] is not sorted), and t3=0,l3=3,r3=5 (the subarray [a3,a5]=[1,3,2] is not sorted).
You don’t know the array a. Find any array which satisfies all the given facts.
Input
The first line contains two integers n and m (2≤n≤1000,1≤m≤1000).
Each of the next m lines contains three integers ti, li and ri (0≤ti≤1,1≤li<ri≤n).
If ti=1 then subbarray ali,ali+1,…,ari is sorted. Otherwise (if ti=0) subbarray ali,ali+1,…,ari is not sorted.
Output
If there is no array that satisfies these facts in only line print NO (in any letter case).
If there is a solution, print YES (in any letter case). In second line print n integers a1,a2,…,an (1≤ai≤109) — the array a, 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
思路:用f[i]表示所求数列的a[i] - a[i-1] (i > 1) , 先假设数列是一个递减序列,a[1] = n(数组的元素个数),设置d[i] = -1。先对所有t = 1的要求进行判断,如果给出l — r 位置是非递减的,就把这一段设置d[i] = 0 。再对t = 0 的要求进行判断,如果给出一段l — r是不完全非递减的,且这一段的d[i]都是0,则与之前的条件矛盾,输出no,否则合理,最后输出num[i] = num[i-1] + f[i]
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
struct node {
int t,l,r;
node(int a,int b,int c):t(a),l(b),r(c){}
};
vector <node>v[2];
int num[maxn],f[maxn];
int main() {
int n,m;
ios::sync_with_stdio(0);
cin >> n >> m;
for(int i = 1; i <= n; i++)f[i] = -1;
for(int i = 0; i < m; i++) {
int t,l,r;
cin >> t >> l >> r;
v[t].push_back(node(t,l,r));
}
for(int i = 0; i < v[1].size(); i++) {
int l = v[1][i].l , r = v[1][i].r;
for(int j = l + 1; j <= r; j++) {
f[j] = 0;
}
}
int flag = 0;
for(int i = 0; i < v[0].size()&&!flag; i++) {
int l = v[0][i].l , r = v[0][i].r;
int tmp = 0;
for(int j = l + 1; j <= r && !tmp; j++) {
if(f[j] == -1)tmp = 1;
}
if(!tmp)flag = 1;
}
if(flag) cout << "NO" <<"\n";
else {
num[1] = n;
for(int i = 2; i <= n; i++) {
num[i] = num[i-1] + f[i];
}
cout << "YES" << "\n";
for(int i = 1; i <= n; i++) {
if(i > 1)cout << " ";
cout << num[i];
}
cout << "\n";
}
return 0;
}