A. Good Pairs
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an array a1,a2,…,ana1,a2,…,an of positive integers. A good pair is a pair of indices (i,j)(i,j) with 1≤i,j≤n1≤i,j≤n such that, for all 1≤k≤n1≤k≤n, the following equality holds:
|ai−ak|+|ak−aj|=|ai−aj|,|ai−ak|+|ak−aj|=|ai−aj|,
where |x||x| denotes the absolute value of xx.
Find a good pair. Note that ii can be equal to jj.
Input
The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer nn (1≤n≤1051≤n≤105) — the length of the array.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) where aiai is the ii-th element of the array.
The sum of nn for all test cases is at most 2⋅1052⋅105.
Output
For each test case, print a single line with two space-separated indices ii and jj which form a good pair of the array. The case i=ji=j is allowed. It can be shown that such a pair always exists. If there are multiple good pairs, print any of them.
Example
input
Copy
3 3 5 2 7 5 1 4 2 2 3 1 2
output
Copy
2 3 1 2 1 1
Note
In the first case, for i=2i=2 and j=3j=3 the equality holds true for all kk:
- k=1k=1: |a2−a1|+|a1−a3|=|2−5|+|5−7|=5=|2−7|=|a2−a3||a2−a1|+|a1−a3|=|2−5|+|5−7|=5=|2−7|=|a2−a3|,
- k=2k=2: |a2−a2|+|a2−a3|=|2−2|+|2−7|=5=|2−7|=|a2−a3||a2−a2|+|a2−a3|=|2−2|+|2−7|=5=|2−7|=|a2−a3|,
- k=3k=3: |a2−a3|+|a3−a3|=|2−7|+|7−7|=5=|2−7|=|a2−a3||a2−a3|+|a3−a3|=|2−7|+|7−7|=5=|2−7|=|a2−a3|.
import sys
import math
#import numpy as np
#sys.stdin = open("in.txt", "r")
T = int(input())
for _ in range(T):
n= int(input())
arr=[int(i) for i in input().split()]
maxi=0
mini=0
for i in range(len(arr)):
if arr[maxi]<arr[i]:
maxi=i
if arr[mini]>arr[i]:
mini=i
if maxi>mini:
maxi,mini=mini,maxi
print('{} {}'.format(maxi+1,mini+1))
最大最小数满足这个条件
本文介绍了一个Codeforces上的算法题目——寻找数组中的GoodPair。通过找出数组中的最大值和最小值的位置来快速解决该问题,实现简单且高效。
1741

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



