CF 27C Unordered Subsequence

題目鏈接:http://codeforces.com/problemset/problem/27/C


題目大意:

給定一個數列,求其中最短的非順序子序列(不一定連續)。

順序序列指降序序列和升序序列。


題目思路:

設數列爲a[].

(1)dp求出在i位置時:

往左a[i]>=a[i-1]>=..>=a[lx]和a[i]<=a[i-1]<=..<=a[ry]的最小lx和ly值。

往右a[i]>=a[i+1]>=..>=a[rx]和a[i]<=a[i+1]<=..<=a[ry]的最大rx和ry值。

(2)任意找一個出滿足a[i]!=a[lx] && a[i]!=a[rx]的位置i,

答案即爲lx i rx。

對於y同理。


題目代碼:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define ll __int64
#define ls rt<<1
#define rs ls|1
#define lson l,mid,ls
#define rson mid+1,r,rs
#define middle (l+r)>>1
#define clr_all(x,c) memset(x,c,sizeof(x))
#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1))
#define eps (1e-8)
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#pragma comment(linker, "/STACK:102400000,102400000")
template <class T> T _max(T x,T y){return x>y? x:y;}
template <class T> T _min(T x,T y){return x<y? x:y;}
template <class T> T _abs(T x){return (x < 0)? -x:x;}
template <class T> T _mod(T x,T y){return (x > 0)? x%y:((x%y)+y)%y;}
template <class T> void _swap(T &x,T &y){T t=x;x=y;y=t;}
template <class T> void getmax(T& x,T y){x=(y > x)? y:x;}
template <class T> void getmin(T& x,T y){x=(x<0 || y<x)? y:x;}
int TS,cas=1;
const int M=100000+5;
int n;
int a[M],l[M][2],r[M][2];

void run(){
	int i,j;
	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	l[1][1]=l[1][0]=1;
	for(i=2;i<=n;i++){
		if(a[i]>=a[i-1]) l[i][0]=l[i-1][0];
		else l[i][0]=i;
		if(a[i]<=a[i-1]) l[i][1]=l[i-1][1];
		else l[i][1]=i;
	}
	r[n][1]=r[n][0]=n;
	for(i=n-1;i>=1;i--){
		if(a[i]>=a[i+1]) r[i][0]=r[i+1][0];
		else r[i][0]=i;
		if(a[i]<=a[i+1]) r[i][1]=r[i+1][1];
		else r[i][1]=i;
	}
	for(i=2;i<n;i++){
		for(j=0;j<2;j++){
		int x=l[i][j],y=r[i][j];
		//printf("..%d %d %d\n",a[x],a[i],a[y]);
			if(a[i]!=a[x] && a[i]!=a[y]){
				printf("3\n%d %d %d\n",x,i,y);
				return;
			}
		}
	}
	puts("0");
}

void preSof(){
}

int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    preSof();
    //run();
    while((~scanf("%d",&n))) run();
    //for(scanf("%d",&TS);cas<=TS;cas++) run();
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章