CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes!) A ~ D

CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes!) A ~ D

A.

给定一个序列,对于任意1<=k<=n 都满足|ai−ak|+|ak−aj|=|ai−aj|,

找满足条件的i和j并输出

思路:

观察样例,发现输出的是最大值和最小值,那么猜答案是最大值和最小值,进行证明

若答案不是最大值和最小值,则一定存在一个k使得|ak-ap|大于|aj-ai| 一定不满足|ai−ak|+|ak−aj|=|ai−aj| 与命题矛盾

所以记录最大值和最小值 输出即可。

代码:

 

#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define endl "
"
#define int long long
#define debug(x) cout << "*" << x << endl;  
const int P = 13131;
#define ll long long
const int mod = 1E6 + 7;
const int INF = 0x3f, sINF = 0x3f3f3f3f;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const int N = 3e5 + 10;;
int T;
const int UN = 1e9 + 10;

signed main()
{
    cin>>T;
    while(T--)
    {
        int n;
        int maxa = 0, mina = UN;
        cin>>n;
        int ans1, ans2;
        for(int i = 1; i <= n; i++)
        {
            int temp;
            cin>>temp;
            
            if(temp > maxa) 
            {
                ans1 = i;
                maxa = temp;
            }
            
            if(temp < mina)
            {
                ans2 = i;
                mina = temp;
            }
        }
        if(n == 1) cout<<"1 1"<<endl;
        else cout<<ans2<<" "<<ans1<<endl;
        
    }
}
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes!) A ~ D