C. Vasya And Array

https://codeforces.com/problemset/problem/1187/C

题目描述

Vasya has an array a_1, a_2, \dots, a_na1​,a2​,…,an​ .

You don't know this array, but he told you mm facts about this array. The ii -th fact is a triple of numbers t_iti​ , l_ili​ and r_iri​ ( 0 \le t_i \le 1, 1 \le l_i < r_i \le n0≤ti​≤1,1≤li​<ri​≤n ) and it means:

  • if t_i=1ti​=1 then subbarray a_{l_i}, a_{l_i + 1}, \dots, a_{r_i}ali​​,ali​+1​,…,ari​​ is sorted in non-decreasing order;
  • if t_i=0ti​=0 then subbarray a_{l_i}, a_{l_i + 1}, \dots, 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] then he could give you three facts: t_1=1, l_1=2, r_1=4t1​=1,l1​=2,r1​=4 (the subarray [a_2, a_3, a_4] = [1, 1, 3][a2​,a3​,a4​]=[1,1,3] is sorted), t_2=0, l_2=4, r_2=5t2​=0,l2​=4,r2​=5 (the subarray [a_4, a_5] = [3, 2][a4​,a5​]=[3,2] is not sorted), and t_3=0, l_3=3, r_3=5t3​=0,l3​=3,r3​=5 (the subarray [a_3, a_5] = [1, 3, 2][a3​,a5​]=[1,3,2] is not sorted).

You don't know the array aa . Find any array which satisfies all the given facts.

输入格式

The first line contains two integers nn and mm ( 2 \le n \le 1000, 1 \le m \le 10002≤n≤1000,1≤m≤1000 ).

Each of the next mm lines contains three integers t_iti​ , l_ili​ and r_iri​ ( 0 \le t_i \le 1, 1 \le l_i < r_i \le n0≤ti​≤1,1≤li​<ri​≤n ).

If t_i = 1ti​=1 then subbarray a_{l_i}, a_{l_i + 1}, \dots , a_{r_i}ali​​,ali​+1​,…,ari​​ is sorted. Otherwise (if t_i = 0ti​=0 ) subbarray a_{l_i}, a_{l_i + 1}, \dots , a_{r_i}ali​​,ali​+1​,…,ari​​ is not sorted.

输出格式

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 nn integers a_1, a_2, \dots, a_na1​,a2​,…,an​ ( 1 \le a_i \le 10^91≤ai​≤109 ) — the array aa , satisfying all the given facts. If there are multiple satisfying arrays you can print any of them.

题意翻译

题目描述

Vasya有一个数组a[1...n]a[1...n]

你从来没听说过这个数组,但Vasya会告诉你 mm 条关于这个数组的信息。每条信息包含三个参数t_i,l_i,r_i(0\leq t_i \leq 1 ,1 \leq l_i < r_i \leq n)ti​,li​,ri​(0≤ti​≤1,1≤li​<ri​≤n),其含义分别为:

  • 如果 t_i=1ti​=1 则说明子数组a[l_i...r_i]a[li​...ri​] 是一个不降序列

  • 如果 t_i=0ti​=0 则说明子数组a[l_i...r_i]a[li​...ri​] 不是一个不降序列。一个数组 aa 不是一个不降序列说明存在两个相邻元素a[i] ,a[i+1]a[i],a[i+1]使得a[i]>a[i+1]a[i]>a[i+1]

举个栗子:假设a=[2,1,1,3,2]a=[2,1,1,3,2] ,然后 Vasya 告诉你:

t_1=1,l_1=2,r_1=4t1​=1,l1​=2,r1​=4,意思是 a[2...4]=[1,1,3]a[2...4]=[1,1,3]是一个不降序列

t_1=0,l_1=4,r_1=5t1​=0,l1​=4,r1​=5,意思是 a[4...5]=[3,2]a[4...5]=[3,2]不是一个不降序列 t_1=0,l_1=3,r_1=5t1​=0,l1​=3,r1​=5,意思是 a[4...5]=[1,3,2]a[4...5]=[1,3,2]不是一个不降序列

然而就算Vasya 告诉你这么多条件,你依然不会知道数组 aa,但是请你找出一种可能的情况。

输入输出格式

输入格式

第一行有两个整数n,m(2\leq n\leq 1000 ,1 \leq m \leq 1000)n,m(2≤n≤1000,1≤m≤1000)

接下来 mm 行每一行有三个整数 t_i,l_i,r_iti​,li​,ri​表示一条信息,含义见题目描述

输出格式

如果Vasya自相矛盾,只需输出NO

如果有解,请先输出YES,之后输出 nn 个整数 a_1,a_2...,a_n(1\leq a_i \leq 10^9)a1​,a2​...,an​(1≤ai​≤109)。有多组解请任意输出一种。

输入输出样例

输入 #1复制

7 4
1 1 3
1 2 5
0 5 6
1 6 7

输出 #1复制

YES
1 2 2 3 5 4 4

输入 #2复制

4 2
1 1 4
0 2 3

输出 #2复制

NO

差分好题。

开始自己做的时候想到了差分,但是忘了差分的一个性质。

参考博客:https://www.cnblogs.com/wzj-xhjbk/p/11181976.html

区间增减性的问题应该从差分角度进行考虑。对于 [l,r] 中元素单调不减时,对应的差分数组 [l+1,r] 应该每一项均大于等于 0;对于 [l,r] 中元素存在单调减的相邻数对时,对应的差分数组应该至少存在一项使得 d[i] = 0 成立。

然后考虑到第一个条件的满足要求强,第二个只要存在就好了。所以先对第一个条件进行构造,也就是初始化差分数组为严格单调减,即:d[i] = -1。

对于所有的性质一来说,将差分数组赋值为大于等于 0 的数字。处理完所有的性质一区间之后,再处理性质二的区间,对于每个性质二的区间,只需要进行判定是否存在一个小于 0 的差分数即可。

最后令a[1]为一个大一点的数,满足题目的a[i]>=1,就可以构造出这个数组了。(d[i]表示第i个元素与前一个元素的差值。如d[2]是第2个元素和第一个元素的差。特别的d[1]是和第0个元素的差。)

启发:区间增减性/单调性的问题可以从差分角度进行考虑。对于 [l,r] 中元素单调不减时,对应的差分数组 [l+1,r] 应该每一项均大于等于 0;对于 [l,r] 中元素存在单调减的相邻数对时,对应的差分数组应该至少存在一项使得 d[i] = 0 成立。


#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e3;
typedef long long LL;
LL d[maxn],a[maxn];
vector< pair<LL,LL> >v;
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,m;cin>>n>>m;
  for(LL i=2;i<=n;i++) d[i]=-1;
  for(LL i=1;i<=m;i++)
  {
  	LL op,l,r;cin>>op>>l>>r;
  	if(op==1){
  		for(LL j=l+1;j<=r;j++) d[j]=1;	
	}
	else{
		v.push_back({l,r});
	}
  }
  bool flag=1;
  for(LL i=0;i<v.size();i++)
  {
  	pair<LL,LL> u=v[i];
  	LL sum=0;
  	for(LL j=u.first+1;j<=u.second;j++)
  	{
  		if(d[j]==-1) sum++;
	}
	if(sum<=0)
	{
		flag=0;
		break;
	}
  }
  if(flag==0) cout<<"NO"<<endl;
  else{
  	cout<<"YES"<<endl;
  	a[1]=30000;
  	for(LL i=2;i<=n;i++) a[i]=a[i-1]+d[i];
  	for(LL i=1;i<=n;i++) cout<<a[i]<<" ";
  	cout<<endl;
  }
return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/108342713