模拟+数组排序
代码如下:
#include <iostream>
#include<cstdio>
#include <algorithm>
using namespace std;
int const maxn=100000+10;
struct node
{
int val,id;
bool operator <(const node &rhs) const
{
return val<rhs.val;
}
}nod[maxn];
int main()
{
int n,i,r,l;
bool flag=1;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&nod[i].val);
nod[i].id=i;
}
//nod[n+1].id=-1;
sort(nod+1,nod+n+1);
nod[n+1].id=-1;
l=-1;
for(i=1;i<=n;i++)
if(nod[i].id!=i)
{
l=i;break;
}
if(l==-1)
{
printf("yes\n1 1\n");
return 0;
}
for(i=l;i<=n;i++)
if(nod[i].id-nod[i+1].id!=1)
{
r=i+1;break;
}
// if(i==n+1){printf("yes\n%d %d\n",l,r-1);return 0;}//加上这句虽然没错,但是多余了
for(i=r;i<=n;i++)
if(nod[i].id!=i)
{
//printf("no\n");return 0;
flag=false;//或者flag=0
break;
}
if(flag) printf("yes\n%d %d\n",l,r-1);
else printf("no\n");
return 0;
}
总结:
主要是熟悉一下结构体中重载运算符<用于sort函数的方法。重视基础,方法得当,事半功倍。