首页 > 其他 > 详细

[CF1479B1/CF1480D1] Painting the Array I

时间:2021-02-08 10:49:24      阅读:41      评论:0      收藏:0      [点我收藏+]

[CF1479B1/CF1480D1] Painting the Array I

Description

将一个序列拆成两个子序列,然后每个子序列中相邻相同的元素只保留一个,最大化剩下元素的个数。

Solution

贪心,决定每个元素 \(a[i]\) 放在哪里,取决于 \(a[i],a[i+1]\) 和当前两个已有子序列的末尾

#include <bits/stdc++.h>
using namespace std;

int n;
int a[100005];

signed main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    a[n+1]=-1;
    int top1=-1,top2=-1,ans=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]==top1 && a[i]==top2) continue;
        else if(a[i]==top1) 
        {
            top2=a[i];
            ans++;
        }
        else if(a[i]==top2)
        {
            top1=a[i];
            ans++;
        }
        else 
        {
            ans++;
            if(a[i+1]==top1 && a[i+1]!=top2) 
            {
                top1=a[i];
            }
            else if(a[i+1]!=top1 && a[i+1]==top2)
            {
                top2=a[i];
            }
            else 
            {
                top1=a[i];
            }
        }
    }
    cout<<ans<<endl;
}

[CF1479B1/CF1480D1] Painting the Array I

原文:https://www.cnblogs.com/mollnn/p/14387530.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!