Educational Codeforces Round 67 (Rated for Div. 2)C. Vasya And Array

题目:
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值