#include <cstdio> static int c[1000000] = {}; static int d[1000000] = {}; static int dp[1000001] = {}; static int dp2[1000001] = {}; static int n; const int MODULUS = 1000000007; inline void updateIfGreater(int &a, int b) { if (a < b) { a = b; } } bool test(int i, int k) { int start = i - k; if (start < 0) { return false; } for (int j = start; j < i; ++j) { if (!(c[j] <= k && k <= d[j])) { return false; } } return true; } int f(int n) { dp[0] = 0; dp2[0] = 1; for (int i = 1; i <= n; ++i) { int result = 0; for (int k = c[i - 1]; k <= d[i - 1]; ++k) { if(test(i, k)) { updateIfGreater(result, dp[i - k] + 1); } } for (int k = c[i - 1]; k <= d[i - 1]; ++k) { if (test(i, k) && dp[i - k] + 1 == result) { dp2[i] += dp2[i - k]; } } dp[i] = result; } return dp[n]; } int main() { scanf("%d", &n); for(int i = 0; i < n; ++i) { scanf("%d", &c[i]); scanf("%d", &d[i]); } f(n); int maxDiv = dp[n]; int numOfDiv = dp2[n]; /* for(int i =0; i <= n; ++i) { fprintf(stderr, "%d %d\n", dp[i], dp2[i]); } */ if (maxDiv > 0) { printf("%d %d\n", maxDiv, numOfDiv); } else { printf("NIE\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 | #include <cstdio> static int c[1000000] = {}; static int d[1000000] = {}; static int dp[1000001] = {}; static int dp2[1000001] = {}; static int n; const int MODULUS = 1000000007; inline void updateIfGreater(int &a, int b) { if (a < b) { a = b; } } bool test(int i, int k) { int start = i - k; if (start < 0) { return false; } for (int j = start; j < i; ++j) { if (!(c[j] <= k && k <= d[j])) { return false; } } return true; } int f(int n) { dp[0] = 0; dp2[0] = 1; for (int i = 1; i <= n; ++i) { int result = 0; for (int k = c[i - 1]; k <= d[i - 1]; ++k) { if(test(i, k)) { updateIfGreater(result, dp[i - k] + 1); } } for (int k = c[i - 1]; k <= d[i - 1]; ++k) { if (test(i, k) && dp[i - k] + 1 == result) { dp2[i] += dp2[i - k]; } } dp[i] = result; } return dp[n]; } int main() { scanf("%d", &n); for(int i = 0; i < n; ++i) { scanf("%d", &c[i]); scanf("%d", &d[i]); } f(n); int maxDiv = dp[n]; int numOfDiv = dp2[n]; /* for(int i =0; i <= n; ++i) { fprintf(stderr, "%d %d\n", dp[i], dp2[i]); } */ if (maxDiv > 0) { printf("%d %d\n", maxDiv, numOfDiv); } else { printf("NIE\n"); } } |