code force-27C---Unordered Subsequence (模拟+暴力)

C. Unordered Subsequence

time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

The sequence is called ordered if it is non-decreasing or non-increasing. For example, sequnces [3, 1, 1, 0] and [1, 2, 3, 100] are ordered, but the sequence [1, 3, 3, 1] is not. You are given a sequence of numbers. You are to find it's shortest subsequence which is not ordered.

A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.

Input

The first line of the input contains one integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers — the given sequence. All numbers in this sequence do not exceed 106 by absolute value.

Output

If the given sequence does not contain any unordered subsequences, output 0. Otherwise, output the length k of the shortest such subsequence. Then output k integers from the range [1..n] — indexes of the elements of this subsequence. If there are several solutions, output any of them.

Examples
Input
5
67 499 600 42 23
Output
3
1 3 5
Input
3
1 2 3
Output
0
Input
3
2 3 1
Output
3
1 2 3


题意: 题目规定非递增和非递减的序列为有序的序列,然后题目要求找到一个最短的无序的序列,可以不连续;

思路:如果这个序列存在,那么肯定最短是3个,有两种形式,小大小,大小大;想清楚这个后就很好办了,拿小大小这种情况举例子,首先从第一个数开始扫找到第一个a[i]<a[i+1]的转折点,然后此时的a[i]就为第一个数,再继续往后扫,如果能找到a[i]>a[i+1]这个转折点,此时的a[i]为第二个数,a[i+1]为第三个数;第二种情况一样;

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#define inf 0xfffffff
#define maxn 100100
#define ll  segtree[root].l
#define rr  segtree[root].r
using namespace std;
int n;
int a[100010];
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        int p;
        int x,y,z;
        int flag=0,k=0;
        //第一种情况
        for(int i=1;i<n;i++)
        {
            if(a[i]<a[i+1])
            {
                 x=i;p=i+1;
                 k=1;
                 break;
            }
        }
        if(k)
        {
            for(int i=p;i<n;i++)
            {
                if(a[i]>a[i+1])
                {
                    y=i;
                    z=i+1;
                    flag=1;
                    break;
                }
            }
        }
        if(flag==0)
        {
            k=0;
            for(int i=1;i<n;i++)//第二种情况
            {
                if(a[i]>a[i+1])
                {
                    x=i;
                    y=i+1;
                    p=i+1;
                    k=1;

                    break;
                }
            }
            if(k)
            {
                for(int i=p;i<n;i++)
                {
                    if(a[i]<a[i+1])
                    {
                        z=i+1;
                        flag=1;
                        break;
                    }
                }
            }

        }
        if(flag)
        {
            printf("3\n");
            printf("%d %d %d\n",x,y,z);
        }
        else printf("0\n");
    }
    return 0;
}  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值