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

来源:互联网 发布:虚拟机无法桥接网络 编辑:程序博客网 时间:2024/06/07 00:00

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 containsn space-separated integers — the given sequence. All numbers in this sequence do not exceed106 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 outputk integers from the range [1..n] — indexes of the elements of this subsequence. If there are several solutions, output any of them.

Examples
Input
567 499 600 42 23
Output
31 3 5
Input
31 2 3
Output
0
Input
32 3 1
Output
31 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].rusing 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;}  


原创粉丝点击