結果
問題 | No.209 Longest Mountain Subsequence |
ユーザー | Kmcode1 |
提出日時 | 2015-05-15 23:28:19 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 47 ms / 2,000 ms |
コード長 | 2,368 bytes |
コンパイル時間 | 749 ms |
コンパイル使用メモリ | 99,640 KB |
実行使用メモリ | 6,940 KB |
最終ジャッジ日時 | 2024-07-06 04:17:41 |
合計ジャッジ時間 | 1,311 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 18 ms
6,812 KB |
testcase_01 | AC | 19 ms
6,940 KB |
testcase_02 | AC | 16 ms
6,940 KB |
testcase_03 | AC | 47 ms
6,940 KB |
testcase_04 | AC | 47 ms
6,940 KB |
testcase_05 | AC | 4 ms
6,940 KB |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:83:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 83 | scanf("%d", &t); | ~~~~~^~~~~~~~~~ main.cpp:85:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 85 | scanf("%d", &n); | ~~~~~^~~~~~~~~~ main.cpp:87:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 87 | scanf("%d", &a[i]); | ~~~~~^~~~~~~~~~~~~
ソースコード
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cctype> #include<cstdlib> #include<algorithm> #include<bitset> #include<vector> #include<list> #include<deque> #include<queue> #include<map> #include<set> #include<stack> #include<cmath> #include<sstream> #include<fstream> #include<iomanip> #include<ctime> #include<complex> #include<functional> #include<climits> #include<cassert> #include<iterator> using namespace std; int t; int n; #define MAX 102 int a[MAX]; vector<pair<int, int> > v[MAX]; //cost,go int use[MAX][MAX]; int dp[MAX][MAX]; int u_s = 0; inline void dfs(int aa, int b){ if (use[aa][b]==u_s){ return; } use[aa][b] = u_s; dp[aa][b] = 0; int pre = a[b] - a[aa]; int ind; if (aa == b){ pre = INT_MAX; ind = v[aa].size(); } else{ ind = lower_bound(v[aa].begin(), v[aa].end(), make_pair(pre, -1)) - v[aa].begin(); } ind--; for (int i = ind; i >= 0; i--){ dfs(v[aa][i].second, aa); dp[aa][b] = max(dp[aa][b], dp[v[aa][i].second][aa]); } dp[aa][b]++; } int dp2[MAX][MAX]; inline void dfs2(int aa, int b){ if (use[aa][b]==u_s){ return; } use[aa][b] = u_s; dp2[aa][b] = 0; int pre = a[b] - a[aa]; int ind; if (aa == b){ pre = INT_MAX; ind = v[aa].size(); } else{ ind = lower_bound(v[aa].begin(), v[aa].end(), make_pair(pre, -1)) - v[aa].begin(); } ind--; for (int i = ind; i >= 0; i--){ dfs2(v[aa][i].second, aa); dp2[aa][b] = max(dp2[aa][b], dp2[v[aa][i].second][aa]); } dp2[aa][b]++; } int main(){ scanf("%d", &t); while (t--){ scanf("%d", &n); for (int i = 0; i < n; i++){ scanf("%d", &a[i]); } for (int i = 0; i < n; i++){ v[i].clear(); } for (int i = 0; i < n; i++){ for (int j = i + 1; j < n; j++){ if (a[i] > a[j]){ v[i].push_back(make_pair(a[i] - a[j], j)); } } } for (int i = 0; i < n; i++){ sort(v[i].begin(), v[i].end()); } u_s++; for (int i = 0; i < n; i++){ dfs2(i, i); } for (int i = 0; i < n; i++){ v[i].clear(); } for (int i = 0; i < n; i++){ for (int j = i - 1; j >= 0; j--){ if (a[i]>a[j]){ v[i].push_back(make_pair(a[i] - a[j], j)); } } } for (int i = 0; i < n; i++){ sort(v[i].begin(), v[i].end()); } u_s++; int maxt = 1; for (int i = 0; i < n; i++){ dfs(i, i); maxt = max(maxt, dp[i][i] + dp2[i][i] - 1); } printf("%d\n", maxt); } return 0; }