2020杭电多校8 Fluctuation Limit(缩小区间找可行域)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6860
Problem Description
It is preferrable to read the pdf statment.

Cuber QQ has signed up for a gambling game, that challenges him to predict the stock price of Quber CC Limited, for the next following n days. He shall make his prediction by filling a table with n intervals, the i-th of which is the predicted interval [li,ri] at the i-th day. If all n prices lie in the corresponding interval, Cuber QQ will win 1 million dollars. Otherwise, he will not earn a single penny.

As is well known, the stock price has a fluctuation limit. For simplicity, we assume the limit up and the limit down are both k, which is an integer. That means, if the stock price at the i-th day is x, the price at the i+1-th day is at most x+k and at least x−k.

Cuber QQ wants to know whether it is possible to manipulate the stock price, without breaking the limitation above of course, so that he can have the 1 million dollars. Since his table has already been submitted, he cannot modify his predicted intervals any more. It has to be done secretly behind the scenes, and smartly cover it up so that no one will notice.

Input
The input starts with an integer T (1≤T≤105), denoting the number of test cases.

For each test case, the first line contains two space-separated integers n and k ( 2 ≤ n ≤ 1 0 5 , 0 ≤ k ≤ 1 0 9 ) , (2≤n≤10^5, 0≤k≤10^9), (2n105,0k109), where n is the number of days and k is the fluctuation limit.

The i-th line of the next n lines contains two space-separated integers li and ri (0≤li≤ri≤109), which is Cuber QQ’s predicted interval in the i-th day. A prediction is believed to be correct if the stock price i-th day lies between li and ri, inclusive.

It is guaranteed that the sum of all n does not exceed 10^6.

Output
For each test case, first output a single line YES or NO, that states whether Cuber QQ will win the 1 million price.

If YES, in the next line, output a possible price series, a1,a2,…,an, where li≤ai≤ri (1≤i≤n) and ∣ a i − a i + 1 ∣ ≤ k ( 1 ≤ i ≤ n − 1 ) |a_i−a_{i+1}|≤k (1≤i≤n−1) aiai+1k(1in1). The integers should be separated with space.

Sample Input

2
3 1
1 6
2 5
3 6
2 3
1 5
10 50

Sample Output

YES
2 3 3
NO

翻译:
输入n和k,接下来n个区间。问:是否能从每个区间内选一个数,
这n个数满足 ∣ a i − a i + 1 ∣ ≤ k ( 1 ≤ i ≤ n − 1 ) |a_i−a_{i+1}|≤k (1≤i≤n−1) aiai+1k(1in1),若能,输出YES和这n个数;否则,输出NO

分析:
如果 i 是在 [l,r] 范围内, 那么 i + 1 必须要在 [l−k,r + k] 范围内.这是因为如果 i + 1 选了 范围外的值, i 就无解了. 这样可以从左往右, 把左边的约束带到右边.再从右往左做一遍.最后剩下的区间应该就是可 行域.

设·: up[i] 表示第i天能预测的最大值down[i] 表示第i天能预测的最小值

从前到后缩小区间:

        for(int i=2; i<=n; i++)
		{
     
			up[i]=min(r[i],up[i-1]+k);
			down[i]=max(l[i],down[i-1]-k);
			if(up[i]<down[i])
				book=true;
		}
  1. up[i]=min(r[i],up[i-1]+k);

  2. down[i]=max(l[i],down[i-1]-k);

从后到前缩小区间:

	    for(int i=n-1; i>=1; i--)
		{
     
			up[i]=min(up[i],up[i+1]+k);
			down[i]=max(down[i],down[i+1]-k);
			if(up[i]<down[i])
				book=true;
		}
  1. up[i]=min(up[i],up[i+1]+k);

  2. down[i]=max(down[i],down[i+1]-k);

完整代码:

#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int N=1e5+10;
int n;
LL k;
LL l[N],r[N];
LL up[N],down[N];
int main()
{
     
	int t;
	scanf("%d",&t);
	while(t--)
	{
     
		scanf("%d%lld",&n,&k);
		for(int i=1; i<=n; i++)
			scanf("%lld%lld",&l[i],&r[i]);
		up[1]=r[1],down[1]=l[1];
		bool book=false;
		for(int i=2; i<=n; i++)
		{
     
			up[i]=min(r[i],up[i-1]+k);
			down[i]=max(l[i],down[i-1]-k);
			if(up[i]<down[i])
				book=true;
		}
		for(int i=n-1; i>=1; i--)
		{
     
			up[i]=min(up[i],up[i+1]+k);
			down[i]=max(down[i],down[i+1]-k);
			if(up[i]<down[i])
				book=true;
		}
		if(book)
			printf("NO\n");
		else
		{
     
			printf("YES\n");
			for(int i=1; i<n; i++)
				printf("%lld ",down[i]);
			printf("%lld\n",down[n]);
		}
	}
	return 0;
}

对他人最大的祝福就是走好脚下的每一步路

你可能感兴趣的:(比赛)