生活随笔
收集整理的這篇文章主要介紹了
luogu p3515 Lightning Conductor
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
luogu p3515 Lightning Conductor
給定一個長度為n的序列,對于每一個i∈[1,n]i∈[1,n]i∈[1,n],求出一個最小的非負(fù)整數(shù)p,使得對于所有的j∈[1,n]j∈[1,n]j∈[1,n],都有aj<=ai+p?∣i?j∣a_j<=a_i+p-\sqrt{|i-j|}aj?<=ai?+p?∣i?j∣?
通過這個題學(xué)習(xí)到兩點,一個是用分治的方法解決1D1D類型的四邊形優(yōu)化dp問題,另一個是根據(jù)凸性來得到四邊形不等式。
稍微對于原來的式子做一點變化,可以得到:
pi=max(aj+∣i?j∣?ai)p_i = max(a_j+\sqrt{|i-j|} - a_i)pi?=max(aj?+∣i?j∣??ai?)取相反數(shù)之后,pi=min(ai?aj?∣i?j∣)p_i=min(a_i-a_j-\sqrt{|i-j|})pi?=min(ai??aj??∣i?j∣?)其中?x-\sqrt{x}?x?為下凸函數(shù),所以可以滿足決策單調(diào)性。
const int N
= 500010, inf
= 1e9, eps
= 1e-6;
int a
[N
];
double ans
[N
];void solve1(int l
, int r
, int m_l
, int m_r
)
{if (l
> r
) return;if (l
== r
){for (int i
= m_l
; i
<= min(m_r
, l
); i
++){double temp
= a
[i
];temp
+= sqrt((double)l
- i
);ans
[l
] = max(ans
[l
], temp
);}return;}int mid
= (l
+ r
) >> 1, k
;double mx
= -inf
;for (int i
= m_l
; i
<= min(m_r
, mid
); i
++){double temp
= a
[i
];temp
+= sqrt((double)mid
- i
);if (temp
> mx
){mx
= temp
;k
= i
;}}ans
[mid
] = max(ans
[mid
], mx
);solve1(l
, mid
- 1, m_l
, k
);solve1(mid
+ 1, r
, k
, m_r
);return;
}void solve2(int l
, int r
, int m_l
, int m_r
)
{if (l
> r
) return;if (l
== r
){for (int i
= m_r
; i
>= max(m_l
, l
); i
--){double temp
= a
[i
];temp
+= sqrt((double)i
- l
);ans
[l
] = max(ans
[l
], temp
);}return;}int mid
= (l
+ r
) >> 1, k
;double mx
= -inf
;for (int i
= m_r
; i
>= max(mid
, m_l
); i
--){double temp
= a
[i
];temp
+= sqrt((double)i
- mid
);if (temp
> mx
){mx
= temp
;k
= i
;}}ans
[mid
] = max(ans
[mid
], mx
);solve2(l
, mid
- 1, m_l
, k
);solve2(mid
+ 1, r
, k
, m_r
);return;
}int main()
{int T
= 1;while (T
--){int n
;n
= read();for (int i
= 1; i
<= n
; i
++)a
[i
] = read();solve1(1, n
, 1, n
);solve2(1, n
, 1, n
);for (int i
= 1; i
<= n
; i
++)printf ("%d\n", (int)ceil(ans
[i
] - (double)a
[i
]));}return 0;
}
總結(jié)
以上是生活随笔為你收集整理的luogu p3515 Lightning Conductor的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。