当前位置:网站首页>Two merged sequences (CF 1144 g) -dp
Two merged sequences (CF 1144 g) -dp
2022-06-09 03:09:00 【Tan_ Yuu】
Topic link ;
Reference resources ;
Ideas
Build state representation :
d p [ i ] [ 1 ] dp[i][1] dp[i][1] Representative in front i In a , Ruodi i Bit is the end of the ascending sequence , The maximum value at the end of the descending sequence ;
o p [ i ] [ 1 ] op[i][1] op[i][1] For and on behalf of i Bit is the end of the ascending sequence , d p [ i ] [ 1 ] dp[i][1] dp[i][1] When the current value is taken , The first i-1 Bits in ascending sequence / Descending sequence (1 l 0 drop );
d p [ i ] [ 0 ] dp[i][0] dp[i][0] Representative in front i In a , Ruodi i Bit is the end of the descending sequence , The minimum value at the end of the ascending sequence ;
o p [ i ] [ 0 ] op[i][0] op[i][0] For and on behalf of i Bit is the end of the descending sequence , d p [ i ] [ 0 ] dp[i][0] dp[i][0] When the current value is taken , The first i-1 Bits in ascending sequence / Descending sequence (1 l 0 drop );
Define the initial value :
d p [ i ] [ 1 ] = − i n f , d p [ i ] [ 1 ] = i n f dp[i][1]=-inf,dp[i][1]=inf dp[i][1]=−inf,dp[i][1]=inf That is, each bit cannot participate in the sequence by default , It can not be updated until the conditions are met when the equation is transferred ;
Specifically , d p [ 1 ] [ 1 ] = i n f , d p [ 1 ] [ 0 ] = − i n f dp[1][1]=inf,dp[1][0]=-inf dp[1][1]=inf,dp[1][0]=−inf That is, the first bit can be in the ascending sequence , Or in descending sequence ;
State transition equation :
d p [ i ] [ 1 ] = d p [ i − 1 ] [ 1 ] , o p [ i ] [ 1 ] = 1 ; ( a [ i − 1 ] < a [ i ] & & d p [ i ] [ 1 ] < d p [ i − 1 ] [ 1 ] ) dp[i][1] = dp[i - 1][1],op[i][1] = 1;(a[i - 1] < a[i] \&\& dp[i][1] < dp[i - 1][1]) dp[i][1]=dp[i−1][1],op[i][1]=1;(a[i−1]<a[i]&&dp[i][1]<dp[i−1][1]) This bit is followed by the preceding bit in ascending sequence
d p [ i ] [ 1 ] = a [ i − 1 ] , o p [ i ] [ 1 ] = 0 ; ( d p [ i − 1 ] [ 0 ] < a [ i ] & & d p [ i ] [ 1 ] < a [ i − 1 ] ) dp[i][1] = a[i - 1],op[i][1] = 0;(dp[i - 1][0] < a[i] \&\& dp[i][1] < a[i - 1]) dp[i][1]=a[i−1],op[i][1]=0;(dp[i−1][0]<a[i]&&dp[i][1]<a[i−1]) The first bit ends the descending sequence , This bit ends the ascending sequence
d p [ i ] [ 0 ] = d p [ i − 1 ] [ 0 ] , o p [ i ] [ 0 ] = 0 ; ( a [ i − 1 ] > a [ i ] & & d p [ i ] [ 0 ] > d p [ i − 1 ] [ 0 ] ) dp[i][0] = dp[i - 1][0],op[i][0] = 0;(a[i - 1] > a[i] \&\& dp[i][0] > dp[i - 1][0]) dp[i][0]=dp[i−1][0],op[i][0]=0;(a[i−1]>a[i]&&dp[i][0]>dp[i−1][0]) This bit is followed by the preceding bit in descending order
d p [ i ] [ 0 ] = a [ i − 1 ] , o p [ i ] [ 0 ] = 1 ; ( d p [ i − 1 ] [ 1 ] > a [ i ] & & d p [ i ] [ 0 ] > a [ i − 1 ] ) dp[i][0] = a[i - 1],op[i][0] = 1;(dp[i - 1][1] > a[i] \&\& dp[i][0] > a[i - 1]) dp[i][0]=a[i−1],op[i][0]=1;(dp[i−1][1]>a[i]&&dp[i][0]>a[i−1]) The preceding bit ends the ascending sequence , This bit ends the descending sequence
In the above two restrictions , The former condition determines the feasibility of this situation , The latter condition determines the necessity of making an update ;
about dp After the results of the , as long as d p [ i ] [ j ] dp[i][j] dp[i][j] Not the initial value , It means that there is a feasible solution in this case , adopt o p [ i ] [ j ] op[i][j] op[i][j] The direction of the mark can be traced back ;
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x7f7f7f7f;
int a[200005];
int dp[200005][2];
int op[200005][2];
int opt[200005];
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
dp[1][1] = inf, dp[1][0] = -inf;
for (int i = 2; i <= n; i++)
{
dp[i][1] = -inf, dp[i][0] = inf;
if (a[i - 1] < a[i] && dp[i][1] < dp[i - 1][1])
{
dp[i][1] = dp[i - 1][1];
op[i][1] = 1;
}
if (dp[i - 1][0] < a[i] && dp[i][1] < a[i - 1])
{
dp[i][1] = a[i - 1];
op[i][1] = 0;
}
if (a[i - 1] > a[i] && dp[i][0] > dp[i - 1][0])
{
dp[i][0] = dp[i - 1][0];
op[i][0] = 0;
}
if (dp[i - 1][1] > a[i] && dp[i][0] > a[i - 1])
{
dp[i][0] = a[i - 1];
op[i][0] = 1;
}
}
if (dp[n][1] != -inf)
{
printf("YES\n");
for (int i = n, optmp = 1; i >= 1; i--)
{
opt[i] = optmp;
optmp = op[i][optmp];
}
for (int i = 1; i <= n; i++)
{
printf("%d ", opt[i] != 1);
}
}
else if (dp[n][0] != inf)
{
printf("YES\n");
for (int i = n, optmp = 0; i >= 1; i--)
{
opt[i] = optmp;
optmp = op[i][optmp];
}
for (int i = 1; i <= n; i++)
{
printf("%d ", opt[i] != 1);
}
}
else
printf("NO");
return 0;
}
边栏推荐
- New textbook for self taught examination-p292
- Ccf-csp 201903-1 small, medium and large
- How to apply for compulsory redemption of closed financial products?
- Now VB6.0 has been connected to SQL, but when using the query function, you can't query with any conditions. The situation on the Internet is not consistent with mine. How can you implement it?
- In unity, inherit the lifecycle of monobehavior game objects
- Ccf-csp 201803-4 chess game evaluation +dfs
- STM32 flash erase crash
- [detailed explanation of kubernetes 12] - Security Certification
- About me [my 2022]
- ClassNotFoundException vs NoClassDefFoundError
猜你喜欢

