From a394fb6b2e382ded84d23d97afab7be987097b18 Mon Sep 17 00:00:00 2001 From: KiritoTRw <3021577574@qq.com> Date: Wed, 27 Apr 2016 20:36:00 +0800 Subject: [PATCH] From http://blog.csdn.net/qq_33765907/article/details/51228610 --- ZOJ/3942_csdn.cpp | 121 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 ZOJ/3942_csdn.cpp diff --git a/ZOJ/3942_csdn.cpp b/ZOJ/3942_csdn.cpp new file mode 100644 index 0000000..3fce1be --- /dev/null +++ b/ZOJ/3942_csdn.cpp @@ -0,0 +1,121 @@ +#include +using namespace std; +typedef long long LL; +typedef pair PII; +typedef vector VI; +typedef vector VP; + +class Solution { + static const int MAXN = 2000 + 10; + int a[MAXN], n, m; + LL count00(LL ret = 0) { + map mp; + for (int i = 1; i < n; i += 2) { + VI pt; int sum = 0; + for (int j = i; j + 1 < n; ++j) { + pt.push_back(a[j]); + if (j & 1) { + if (a[i - 1] + sum + a[j + 1] >= m) { + mp[pt].push_back(PII(a[i - 1], a[j + 1])); + } + } else sum += a[j]; + if (sum >= m) break; + } + } + for (auto &e: mp) { + VP &y = e.second, x; + int sum = m; + for (size_t i = 1; i < e.first.size(); i += 2) sum -= e.first[i]; + for (auto &v: y) { + int L = v.first, R = v.second; + v.first = max(1, sum - R); + v.second = min(L, sum - 1); + if (v.first <= v.second) x.push_back(v); + } + if (x.empty()) continue; + sort(x.begin(), x.end()); + int l = x[0].first, r = x[0].second; + for (size_t i = 0; i < x.size(); ++i) { + if (x[i].first <= r) r = max(r, x[i].second); + else { + ret += r - l + 1; + l = x[i].first; + r = x[i].second; + } + } + ret += r - l + 1; + } + return ret; + } + LL count10(LL ret = 0) { + map mp; + for (int i = 1; i < n; i += 2) { + VI pt; int sum = 0; + for (int j = i + 1; j < n; ++j) { + pt.push_back(a[j]); + if (~j & 1) { + sum += a[j]; + if (sum >= m) { + sum -= a[j]; + pt.pop_back(); + pt.push_back(m - sum); + mp[pt] = max(mp[pt], a[i]); + break; + } + } + } + } + for (auto &e: mp) ret += e.second; + return ret; + } + LL count11(LL ret = 0) { + map mp; + for (int i = 2; i < n; i += 2) { + VI pt; int sum = 0; + for (int j = i; j + 1 < n; ++j) { + pt.push_back(a[j]); + if (~j & 1) { + sum += a[j]; + if (sum == m) mp[pt].push_back(PII(a[i - 1], a[j + 1])); + else if (sum > m) break; + } + } + } + for (auto &e: mp) { + VP &y = e.second; + sort(y.begin(), y.end()); + for (int i = y.size() - 2; i >= 0; --i) { + y[i].second = max(y[i].second, y[i + 1].second); + } + for (size_t i = 0; i < y.size(); ++i) { + int extra = i ? y[i].first - y[i - 1].first : y[i].first; + ret += 1ll * extra * y[i].second; + } + } + return ret; + } +public: + void run() { + scanf("%d%d", &n, &m); + for (int i = 0; i < n; ++i) scanf("%d", a + i); + LL ret = count00() + count11(); + ret += count10(); + if (n % 2 == 0) a[n++] = 0; + reverse(a, a + n); + ret += count10(); + for (int i = 0; i < n; i += 2) if (a[i] >= m && m) { + ++ret; break; + } + if (m == 0) { + ret = 0; + for (int i = 1; i < n; i += 2) ret = max(ret, (LL)a[i]); + } + printf("%lld\n", ret); + } +} sol; + +int main() { + int T; scanf("%d", &T); + for (int cas = 1; cas <= T; ++cas) sol.run(); + return 0; +}