1. 程式人生 > 實用技巧 >F - Fluctuation Limit HDU - 6860

F - Fluctuation Limit HDU - 6860

題目連結:https://vjudge.net/problem/HDU-6860

題意:相鄰兩天的差值的絕對值不超過K。

思路:該題的關鍵在於前面的點會影響後面的點,後面的點會影響前面的點,我們要找到一個區間滿足變化。那麼就是貪心的思想從前往後遍歷一遍,從後往前也遍歷一遍。

  1 #include <bits/stdc++.h>
  2 #include <time.h>
  3 #include <set>
  4 #include <map>
  5 #include <stack>
  6 #include <cmath>
  7
#include <queue> 8 #include <cstdio> 9 #include <string> 10 #include <vector> 11 #include <cstring> 12 #include <utility> 13 #include <cstring> 14 #include <iostream> 15 #include <algorithm> 16 #include <list> 17 using namespace
std; 18 //cout<<setprecision(10)<<fixed; 19 #define eps 1e-6 20 #define PI acos(-1.0) 21 #define lowbit(x) ((x)&(-x)) 22 #define zero(x) (((x)>0?(x):-(x))<eps) 23 #define mem(s,n) memset(s,n,sizeof s); 24 #define rep(i,a,b) for(int i=a;i<=b;i++) 25 #define rep2(i,a,b) for(int i=a;i>=b;i--) 26
#define ios {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);} 27 typedef long long ll; 28 typedef unsigned long long ull; 29 const int maxn=1e6+5; 30 const ll Inf=0x7f7f7f7f7f7f7f7f; 31 const ll mod=1e6+3; 32 //const int N=3e3+5; 33 bool isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; }//判斷一個數是不是 2 的正整數次冪 34 int modPowerOfTwo(int x, int mod) { return x & (mod - 1); }//對 2 的非負整數次冪取模 35 int getBit(int a, int b) { return (a >> b) & 1; }// 獲取 a 的第 b 位,最低位編號為 0 36 int Max(int a, int b) { return b & ((a - b) >> 31) | a & (~(a - b) >> 31); }// 如果 a>=b,(a-b)>>31 為 0,否則為 -1 37 int Min(int a, int b) { return a & ((a - b) >> 31) | b & (~(a - b) >> 31); } 38 ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} 39 ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} 40 inline int read() 41 { 42 int X=0; bool flag=1; char ch=getchar(); 43 while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();} 44 while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();} 45 if(flag) return X; 46 return ~(X-1); 47 } 48 inline void write(int X) 49 { 50 if(X<0) {X=~(X-1); putchar('-');} 51 if(X>9) write(X/10); 52 putchar(X%10+'0'); 53 } 54 /* 55 inline int write(int X) 56 { 57 if(X<0) {putchar('-'); X=~(X-1);} 58 int s[20],top=0; 59 while(X) {s[++top]=X%10; X/=10;} 60 if(!top) s[++top]=0; 61 while(top) putchar(s[top--]+'0'); 62 } 63 */ 64 int Abs(int n) { 65 return (n ^ (n >> 31)) - (n >> 31); 66 /* n>>31 取得 n 的符號,若 n 為正數,n>>31 等於 0,若 n 為負數,n>>31 等於 -1 67 若 n 為正數 n^0=n, 數不變,若 n 為負數有 n^(-1) 68 需要計算 n 和 -1 的補碼,然後進行異或運算, 69 結果 n 變號並且為 n 的絕對值減 1,再減去 -1 就是絕對值 */ 70 } 71 ll binpow(ll a, ll b) { 72 ll res = 1; 73 while (b > 0) { 74 if (b & 1) res = res * a%mod; 75 a = a * a%mod; 76 b >>= 1; 77 } 78 return res%mod; 79 } 80 void extend_gcd(ll a,ll b,ll &x,ll &y) 81 { 82 if(b==0) { 83 x=1,y=0; 84 return; 85 } 86 extend_gcd(b,a%b,x,y); 87 ll tmp=x; 88 x=y; 89 y=tmp-(a/b)*y; 90 } 91 ll mod_inverse(ll a,ll m) 92 { 93 ll x,y; 94 extend_gcd(a,m,x,y); 95 return (m+x%m)%m; 96 } 97 ll eulor(ll x) 98 { 99 ll cnt=x; 100 ll ma=sqrt(x); 101 for(int i=2;i<=ma;i++) 102 { 103 if(x%i==0) cnt=cnt/i*(i-1); 104 while(x%i==0) x/=i; 105 } 106 if(x>1) cnt=cnt/x*(x-1); 107 return cnt; 108 } 109 ll l[maxn],r[maxn],a[maxn],b[maxn]; 110 int main() 111 { 112 int t=read(); 113 while(t--) 114 { 115 int n=read(),k=read(); 116 for(int i=1;i<=n;i++) scanf("%lld %lld",&l[i],&r[i]); 117 a[1]=l[1]; 118 b[1]=r[1]; 119 bool flag=true; 120 for(int i=2;i<=n;i++) 121 { 122 a[i]=max(a[i-1]-k,l[i]); 123 b[i]=min(b[i-1]+k,r[i]); 124 if(a[i]>b[i]) {flag=false;break;} 125 } 126 for(int i=n-1;i>=1;i--) 127 { 128 a[i]=max(a[i],a[i+1]-k); 129 b[i]=min(b[i],b[i+1]+k); 130 if(a[i]>b[i]) {flag=false;break;} 131 } 132 if(!flag) puts("NO"); 133 else 134 { 135 puts("YES"); 136 for(int i=1;i<=n;i++) 137 { 138 if(i!=n) printf("%lld ",a[i]); 139 else printf("%lld\n",a[i]); 140 } 141 } 142 } 143 return 0; 144 }