Jsnpp框架的全链式语法初探

Leetcode 454. Quad add II hash

Unity中,继承MonoBehaviour游戏对象的生命周期

Ccf-csp 201903-3 damaged RAID5 70 points to be optimized

FPGA初次尝试

Embracing out of hospital prescription drugs, Internet medicine should also "get rid of virtual reality"?

Ccf-csp 201812-4 data center minimum spanning tree

Ccf-csp 202104-1 gray histogram 100 points

Linux 安装Mysql 详细教程(图文教程)

The writing speed is increased by tens of times. The application of tdengine in tostar intelligent factory solution
随机推荐
ClassNotFoundException vs NoClassDefFoundError
Jsnpp框架的全链式语法初探
ERP starts from internal integration
Embedded related open source projects, libraries and materials
FPGA初次尝试
Free e-book reading platform
Calendar time operation
Ccf-csp 201412-3 call auction
Linux MySQL installation tutorial (Graphic tutorial)
Ccf-csp 201503-3 Festival
Leetcode 238. Product of arrays other than itself
Basic method for missing data filling (2) -- random forest (missforest) filling
Redis data storage
Dest0g3 520 orientation web easyssti
Ranking list of short-term financial products in 2022
Leetcode 1155. N façons de rouler les dés
Shocked, you can't debug remotely
Tamidog information | Maersk completed another large-scale enterprise acquisition
Unity中,继承MonoBehaviour游戏对象的生命周期
Common commands for detecting Huawei network devices