[NYOJ] [1767] Abnormal Maximum

Problem Description

Loading...

Problem Analysis

Overestimated what C++’s built-in max/min functions could do
So I wrote one myself

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<stdio.h>
int a[10200];
int b[3500];
int ma(int m) {
	if(a[m]>=a[m+1]&&a[m]>=a[m+2])
		return a[m];
	if(a[m+1]>=a[m]&&a[m+1]>=a[m+2])
		return a[m+1];
	if(a[m+2]>=a[m+1]&&a[m+2]>=a[m])
		return a[m+2];
}
int mi(int m) {
	if(a[m]<=a[m+1]&&a[m]<=a[m+2])
		return a[m];
	if(a[m+1]<=a[m]&&a[m+1]<=a[m+2])
		return a[m+1];
	if(a[m+2]<=a[m+1]&&a[m+2]<=a[m])
		return a[m+2];
}
int main() {
	int n;
	while(scanf("%d",&n)!=EOF) {
		for(int i=0; i<n; i++)
			scanf("%d",&a[i]);
		int cnt=0;
		for(int i=0; i<n; i+=3) {
			if(cnt&1==1) {
				b[++cnt]=mi(i);
			} else {
				b[++cnt]=ma(i);
			}
		}
		int max=0;
		for(int i=1; i<=cnt; i++) {
			if(b[i]>max)
				max=b[i];
		}
		printf("%d\n",max);
	}
	return 0;
}

But the reference solution implements it with define
I should brush up on that side of things when I get the time
(Reminds me of the long string of I/O templates Yu-shen brings out every time…)

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <cstdio>
#define Max(a,b,c) a>(b>c?b:c)?a:(b>c?b:c)
#define Min(a,b,c) a>(b>c?c:b)?(b>c?c:b):a
int main() {
	int a[10005];
	int n,i,j,h;
	while(~scanf("%d",&n)) {
		int max=1<<32;
		for(i=0; i<n&&scanf("%d",&a[i]); i++) {}
		for(i=0; i<n; i+=3) {
			h=i%2==0?Max(a[i],a[i+1],a[i+2]):Min(a[i],a[i+1],a[i+2]);
			max=h>max?h:max;
		}
		printf("%d\n",max);
	}
	return 0;
}

Problem link:【NYOJ】[1767]Abnormal Maximum Value

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...