Codeforces 486E LIS of Sequence

本文介绍了一种针对最长递增子序列(LIS)问题的解决方案,通过前后遍历数列并记录相关信息来判断每个元素是否属于所有的LIS、部分LIS或是不属于任何LIS。文中提供了一个详细的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:给出一个数列,判断每个数:

1:不在任何lis中

2:在lis中,但不在所有的lis中(就是lis不一定要经过他)

3:在所有lis中

思路:

对于1,前后各跑一遍,然后记录一下以它开始(到它结束)的lis长度,若开始和结束的两个长度和<len+1(len为数列lis长度),则为1(应该很显然)

对于3,记录所有不是1情况的数以它开始的lis长度,若一个长度出现>=1次,那么所有为这种长度的数均不是必经过的数(就是为2),否则为1(似乎也蛮明显)

然后就好了。。

代码:

#include <bits/stdc++.h>
#define N 100009
using namespace std;
int n,m,a[N],Stack[N],top,x,now[N],Now[N],Ans;
int pd[N];
char b[N];
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]);
    Stack[++top]=a[n];
    now[n]=1;
    for (int i=n-1;i>=1;i--)
    {
        if (a[i]<Stack[top]) Stack[++top]=a[i],now[i]=top;
        else
        {
            int l=1,r=top,ans=0;
            while (l<=r)
            {
                int mid=l+r>>1;
                if (Stack[mid]>a[i]) ans=mid,l=mid+1;
                else r=mid-1;
            }
            Stack[ans+1]=a[i];
            now[i]=ans+1;
        }
    }
    Ans=top;
    top=0;
    Stack[++top]=a[1];
    Now[1]=1;
    for (int i=2;i<=n;i++)
    {
        if (a[i]>Stack[top]) Stack[++top]=a[i],Now[i]=top;
        else
        {
            int l=1,r=top,ans=0;
            while (l<=r)
            {
                int mid=l+r>>1;
                if (Stack[mid]>=a[i]) ans=mid,r=mid-1;
                else l=mid+1;
            }
            Stack[ans]=a[i];
            Now[i]=ans;
        }
    }
    for (int i=1;i<=n;i++)
    	if (now[i]+Now[i]<Ans+1) b[i]=49;
    	else b[i]=50,pd[Now[i]]++;
    for (int i=1;i<=n;i++)
    	if (b[i]==50&&pd[Now[i]]==1) b[i]++;
    b[n+1]='\0';
    puts(b+1);
    return 0;
}


### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值