Submission #1532358
Source Code Expand
#include "bits/stdc++.h" #include<unordered_map> #include<unordered_set> #pragma warning(disable:4996) using namespace std; using ld = long double; const ld eps = 1e-9; vector<int>primes; void hurui(const int amax) { static bool flag = false; if (flag)return; vector<int>sos; sos = vector<int>(amax + 1, true); sos[0] = false; sos[1] = false; for (int i = 2; i <= amax; ++i) { if (sos[i]) { for (int j = 2 * i; j <= amax; j += i)sos[j] = false; } } for (int i = 0; i <= amax; ++i) { if (sos[i]) { primes.push_back(i); } } flag = true; } vector<int>v; map<pair<int, int>, int>memo; int solve(int l, int r) { if (l + 1 == r)return 3; if (memo.find(make_pair(l, r)) != memo.end())return memo[make_pair(l, r)]; int ans = 0; vector<int>as(v.begin() + l, v.begin() + r); int t = as.back() - as.front() + 1; if (t % 2) { if (binary_search(primes.begin(), primes.end(), t)) { ans = 1; } else { ans = 3; } } else { ans = 2; } for (int i = 0; i < as.size() - 1; ++i) { int k = as[i + 1] - as[i] - 1; if (k % 2) { if (binary_search(primes.begin(), primes.end(), k)) { ans += 1; } else { ans += 3; } } else { if (k == 0)ans += 0; else ans += 2; } } for (int k = l + 1; k <= r - 1; ++k) { ans = min(ans, solve(l, k) + solve(k, r)); } return memo[make_pair(l,r)]=ans; } int main() { hurui(1e7); int N; cin >> N; for (int i = 0; i < N; ++i) { int a; cin >> a; v.emplace_back(a); } int aans = solve(0, N); cout << aans << endl; return 0; }
Submission Info
Submission Time | |
---|---|
Task | C - 4-adjacent |
User | yuma000 |
Language | C++14 (GCC 5.4.1) |
Score | 0 |
Code Size | 1609 Byte |
Status | WA |
Exec Time | 2121 ms |
Memory | 288724 KB |
Judge Result
Set Name | Sample | All | ||||||
---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 0 / 400 | ||||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 0_04.txt |
All | 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 0_04.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
0_00.txt | WA | 232 ms | 45040 KB |
0_01.txt | WA | 228 ms | 44144 KB |
0_02.txt | WA | 226 ms | 43888 KB |
0_03.txt | WA | 230 ms | 43760 KB |
0_04.txt | WA | 228 ms | 44656 KB |
1_00.txt | WA | 228 ms | 44272 KB |
1_01.txt | WA | 230 ms | 44912 KB |
1_02.txt | WA | 229 ms | 45552 KB |
1_03.txt | WA | 232 ms | 43504 KB |
1_04.txt | WA | 237 ms | 45040 KB |
1_05.txt | WA | 227 ms | 44272 KB |
1_06.txt | WA | 229 ms | 44912 KB |
1_07.txt | TLE | 2115 ms | 187220 KB |
1_08.txt | TLE | 2115 ms | 187592 KB |
1_09.txt | TLE | 2115 ms | 187732 KB |
1_10.txt | TLE | 2121 ms | 288724 KB |
1_11.txt | TLE | 2121 ms | 286932 KB |
1_12.txt | TLE | 2120 ms | 271060 KB |
1_13.txt | TLE | 2120 ms | 271572 KB |