当前位置:网站首页>[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;
}
边栏推荐
- Good looking UI mall source code has been scanned, no back door, no encryption
- ZABBIX alarm execute remote command
- js如何将带有分割符的字符串转化成一个n维数组
- Detailed explanation of ArrayList expansion
- Explain in detail the process of realizing Chinese text classification by CNN
- Develop those things: add playback address authentication to easycvr platform
- MySQL + JSON = King fried
- Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
- Heavy disclosure! Hundreds of important information systems have been invaded, and the host has become a key attack target
- The latest intelligent factory MES management system software solution
猜你喜欢

Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion

Replace UUID, nanoid is faster and safer!

Mujoco's biped robot Darwin model

New 95 community system whole station source code

Kia recalls some K3 new energy with potential safety hazards

Android development interview was badly hit in 3 years, and now the recruitment technical requirements are so high?

Quick foundation of group theory (5): generators, Kelley graphs, orbits, cyclic graphs, and "dimensions" of groups?

ACL 2022 | decomposed meta learning small sample named entity recognition

Rotation order and universal lock of unity panel

LeetCode 148. Sort linked list
随机推荐
[splishsplash] about how to receive / display user parameters, MVC mode and genparam on GUI and JSON
Software construction scheme of smart factory collaborative management and control application system
Single element of an ordered array
Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day
MES production equipment manufacturing execution system software
Apk signature process introduction [easy to understand]
golang中的select详解
Session layer of csframework, server and client (1)
Radhat builds intranet Yum source server
When the fixed frequency artifact falls in love with multithreading | ros2 fixed frequency topic release demo
Irradiance, Joule energy, exercise habits
ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
Is the fund of futures account safe? How to open an account?
Happy new year | 202112 monthly summary
Redis主从实现10秒检查与恢复
How to write good code - Defensive Programming Guide
Draw drawing process of UI drawing process
Convert the robot's URDF file to mujoco model
An example of data analysis of an old swatch and an old hard disk disassembly and assembly combined with the sensor of an electromagnetic press
What are the six steps of the software development process? How to draw software development flow chart?