結果
問題 | No.910 素数部分列 |
ユーザー |
![]() |
提出日時 | 2019-10-18 23:30:11 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,069 bytes |
コンパイル時間 | 726 ms |
コンパイル使用メモリ | 74,068 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-06-25 19:55:26 |
合計ジャッジ時間 | 2,548 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 WA * 21 |
ソースコード
#include <string>#include <vector>#include <iostream>#include <algorithm>using namespace std;int solve(string str) {int n = str.size();vector<int> lp(n + 1), rp(n + 1);int l = 0, r = n - 1, cnt = 0;while (true) {while (l != n && str[l] == '9') ++l;while (r != -1 && str[r] == '1') --r;if (l >= r) break;lp[l] = ++cnt;++l; --r;}l = 0, r = n - 1, cnt = 0;while (true) {while (l != n && str[l] == '1') ++l;++l;while (l < n && str[l] == '1') ++l;while (r != -1 && str[r] == '9') --r;if (l >= r) break;rp[r + 1] = ++cnt;++l; --r;}int c1 = 0;for (int i = 0; i < n; ++i) {c1 += (str[i] == '1');}int mx = 0, ans = 0;for (int i = 0; i <= n; ++i) {mx = max(mx, lp[i]);int n1 = min(mx + rp[i], c1);ans = max(ans, n1 + (c1 - n1) / 2);}return ans;}int main() {int n;string s;cin >> n >> s;string str;int ans = 0;for (int i = 0; i < s.size(); ++i) {if (s[i] == '1' || s[i] == '9') {str += s[i];}else {++ans;}}ans += solve(str);cout << ans << endl;return 0;}