当前位置:网站首页>[CF1476F]Lanterns
[CF1476F]Lanterns
2022-07-01 18:17:00 【OneInDark】
subject
Ideas
First of all, this is a d p \tt dp dp problem . Then we need to find the recursive subproblem .
Generally, the case of a particular value will be enumerated . For example, the last lantern . Consider which lanterns it covers ? The unlighted lanterns are discrete , no way . Then you can consider who covers it .
You might as well set up lanterns j j j Covered it . that ( j , n ] (j,n] (j,n] All the lanterns illuminate the lanterns on the left , The illuminated lantern continues to illuminate to the left , Until some p i = 0 p_i=0 pi=0 stop it ( I can't see the left ). There's only one left [ 1 , i ) [1,i) [1,i) My lantern is not illuminated , See if they can light up internally .
This is a O ( n 2 ) \mathcal O(n^2) O(n2) Of . be aware d p \tt dp dp Values are b o o l \tt bool bool type , Consider changing it to i n t \tt int int type , So as to reduce the information that needs to be processed during the transfer —— Obviously, it's using i n t \tt int int Dynamically achieve “ The illuminated lantern continues to illuminate to the left ” The process of , Just store the number of lanterns that are illuminated .
Of course, there is no need for recursion , Direct recursion . Say it completely , remember f ( i ) f(i) f(i) For the former i i i The longest prefix length that a lantern can illuminate . There are two kinds of transfer , Corresponding to the original enumeration j j j And continue to iterate back to illuminate .
- Take whatever you like j ⩾ i j\geqslant i j⩾i Satisfy j − p j ⩽ f ( i − 1 ) + 1 j-p_j\leqslant f(i{\rm-}1)+1 j−pj⩽f(i−1)+1, be f ( j ) f(j) f(j) You can use max t = i j − 1 t + p t \max_{t=i}^{j-1}t+p_t maxt=ij−1t+pt to update . Specially , if j = i j=i j=i, use ( j − 1 ) (j{\rm-}1) (j−1) to update .
- if f ( i − 1 ) ⩾ i f(i{\rm-}1)\geqslant i f(i−1)⩾i, be f ( i ) f(i) f(i) You can use max { f ( i − 1 ) , i + p i } \max\{f(i{\rm-}1),i+p_i\} max{ f(i−1),i+pi} to update .
The most interesting thing is , The first kind of transfer does not need to f ( i − 1 ) f(i{\rm-}1) f(i−1) To update , Because it was originally when it could not continue to illuminate , You need to enumerate again j j j . Understand from the formula , It's just f ( i − 1 ) < i f(i{\rm-}1)<i f(i−1)<i We need to consider the first kind of transfer , here t = i t=i t=i Have already let max \max max Take a larger value .
At this time, use the form filling method . For one j j j, Its optimal i i i The smaller the better ; So the goal is to find the smallest i i i Satisfy f ( i − 1 ) ⩾ j − p j − 1 f(i{\rm-}1)\geqslant j-p_j-1 f(i−1)⩾j−pj−1 . Monotonic stack can be achieved by two points . Query the maximum value to S T \tt ST ST surface , Or any data structure . Time complexity O ( n log n ) \mathcal O(n\log n) O(nlogn) .
Code
#include <cstdio> // JZM yydJUNK!!!
#include <iostream> // XJX yyds!!!
#include <algorithm> // XYX yydLONELY!!!
#include <cstring> // (the STRONG long for LONELINESS)
#include <cctype> // ZXY yydSISTER!!!
using namespace std;
# define rep(i,a,b) for(int i=(a); i<=(b); ++i)
# define drep(i,a,b) for(int i=(a); i>=(b); --i)
typedef long long llong;
inline int readint(){
int a = 0, c = getchar(), f = 1;
for(; !isdigit(c); c=getchar())
if(c == '-') f = -f;
for(; isdigit(c); c=getchar())
a = (a<<3)+(a<<1)+(c^48);
return a*f;
}
inline void getMax(int &x, const int &y){
if(x < y) x = y;
}
const int MAXN = 300005;
int p[MAXN];
namespace ST{
const int LOGN = 19;
int st[LOGN][MAXN], logtwo[MAXN];
void init(int n){
rep(i,2^(logtwo[1]=0),n) logtwo[i] = logtwo[i>>1]+1;
}
void build(int n){
rep(i,1,n) st[0][i] = i+p[i];
rep(j,0,LOGN-2) rep(i,1,n-(2<<j)+1)
st[j+1][i] = max(st[j][i],st[j][i+(1<<j)]);
}
inline int query(const int &l, const int &r){
const int &k = logtwo[r-l+1];
return max(st[k][l],st[k][r-(1<<k)+1]);
}
}
int best[MAXN], dp[MAXN], sta[MAXN], top;
void paint(int x){
if(!x) return ;
if(best[x] == -1){
paint(x-1), putchar('R');
return ; // covered
}
paint(best[x]);
rep(i,best[x]+1,x-1) putchar('R');
putchar('L'); // x is chosen to be left
}
bool cmp(const int &x, const int &y){
return dp[x] < y;
}
int main(){
ST::init(MAXN-1);
for(int T=readint(); T; --T){
int n = readint();
rep(i,1,n) p[i] = readint();
ST::build(n), sta[0] = top = 0;
for(int i=1; i<=n; ++i){
sta[top+1] = i; // force best[i] = i
dp[i] = (dp[i-1] >= i) ? max(dp[i-1],i+p[i]) : 0;
best[i] = *lower_bound(sta,sta+top+1,i-p[i]-1,cmp);
int got = (best[i] == i) ? -1 : ((best[i] == i-1) ?
i-1 : ST::query(best[i]+1,i-1)); // at least j-1
if(dp[i] < got) dp[i] = got; else best[i] = -1;
if(dp[sta[top]] < dp[i]) sta[++ top] = i;
}
if(dp[n] < n) puts("NO");
else puts("YES"), paint(n), putchar('\n');
}
return 0;
}
边栏推荐
- L'ouverture d'un compte d'actions en ligne est - elle sécurisée? Fiable?
- This is the latest opportunity of the London bank trend
- How to learn automated testing?
- Key points on February 15, 2022
- EasyCVR通过国标GB28181协议接入设备,出现设备自动拉流是什么原因?
- LeetCode 148. Sort linked list
- [splishsplash] about how to receive / display user parameters, MVC mode and genparam on GUI and JSON
- People help ant help task platform repair source code
- (6) VIM editor MV cat redirection standard input and output more pipe symbols head tail
- Mujoco XML modeling
猜你喜欢

How to write good code - Defensive Programming Guide

Yolov5 practice: teach object detection by hand

ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing

The difference and relationship between iteratible objects, iterators and generators

Cloud picture says | distributed transaction management DTM: the little helper behind "buy buy buy"

This is the latest opportunity of the London bank trend

ISO 27001 Information Security Management System Certification
![[PHP foundation] realize the connection between PHP and SQL database](/img/eb/c8953eddfe3b19b0adb5529957d275.jpg)
[PHP foundation] realize the connection between PHP and SQL database

Data warehouse (3) star model and dimension modeling of data warehouse modeling

Rotation order and universal lock of unity panel
随机推荐
网上股票开户安全吗?是否可靠?
Kernel stray cat stray dog pet adoption platform H5 source code
PHP implements sensitive word filtering system "suggestions collection"
Length of learning and changing
股票万1免5证券开户是合理安全的吗,怎么讲
Heavy disclosure! Hundreds of important information systems have been invaded, and the host has become a key attack target
Thinkphp6 - CMS multi wechat management system source code
網上股票開戶安全嗎?是否可靠?
Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day
Function, condition, regular expression
Is it safe to open an ETF account online? What are the steps?
New patent applications and transfers
开发那些事儿:EasyCVR平台添加播放地址鉴权
Relationship between sensor size, pixel, dpi resolution, inch and millimeter
MES production equipment manufacturing execution system software
How to learn automated testing?
Data warehouse (3) star model and dimension modeling of data warehouse modeling
Replace UUID, nanoid is faster and safer!
. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes
At present, where is the most formal and safe account opening for futures speculation? How to open a futures account?