結果
| 問題 | No.1095 Smallest Kadomatsu Subsequence |
| コンテスト | |
| ユーザー |
hanbei_dayo
|
| 提出日時 | 2020-07-16 11:09:31 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 408 ms / 2,000 ms |
| コード長 | 1,300 bytes |
| 記録 | |
| コンパイル時間 | 993 ms |
| コンパイル使用メモリ | 107,604 KB |
| 実行使用メモリ | 14,336 KB |
| 最終ジャッジ日時 | 2024-11-24 08:28:31 |
| 合計ジャッジ時間 | 5,402 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 30 |
ソースコード
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<functional>
#include<math.h>
#include<random>
#include <bitset>
using namespace std;
#define N (1000000000+7)
//#define N 998244353
#define INF 1e16
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> Q;
const int inf = (int)1e9;
int main(void){
int n;
cin>>n;
vector<ll>a(n);
for(int i=0;i<n;i++)cin>>a[i];
reverse(a.begin(),a.end());
set<ll>s1,s2;
s1.insert(a[0]);
for(int i=1;i<n;i++)s2.insert(a[i]);
ll ans = (ll)INF;
for(int i=1;i<n-1;i++){
s2.erase(a[i]);
auto itr1 = s1.upper_bound(a[i]);
auto itr2 = s2.upper_bound(a[i]);
if(itr1!=s1.end()&&itr2!=s2.end()){
ans = min(ans,a[i]+*(itr1)+*(itr2));
}
itr1 = s1.lower_bound(a[i]);
itr2 = s2.lower_bound(a[i]);
if(itr1==s1.begin()){
s1.insert(a[i]);
continue;
}
if(itr2==s2.begin()){
s1.insert(a[i]);
continue;
}
ans = min(ans,a[i]+*(s1.begin())+*(s2.begin()));
s1.insert(a[i]);
}
if(ans == (ll)INF)cout<<-1<<endl;
else cout<<ans<<endl;
return 0;
}
hanbei_dayo