标签:git amp double dig tchar def style main lan
[题目链接]
http://poj.org/problem?id=1180
[算法]
首先 , 用fi表示前i个任务花费的最小代价
有状态转移方程 : fi = min{ fj + sumTi(sumCi - sumCj) + S(sumCn - sunCj)}
直接进行转移的时间复杂度为O(N ^ 2)
对该式进行展开后不难看出 , 由于式子中有一些i和j的乘积项 , 所以可以进行斜率优化
推导过程略
时间复杂度 : O(N)
[代码]
#include<iostream> #include<stdio.h> #include<algorithm> #include<string.h> #include<queue> using namespace std; #define MAXN 100010 typedef long long ll; typedef long double ld; typedef unsigned long long ull; int n , S , l , r; int t[MAXN] , c[MAXN] , sumt[MAXN] , sumc[MAXN] , f[MAXN] , q[MAXN]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); } template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); } template <typename T> inline void read(T &x) { T f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == ‘-‘) f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - ‘0‘; x *= f; } int main() { read(n); read(S); for (int i = 1; i <= n; i++) { read(t[i]); read(c[i]); sumt[i] = sumt[i - 1] + t[i]; sumc[i] = sumc[i - 1] + c[i]; } f[q[l = r = 1]] = 0; for (int i = 1; i <= n; i++) { while (l < r && f[q[l + 1]] - f[q[l]] <= (S + sumt[i]) * (sumc[q[l + 1]] - sumc[q[l]])) ++l; f[i] = f[q[l]] - (S + sumt[i]) * sumc[q[l]] + sumt[i] * sumc[i] + S * sumc[n]; while (l < r && (f[i] - f[q[r]]) * (sumc[q[r]] - sumc[q[r - 1]]) <= (f[q[r]] - f[q[r - 1]]) * (sumc[i] - sumc[q[r]])) --r; q[++r] = i; } printf("%d\n" , f[n]); return 0; }
标签:git amp double dig tchar def style main lan
原文地址:https://www.cnblogs.com/evenbao/p/10354177.html