[codeforces 1380A] Three Indices 标记左侧最小值位置,标记右侧最小值位置

Educational Codeforces Round 91 (Rated for Div. 2)   参与排名人数6879   不熬夜打比赛的感觉真好

[codeforces 1380A]   Three Indices   标记左侧最小值位置,标记右侧最小值位置

总目录详见https://blog.csdn.net/mrcrack/article/details/103564004

在线测评地址https://codeforces.com/contest/1380/problem/A

ProblemLangVerdictTimeMemory
A - Three Indices GNU C++17Accepted46 ms3900 KB

题目大意:给定一个排列数组成的数组,问是否存在峰值,若存在,输出峰值的左侧小于峰值的位置,峰值位置,峰值的右侧小于峰值的位置。

基本思路:l[i]标记起始位置到i的最小值所处的位置,r[i]标记终点位置到i的最小值所处的位置,判定,若l[i]!=i&&r[i]!=i,那么该值符合题意。

样例模拟如下:

4
2 1 4 3
位置 1 2 3 4
a[i] 2 1 4 3
l[i] 1 2 2 2
r[i] 2 2 4 4

i=3时,l[3]=2,r[3]=4符合题意
输出
YES
2 3 4

6
4 6 1 2 5 3
位置 1 2 3 4 5 6
a[i] 4 6 1 2 5 3
l[i] 1 1 
r[i] 3 3

i=2时,l[2]=1,r[2]=3符合题意
输出
YES
1 2 3


5
5 3 1 2 4

位置 1 2 3 4 5
a[i] 5 3 1 2 4
l[i] 1 2 3 3 3
r[i] 3 3 3 4 5

找不到符合题意的i
输出
NO

AC代码如下:

#include <stdio.h>
#define maxn 1100
int a[maxn],l[maxn],r[maxn];
int main(){
	int t,n,i,mn,flag;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		for(i=1;i<=n;i++)scanf("%d",&a[i]);
		mn=maxn;
		for(i=1;i<=n;i++)//找a[i]左侧最小值的位置
			if(mn>a[i])mn=a[i],l[i]=i;
			else l[i]=l[i-1];
		mn=maxn;
		for(i=n;i>=1;i--)//找a[i]右侧最小值的位置
			if(mn>a[i])mn=a[i],r[i]=i;
			else r[i]=r[i+1];
		flag=0;
		for(i=1;i<=n;i++)
			if(l[i]!=i&&r[i]!=i){flag=1;break;}//若i位置的值可以充当峰值
		if(flag)printf("YES\n"),printf("%d %d %d\n",l[i],i,r[i]);
		else printf("NO\n");
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值