From d9f30124ccfd7f618cfba9410496bf85129e8681 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Tue, 13 Sep 2016 14:17:41 +0800 Subject: [PATCH] =?UTF-8?q?Create=20=E9=94=AF=E6=9C=A8=E5=8E=82=E9=80=89?= =?UTF-8?q?=E5=9D=80=5Fkedebug.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From http://www.cnblogs.com/kedebug/archive/2013/03/02/2940423.html --- CEOI/2004/锯木厂选址_kedebug.cpp | 54 ++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 CEOI/2004/锯木厂选址_kedebug.cpp diff --git a/CEOI/2004/锯木厂选址_kedebug.cpp b/CEOI/2004/锯木厂选址_kedebug.cpp new file mode 100644 index 0000000..cf72450 --- /dev/null +++ b/CEOI/2004/锯木厂选址_kedebug.cpp @@ -0,0 +1,54 @@ +/// Day2 : Two sawmills +#include +#include +#include +using namespace std; + +const int MAXN = 20010; +const int INFS = 0x7fffffff; + +int w[MAXN], d[MAXN], x[MAXN], deq[MAXN]; + +inline double slope(int i, int j) +{ + return 1.0 * (w[i] * x[i] - w[j] * x[j]) / (w[i] - w[j]); +} + +int main() +{ + freopen("two.in","r",stdin); + freopen("two.out","w",stdout); + + int n; + scanf("%d\n", &n); + + for (int i = 1; i <= n; ++i) + scanf("%d %d", &w[i], &d[i]); + + w[0] = d[0] = x[1] = 0; + int sum = 0; + for (int i = 1; i <= n; ++i) + { + x[i+1] = x[i] + d[i]; + sum += x[i] * w[i], w[i] += w[i-1]; + } + + int ans = INFS; + int s = 0, e = -1; + for (int i = 1; i <= n; ++i) + { + while (s < e && slope(deq[e], deq[e-1]) >= slope(i, deq[e])) + --e; + deq[++e] = i; + while (s < e && slope(deq[s], deq[s+1]) <= x[i]) + ++s; + + int delta = -sum + w[i] * x[i] + (w[n] - w[i]) * x[n+1]; + ans = min(ans, delta + w[deq[s]] * x[deq[s]] - x[i] * w[deq[s]]); + } + printf("%d\n", ans); + + fclose(stdin); + fclose(stdout); + return 0; +}