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
| Problem | Lang | Verdict | Time | Memory |
|---|---|---|---|---|
| A - Three Indices | GNU C++17 | Accepted | 46 ms | 3900 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;
}
本文解析了Educational Codeforces Round 91中A题“ThreeIndices”的解题思路,通过标记数组左侧和右侧最小值位置来寻找峰值,附带AC代码及样例模拟,适合初学者学习。
296

被折叠的 条评论
为什么被折叠?



