问题 J: Exception Handling
题目描述
You are given a sequence of length N: A1,A2,...,AN. For each integer i between 1 and N (inclusive), answer the following question:
Find the maximum value among the N−1 elements other than Ai in the sequence.
Constraints
·2≤N≤200000
·1≤Ai≤200000
·All values in input are integers.
Find the maximum value among the N−1 elements other than Ai in the sequence.
Constraints
·2≤N≤200000
·1≤Ai≤200000
·All values in input are integers.
输入
Input is given from Standard Input in the following format:
N
A1
:
AN
N
A1
:
AN
输出
Print N lines. The i-th line (1≤i≤N) should contain the maximum value among the N−1 elements other than Ai in the sequence.
样例输入
【样例1】
3
1
4
3
【样例2】
2
5
5
样例输出
【样例1】
4
3
4
【样例2】
5
5
提示
样例1解释:
The maximum value among the two elements other than A1, that is, A2=4 and A3=3, is 4.
The maximum value among the two elements other than A2, that is, A1=1 and A3=3, is 3.
The maximum value among the two elements other than A3, that is, A1=1 and A2=4, is 4.
The maximum value among the two elements other than A1, that is, A2=4 and A3=3, is 4.
The maximum value among the two elements other than A2, that is, A1=1 and A3=3, is 3.
The maximum value among the two elements other than A3, that is, A1=1 and A2=4, is 4.
给你n个数,输出除了第i个数字之外的n -- 1个最大的那一个
我们只需要找出最大和第二大即可
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 2e5 + 10; 4 int n,a[maxn]; 5 int main(){ 6 //freopen("in","r",stdin); 7 ios::sync_with_stdio(0); 8 cin >> n; 9 int b = 0,c = 0;//c表示最大的,b第二大 10 for(int i = 0; i < n; i++){ 11 cin >> a[i]; 12 if(a[i] > c) b = c,c = a[i]; 13 else if(a[i] > b) b = a[i]; 14 } 15 for(int i = 0; i < n; i++){ 16 if(a[i] == c) cout << b << endl; 17 else cout << c << endl; 18 } 19 return 0; 20 }