版本比较
[P3648 [APIO2014] 序列分割]题解
正文
增加 8 行删除 8 行
62 行未改动636465666768697071727374757677784 行未改动838485868788899091929394959697989910010110210310410510610710810911011111211311411511611715 行未改动62 行未改动const int K = 205;int n, k;ll s[N];ll dp[N], g[N];int way[K][N];int q[N], hh, tt;double Y(int j) {return (double)g[j] - (double)s[j] * s[j];}double slope(int j, int k) {if (s[j] == s[k]) return -1e18;return (Y(k) - Y(j)) / (double)(s[k] - s[j]);}void slove(){4 行未改动}mem0(g);f(t, 1, k) {hh = 1; tt = 0;q[++tt] = 0;f(i, 1, n) {// 队首弹出:如果队首线段斜率 <= 当前需要的斜率 S[i]// 因为我们要最大值,且S[i]递增,上凸壳左边的斜率大,右边小// 我们要找切线斜率为S[i]的点。// 若 slope(q[hh], q[hh+1]) >= S[i],说明hh+1比hh好while(hh < tt && slope(q[hh], q[hh+1]) >= -s[i]) hh++;int best = q[hh];dp[i] = g[best] + s[best] * (s[i] - s[best]);way[t][i] = best;// 将当前点 i 加入队尾// 维护上凸壳,斜率递减。// 如果新线段 slope(tt, i) >= slope(tt-1, tt),则 tt 点凹进去了,删掉// 复制代码提交会wa的~~~// 如果 s[i] == s[q[tt]],因为 Y(i) 可能比 Y(q[tt]) 大,我们直接弹出旧的while(hh < tt && (s[i] == s[q[tt]] || slope(q[tt-1], q[tt]) <= slope(q[tt], i))) {if(s[i] == s[q[tt]]) {if(Y(i) > Y(q[tt])) tt--;else break; // 新的不如旧的,且X相同,不加了} else {tt--;}}if(s[i] != s[q[tt]]) q[++tt] = i;}memcpy(g, dp, sizeof(dp));}printf("%lld\n", dp[n]);15 行未改动62 行未改动636465666768697071727374757677784 行未改动838485868788899091929394959697989910010110210310410510610710810911011111211311411511611715 行未改动62 行未改动const int K = 205;int n, k;ll s[N];ll dp[N], g[N];int way[K][N];int q[N], hh, tt;double Y(int j) {return (double)g[j] - (double)s[j] * s[j];}double slope(int j, int k) {if (s[j] == s[k]) return -1e18;return (Y(k) - Y(j)) / (double)(s[k] - s[j]);}void slove(){4 行未改动}mem0(g);f(t, 1, k) {hh = 1; tt = 0;q[++tt] = 0;f(i, 1, n) {// 队首弹出:如果队首线段斜率 <= 当前需要的斜率 S[i]// 因为我们要最大值,且S[i]递增,上凸壳左边的斜率大,右边小// 我们要找切线斜率为S[i]的点。// 若 slope(q[hh], q[hh+1]) >= S[i],说明hh+1比hh好while(hh < tt && slope(q[hh], q[hh+1]) >= -s[i]) hh++;int best = q[hh];dp[i] = g[best] + s[best] * (s[i] - s[best]);way[t][i] = best;// 将当前点 i 加入队尾// 维护上凸壳,斜率递减。// 如果新线段 slope(tt, i) >= slope(tt-1, tt),则 tt 点凹进去了,删掉// 复制代码提交会wa的~~~// 如果 s[i] == s[q[tt]],因为 Y(i) 可能比 Y(q[tt]) 大,我们直接弹出旧的while(hh < tt && (s[i] == s[q[tt]] || slope(q[tt-1], q[tt]) <= slope(q[tt], i))) {if(s[i] == s[q[tt]]) {if(Y(i) > Y(q[tt])) tt--;else break; // 新的不如旧的,且X相同,不加了} else {tt--;}}if(s[i] != s[q[tt]]) q[++tt] = i;}memcpy(g, dp, sizeof(dp));}printf("%lld\n", dp[n]);15 行未改动