栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Codeforces Global Round 17 B 双指针 模拟

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Codeforces Global Round 17 B 双指针 模拟

题目

给你一个序列A
要求只删除一个值(删除他的一部分或者全部或者不删)
使得原序列为回文序列
4 2 1 2 4
1 2 1
特别的
单个值也是回文序列
1

题解思路

第二次碰到这种题目了,还是Wa了一发。

直接两个指针一头一尾。
碰到不同的时候就是不是你死就是我亡。
肯定是选择删除两个种的一个。
枚举即可。

AC代码
#include 
//#include 
//priority_queue
#define PII pair
#define ll long long

using namespace std;

const  int  INF =  0x3f3f3f3f;
const  int N = 200100 ;
int a[N] ; 
int n ;

int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int T ;
    cin >> T ;
    while (T--)
    {
        cin >> n ;
        for (int i = 1 ; i <= n ; i++ )
            cin >> a[i] ; 
        if ( n <= 2 )
            cout << "YESn";
        else
        {
            int t1 = 1 , t2 = n ;
            bool falg = 1 ; 
            int ck = -1 ; 
            while (t1 < t2 )
            {
                if ( a[t1] != a[t2] && ck == -1 )
                {
                    ck = a[t1] ; 
                    falg = 1 ; 
                    int s1 = t1 , s2 = t2 ; 
                    while ( t1 < t2 )
                    {
                        if ( a[t1] != a[t2] && ck != -1 )
                        {
                            if ( a[t1] == ck )
                                t1++;
                            else if (a[t2] == ck )
                                t2--;
                            else
                            {
                                falg = 0 ;
                                break ; 
                            }
                        }
                        if (a[t1] == a[t2] )
                        {
                            t1++;
                            t2--;
                        }
                    }
                    if (falg)
                        break ;
                    t1 = s1 , t2 = s2 ; 
                    falg = 1 ; 
                    ck = a[t2] ;
                    while ( t1 < t2 )
                    {
                        if ( a[t1] != a[t2] && ck != -1 )
                        {
                            if ( a[t1] == ck )
                                t1++;
                            else if (a[t2] == ck )
                                t2--;
                            else
                            {
                                falg = 0 ;
                                break ; 
                            }
                        }
                        if (a[t1] == a[t2] )
                        {
                            t1++;
                            t2--;
                        }
                    }
                    break ; 
                }
                if (a[t1] == a[t2] )
                {
                    t1++;
                    t2--;
                }
            }
            if (falg)
                cout << "YESn" ;
            else
                cout << "NOn" ;
        }
    }
    return 0 ;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/588928.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号