結果
| 問題 | No.1095 Smallest Kadomatsu Subsequence |
| コンテスト | |
| ユーザー |
chocorusk
|
| 提出日時 | 2020-06-27 12:37:29 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 330 ms / 2,000 ms |
| コード長 | 1,778 bytes |
| 記録 | |
| コンパイル時間 | 1,249 ms |
| コンパイル使用メモリ | 133,556 KB |
| 最終ジャッジ日時 | 2025-01-11 12:46:39 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 30 |
ソースコード
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const int INF=1e9+7;
int solve1(int n, vector<int> a){
int mnl[200020], mnr[200020];
mnl[0]=INF, mnr[n]=INF;
for(int i=0; i<n; i++){
mnl[i+1]=min(mnl[i], a[i]);
}
for(int i=n-1; i>=0; i--){
mnr[i]=min(mnr[i+1], a[i]);
}
int ans=INF;
for(int i=0; i<n; i++){
if(mnl[i]<a[i] && mnr[i+1]<a[i]) ans=min(ans, a[i]+mnl[i]+mnr[i+1]);
}
return ans;
}
int solve2(int n, vector<int> a){
int mnl[200020], mnr[200020];
set<int> st; st.insert(a[0]);
for(int i=1; i<n-1; i++){
auto itr=st.upper_bound(a[i]);
if(itr==st.end()) mnl[i]=INF;
else mnl[i]=(*itr);
st.insert(a[i]);
}
st.clear();
st.insert(a[n-1]);
for(int i=n-2; i>=1; i--){
auto itr=st.upper_bound(a[i]);
if(itr==st.end()) mnr[i]=INF;
else mnr[i]=(*itr);
st.insert(a[i]);
}
int ans=INF;
for(int i=1; i<n-1; i++){
ans=min(ans, a[i]+mnl[i]+mnr[i]);
}
return ans;
}
int main()
{
int n;
cin>>n;
vector<int> a(n);
for(int i=0; i<n; i++) cin>>a[i];
int ans=solve1(n, a);
ans=min(ans, solve2(n, a));
if(ans>=INF) cout<<-1<<endl;
else cout<<ans<<endl;
return 0;
}
chocorusk