题解:P14917 [GESP202512 五级] 数字移动

· · 题解

//时间复杂度:o(n),只用了四个变量。
#include <iostream>

using namespace std;
//边输入边处理版
int main() {
    int n,t,ans=0,ti=1e6;
    cin>>n;
    while(n--) {
        cin>>t;//输入
        if(t>ans)//小于等于ans不处理
            if(ti==t)//如果配对成功
                ti=1e6;//赋初值
            else {//配对未成功
                if(ti!=1e6)//内存已有
                    if(t<ti)//将ans设为或ti
                        ans=t;
                    else
                        ans=ti,ti=t;//若设为ti,将ti刷新为t
                else
                    ti=t;//若内存为空,存储
            }
    }
    cout<<ans;
    return 0;
}