https://vjudge.net/contest/389195#problem/F
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 nn days. He shall make his prediction by filling a table with nn intervals, the ii-th of which is the predicted interval [li,ri][li,ri] at the ii-th day. If all nn 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 kk, which is an integer. That means, if the stock price at the ii-th day is xx, the price at the i+1i+1-th day is at most x+kx+k and at least x−kx−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 11 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.
InputThe input starts with an integer TT (1≤T≤1051≤T≤105), denoting the number of test cases.
For each test case, the first line contains two space-separated integers nn and kk (2≤n≤1052≤n≤105, 0≤k≤1090≤k≤109), where nn is the number of days and kk is the fluctuation limit.
The ii-th line of the next nn lines contains two space-separated integers lili and riri (0≤li≤ri≤1090≤li≤ri≤109), which is Cuber QQ's predicted interval in the ii-th day. A prediction is believed to be correct if the stock price ii-th day lies between lili and riri, inclusive.
It is guaranteed that the sum of all nn does not exceed 106106.OutputFor 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,…,ana1,a2,…,an, where li≤ai≤rili≤ai≤ri (1≤i≤n1≤i≤n) and |ai−ai+1|≤k|ai−ai+1|≤k (1≤i≤n−11≤i≤n−1). 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
Sponsor
题意:每次能在一个能够变换的区间变化,找区间
思路:
正反两边扫描, 把左边的约束带到右边,反过来满足后面的方法决定前面的改变.
同时满足正反两面变化的区间,最后剩下的为可行域(选最低的
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<bitset> #include<cassert> #include<cctype> #include<cmath> #include<cstdlib> #include<ctime> #include<deque> #include<iomanip> #include<list> #include<map> #include<queue> #include<set> #include<stack> #include<vector> #include <vector> #include <iterator> #include <utility> #include <sstream> #include <limits> #include <numeric> #include <functional> using namespace std; #define gc getchar() #define mem(a) memset(a,0,sizeof(a)) #define debug(x) cout<<"debug:"<<#x<<" = "<<x<<endl; #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int,int> pii; typedef char ch; typedef double db; const double PI=acos(-1.0); const double eps=1e-6; const int inf=0x3f3f3f3f; const int maxn=1e5+10; const int maxm=100+10; const int N=1e6+10; const int mod=1e9+7; ll L[maxn] = {0}; ll R[maxn] = {0}; int main() { ll l[maxn] = {0}; ll r[maxn] = {0}; ll n = 0 , k = 0; bool flag = 1; int T = 0; cin >> T; while(T--) { flag = 1; cin >> n >> k; for(int i = 0;i<n;i++) { cin >> L[i] >> R[i]; } l[0] = L[0]; r[0] = R[0]; for(int i = 1;i<n;i++) { r[i] = min(R[i] , r[i-1] + k); l[i] = max(L[i] , l[i-1] - k); if(r[i] < l[i]) { flag = 0; } } for(int i = 1;i<n;i++) { r[n-1-i] = min(r[n-1-i] , r[n-i] + k); l[n-1-i] = max(l[n-1-i] , l[n-i] - k); if(r[i] < l[i]) { flag = 0; } } if(flag) { cout << "YES" <<endl; for(int i = 0;i<n;i++) { cout << r[i]; if(i < n-1) { cout<<" "; } } cout <<endl; } else { cout << "NO" <<endl; } } return 0; } /* 2 3 1 1 6 2 5 3 6 2 3 1 5 10 50 */