結果
問題 | No.1095 Smallest Kadomatsu Subsequence |
ユーザー | Drice |
提出日時 | 2020-06-26 23:17:58 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 485 ms / 2,000 ms |
コード長 | 2,511 bytes |
コンパイル時間 | 461 ms |
コンパイル使用メモリ | 47,488 KB |
実行使用メモリ | 8,576 KB |
最終ジャッジ日時 | 2024-07-04 23:57:03 |
合計ジャッジ時間 | 6,247 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 3 ms
5,376 KB |
testcase_03 | AC | 3 ms
5,376 KB |
testcase_04 | AC | 3 ms
5,376 KB |
testcase_05 | AC | 3 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 3 ms
5,376 KB |
testcase_08 | AC | 3 ms
5,376 KB |
testcase_09 | AC | 3 ms
5,376 KB |
testcase_10 | AC | 3 ms
5,376 KB |
testcase_11 | AC | 3 ms
5,376 KB |
testcase_12 | AC | 3 ms
5,376 KB |
testcase_13 | AC | 18 ms
5,376 KB |
testcase_14 | AC | 19 ms
5,376 KB |
testcase_15 | AC | 19 ms
5,376 KB |
testcase_16 | AC | 19 ms
5,376 KB |
testcase_17 | AC | 19 ms
5,376 KB |
testcase_18 | AC | 18 ms
5,376 KB |
testcase_19 | AC | 19 ms
5,376 KB |
testcase_20 | AC | 19 ms
5,376 KB |
testcase_21 | AC | 18 ms
5,376 KB |
testcase_22 | AC | 18 ms
5,376 KB |
testcase_23 | AC | 478 ms
8,492 KB |
testcase_24 | AC | 480 ms
8,516 KB |
testcase_25 | AC | 482 ms
8,516 KB |
testcase_26 | AC | 485 ms
8,576 KB |
testcase_27 | AC | 483 ms
8,388 KB |
testcase_28 | AC | 279 ms
8,420 KB |
testcase_29 | AC | 278 ms
8,260 KB |
testcase_30 | AC | 374 ms
8,444 KB |
testcase_31 | AC | 376 ms
8,576 KB |
testcase_32 | AC | 377 ms
8,484 KB |
ソースコード
#include<cstdio> #include<algorithm> int a[200005]; int b[200005]; int c[200005][2]; int d[200005]; int map[200005]; int cnt[200005]; int min(int a,int b){ return a>b?b:a; } void change(int p,int v,int n,int t){ while(p<=n){ c[p][t] += v; p += (p&-p); } } int ask(int p,int t){ int res = 0; while(p!=0){ res += c[p][t]; p -= (p&-p); } return res; } int getId(int u,int n){ int L = 1, R = n; int res = -1; while(L<=R){ int M = (L+R)/2; if(map[M]<=u){ res = M; L = M+1; } else R = M-1; } return res; } int preWork(int n){ std::sort(d+1,d+1+n); int p = 1, size = 0; while(p<=n){ int np = p; while(np+1<=n && d[np+1]==d[p]) np++; map[++size] = d[p]; cnt[size] = np-p+1; p = np+1; } for(int i = 1; i <= size; i++){ change(i,cnt[i],size,1); } return size; } int getBigger(int u,int n,int t){ int L = u+1, R = n; int res = -1; int base = ask(u,t); while(L<=R){ int M = (L+R)/2; if(ask(M,t)>base){ res = M; R = M-1; } else L = M+1; } return res; } int solve(int n,int size){ int minn = 1e9+7, ans = 1e9+7; for(int i = 2; i <= n-1; i++){ minn = min(minn,a[i-1]); int last = getId(a[i-1],size); change(last,1,size,0); change(last,-1,size,1); int u = getId(a[i],size); if(minn<a[i]){ int right = getBigger(getId(minn,size),size,1); if(right!=-1 && right<u) ans = min(ans,minn+a[i]+map[right]); } int left = getBigger(u,size,0), right = -1; if(left!=-1) right = getBigger(left,size,1); //printf("i = %d: l = %d, r = %d\n",i,left,right); //if(i==2) printf("%d %d\n",ask(2,0),ask(3,0)); if(left!=-1 && right!=-1) ans = min(ans,map[left]+map[right]+a[i]); } return ans; } int main(){ int n; scanf("%d",&n); for(int i = 1; i <= n; i++){ scanf("%d",&a[i]); b[n-i+1] = a[i]; d[i] = a[i]; } int ans = 1e9+7; int size = preWork(n); ans = min(ans,solve(n,size)); for(int i = 1; i <= n; i++) a[i] = b[i]; for(int i = 1; i <= 200000; i++) c[i][1] = c[i][0] = 0; for(int i = 1; i <= size; i++) change(i,cnt[i],size,1); ans = min(ans,solve(n,size)); if(ans<1000000000) printf("%d\n",ans); else printf("-1\n"); return 0; }