#include <iostream> #include <vector> #include <algorithm> using namespace std; struct path { int sum; int ways; }; int main(int argc, char* argv[]) { ios_base::sync_with_stdio (false); int n, p = 1000000007, i; cin >> n ; vector<int> d(n + 1); for (i = 0; i < n; ++i) cin >> d[i]; d[n] = p; sort(d.begin(), d.end()); vector<path> a[4] = {vector<path>(n*n), vector<path>(n*n), vector<path>(n*n), vector<path>(n*n)}; int ac[4] = {1, 0, 0, 0}; int kd = 0, kp = 1, nkd = 2, nkp = 3, xd, xp, e; int sum = 0; a[0][0].ways = 1; path q; for (i = 0; i < n + 1; ++i) { e = d[i]; xd = 0; xp = 0; ac[nkd] = ac[nkp] = 0; while(1) { if (xd < ac[kd]) { if (xp < ac[kp]) { if (a[kd][xd].sum > a[kp][xp].sum) { q=a[kd][xd]; xd++; } else if (a[kd][xd].sum < a[kp][xp].sum) { q=a[kp][xp]; xp++; } else { q={a[kp][xp].sum, (a[kp][xp].ways + a[kd][xd].ways) % p}; xd++; xp++; } } else { q=a[kd][xd]; xd++; } } else { if (xp < ac[kp]) { q=a[kp][xp]; xp++; } else { break; } } if (e <= q.sum + 1) { a[nkd][ac[nkd]++] = {q.sum + e, q.ways}; a[nkp][ac[nkp]++] = {q.sum, q.ways}; } else { sum = (sum + q.ways) % p; } } e = nkp; nkp = kp; kp = e; e = nkd; nkd = kd; kd = e; } sum = (sum + p - 1) % p; cout << sum << "\n"; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #include <iostream> #include <vector> #include <algorithm> using namespace std; struct path { int sum; int ways; }; int main(int argc, char* argv[]) { ios_base::sync_with_stdio (false); int n, p = 1000000007, i; cin >> n ; vector<int> d(n + 1); for (i = 0; i < n; ++i) cin >> d[i]; d[n] = p; sort(d.begin(), d.end()); vector<path> a[4] = {vector<path>(n*n), vector<path>(n*n), vector<path>(n*n), vector<path>(n*n)}; int ac[4] = {1, 0, 0, 0}; int kd = 0, kp = 1, nkd = 2, nkp = 3, xd, xp, e; int sum = 0; a[0][0].ways = 1; path q; for (i = 0; i < n + 1; ++i) { e = d[i]; xd = 0; xp = 0; ac[nkd] = ac[nkp] = 0; while(1) { if (xd < ac[kd]) { if (xp < ac[kp]) { if (a[kd][xd].sum > a[kp][xp].sum) { q=a[kd][xd]; xd++; } else if (a[kd][xd].sum < a[kp][xp].sum) { q=a[kp][xp]; xp++; } else { q={a[kp][xp].sum, (a[kp][xp].ways + a[kd][xd].ways) % p}; xd++; xp++; } } else { q=a[kd][xd]; xd++; } } else { if (xp < ac[kp]) { q=a[kp][xp]; xp++; } else { break; } } if (e <= q.sum + 1) { a[nkd][ac[nkd]++] = {q.sum + e, q.ways}; a[nkp][ac[nkp]++] = {q.sum, q.ways}; } else { sum = (sum + q.ways) % p; } } e = nkp; nkp = kp; kp = e; e = nkd; nkd = kd; kd = e; } sum = (sum + p - 1) % p; cout << sum << "\n"; } |