CodeForces 451B Sort the Array


title: CodeForces 451B Sort the Array
date: 2016-08-04 09:33:29
tags:
- 模拟
- CodeForces

链接:
Sort the Array

题意:
将数列中一个连续子序列逆序之后,判断是否得到一个严格递增的序列

思路:
这道题写了两种方法都没过,第三种终于过了
大概是从a[0]开始找到第一个连续递减的子序列a[start, end],判断a[end]是否大于a[start-1],a[start]是否小于a[end+1],然后看end以后是否还有逆序对

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+7;
int a[MAXN];

int main(int argc, char const *argv[])
{
	int n;
    //freopen("in.txt", "r", stdin); 
	while(~scanf("%d", &n))
	{
		memset(a, 0, sizeof(a));
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d", &a[i]);
		}
		int start = 0, end = 0, yes = 1;
		for (int i = 1; i < n; ++i)
		{
			if(!start&&a[i] > a[i+1])
			{
				start = i;
				for(int j = i; a[j] > a[j+1]&&j<n; j++)
				{
					end = j+1;
				}
				if((start>1 && a[start-1]>a[end]) || (end<n && a[start]>a[end+1]))
					{yes = 0;}
				i=end-1;
			}
			else if(a[i] > a[i+1]) yes = 0;
			
		}
		if(yes&&!start) start = end = 1;
		if(yes) printf("yes
%d %d
", start, end);
		else printf("no
");
	}
	return 0;
}

原文地址:https://www.cnblogs.com/nanf/p/CF451B.html