zoukankan      html  css  js  c++  java
  • 2020杭电多校第八场—Fluctuation Limit

    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 ≤ 105, 0 ≤ k ≤ 109), 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 106.

    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 |ai −ai+1|≤ k (1 ≤ i ≤ n−1). The integers should be separated with space.

    和CF620 div2的C题很类似,但是稍微麻烦一点,正着对区间约束过后还得倒着将每个区间缩一遍。这样最终随便从每段可行区间里任意取一个值即可。

    其中up[i]表示第i天能预测的最大值,down[i]同理,因为可能存在类似第i天的down[i]加上k后还到不了第i+1天的下界,所以最后得倒着根据第i+1天的down[i]减掉k来更新第i天的down[i]。

    #include <bits/stdc++.h>
    using namespace std;
    int n, k, l[100005], r[100005], up[100005], down[100005];
    int main()
    {
    	int t;
    	cin >> t;
    	while(t--)
    	{
    		cin >> n >> k;
    		for(int i = 1; i <= n; i++)
    		{
    			scanf("%d%d", &l[i], &r[i]);
    		}
    		up[1] = r[1], down[1] = l[1];
    		bool flag = 1;
    		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]) flag = 0;
    		}
    		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]) flag = 0;
    		}
    		if(!flag) cout << "NO" << endl;
    		else 
    		{
    			cout << "YES" << endl;
    			for(int i = 1; i <= n; i++)
    			{
    				cout << down[i] << ' ';
    			}
    			cout << endl;
    		}
    	}
    	return 0;
    }
    

    CF链接:https://codeforces.com/contest/1304/problem/C

    官方题解:

    如果 i 是在 [l,r] 范围内, 那么 i + 1 必须要在 [l−k,r + k] 范围内.这是因为如果 i + 1 选了 范围外的值, i 就无解了. 这样可以从左往右, 把左边的约束带到右边.再从右往左做一遍.最后剩下的区间应该就是可 行域.因为题目只要求一种方案, 全部选最低的即可.复杂度 O(n).

  • 相关阅读:
    自定义打包工具对应的解析代码
    自定义的打包工具源码
    一种C语言实现面向对象特性的继承,多态
    buffers和cached的区别
    初识rt-thread杂记
    一种多叉树的实现,提供树形结构打印,树转表输出等功能
    关于rtsp的时间戳问题
    一种基于状态机(表)的小组件
    一种基于消息发布-订阅的观察者模式实现
    命令解析类代码重构
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/13497787.html
Copyright © 2011-2022 走看看