题目:

一个斜率优化+CDQ好题

BZOJ2149

分析:

先吐槽一下题意:保留房子反而要给赔偿金是什么鬼哦……

第一问是一个经典问题。直接求原序列的最长上升子序列是错误的。比如\(\{1,2,2,3\}\),选择\(\{1,2,3\}\)不改变后会发现无论如何修改都无法变成一个严格上升序列。只能选择\(\{1,2\}\),把原序列改成\(\{1,2,3,4\}\)。

考虑对于两个数\(a_i\)和\(a_j(j<i)\),\(a_i\)能接在\(a_j\)后面的充要条件是\(a_i-a_j\geq i-j\)(这样中间才能塞下\(i-j-1\)个数形成上升序列)。移项得到\(a_i-i\geq a_j-j\),所以应该把每个数减去它的编号作为权值然后求最长非降子序列。由于要求美观度为正整数,所以若\(a_i-i<0\),则\(i\)不能作为序列的开端。下面的代码展示了\(O(nlog_2n)\)求法(其中\(c[i]=a[i]-i\),\(f[i]\)表示以\(i\)结尾的最长非降子序列的长度)。

int solve()
{
int ans = 0;
memset(tmp, INF, sizeof(int[n + 1]));
for (int i = 1; i <= n; i++)
{
if (c[i] < 0)
f[i] = 0;
else
{
int pos = upper_bound(tmp + 1, tmp + ans + 1, c[i]) - tmp;
tmp[pos] = c[i];
ans = max(ans, pos);
f[i] = pos;
}
v[f[i]].push_back(i);
}
return ans;
}

然后来看第二问。设\(dp[i]\)为将前\(i\)个数变成单调上升序列的最小总花费。则\(dp[i]\)可以由\(dp[j]\)转移而来的必要条件是\(i>j\),\(a[i]-i>a[j]-j\)且\(f[i]=f[j]+1\)(若\(f[i]>f[j]+1\),则不满足“保留最多的旧房子”;若\(f[i]<f[j]+1\),说明你\(f[i]\)算错了)。

转移时,最优解显然是把\(a[k](j<k<i)\)变成一个以\(a[j]+1\)为首项,公差为\(1\)的等差数列。由于\(a[i]-i>a[j]-j\),所以改完以后一定有\(a[i-1]<a[i]\)

\[dp[i]=min\{dp[j]+\frac{[a[j]+1+a[j]+(i-j-1)]\times(i-j-1)}{2}+a[i]+b[i]\}
\]

整理一下,得到:

\[dp[i]=min\{dp[j]+a[j]\times(i-j-1)+\frac{i(i-1)}{2}+\frac{j(j+1)}{2}+-ij+a[i]+b[i]\}
\]

可以根据\(f[i]\)分层,一起处理所有\(f[j]=k-1\)的\(j\)对\(f[i]=k\)的\(i\)的贡献。下面考虑每一层的情况。

未完待续……

代码:

方便起见,在序列首加一个\(0\)(\(a[0]=f[0]=0\))。这样可以保证改造后美观度为正(因为\(f[i]=1\)的\(dp[i]\)必然从\(dp[0]\)转移而来);在序列尾加一个无穷大作为\(a[n+1]\),\(dp[n+1]-a[n+1]\)即为答案。

#include <cstdio>
#include <algorithm>
#include <cctype>
#include <cstring>
#include <vector>
using namespace std; namespace zyt
{
template<typename T>
inline void read(T &x)
{
char c;
bool f = false;
x = 0;
do
c = getchar();
while (c != '-' && !isdigit(c));
if (c == '-')
f = true, c = getchar();
do
x = x * 10 + c - '0', c = getchar();
while (isdigit(c));
if (f)
x = -x;
}
template<typename T>
inline void write(T x)
{
static char buf[20];
char *pos = buf;
if (x < 0)
putchar('-'), x = -x;
do
*pos++ = x % 10 + '0';
while (x /= 10);
while (pos > buf)
putchar(*--pos);
}
typedef long long ll;
typedef long double ld;
const int N = 1e5 + 10, INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
int n, a[N], b[N], c[N], f[N], tmp[N];
ll dp[N];
vector<int> v[N];
int solve()
{
int ans = 0;
memset(tmp, INF, sizeof(int[n + 1]));
for (int i = 1; i <= n; i++)
{
if (c[i] < 0)
f[i] = 0;
else
{
int pos = upper_bound(tmp + 1, tmp + ans + 1, c[i]) - tmp;
tmp[pos] = c[i];
ans = max(ans, pos);
f[i] = pos;
}
v[f[i]].push_back(i);
}
v[0].push_back(0);
return ans;
}
inline ll x(const int i)
{
return i - a[i];
}
inline ll y(const int i)
{
return dp[i] - (ll)(i + 1) * a[i] + (ll)i * (i + 1) / 2;
}
inline ld ratio(const int i, const int j)
{
if (x(i) == x(j))
return y(i) < y(j) ? -LINF : LINF;
else
return (ld)(y(i) - y(j)) / (x(i) - x(j));
}
struct node
{
int pos;
bool type;
bool operator < (const node &b) const
{
return pos < b.pos;
}
}arr[N];
const int CHANGE = 0, QUERY = 1;
void CDQ(const int l, const int r)
{
if (l == r)
return;
int mid = (l + r) >> 1, i = l, j = mid + 1, k = l;
static node tmp[N];
static int st[N];
CDQ(l, mid), CDQ(mid + 1, r);
int top = 0;
while (i <= mid && j <= r)
{
if (x(arr[i].pos) >= x(arr[j].pos))
{
if (arr[i].type == CHANGE)
{
while (top > 1 && ratio(st[top - 2], st[top - 1]) < ratio(st[top - 1], arr[i].pos))
--top;
st[top++] = arr[i].pos;
}
tmp[k++] = arr[i++];
}
else
{
if (arr[j].type == QUERY && top)
{
int l = 0, r = top - 2, ans = top - 1;
while (l <= r)
{
int mid = (l + r) >> 1;
if (ratio(st[mid], st[mid + 1]) < arr[j].pos)
r = mid - 1, ans = mid;
else
l = mid + 1;
}
dp[arr[j].pos] = min(dp[arr[j].pos],
dp[st[ans]] +
(ll)((a[st[ans]] << 1) + arr[j].pos - st[ans]) * (arr[j].pos - st[ans] - 1) / 2
+ a[arr[j].pos] + b[arr[j].pos]);
}
tmp[k++] = arr[j++];
}
}
while (i <= mid)
tmp[k++] = arr[i++];
while (j <= r)
{
if (arr[j].type == QUERY && top)
{
int l = 0, r = top - 2, ans = top - 1;
while (l <= r)
{
int mid = (l + r) >> 1;
if (ratio(st[mid], st[mid + 1]) < arr[j].pos)
r = mid - 1, ans = mid;
else
l = mid + 1;
}
dp[arr[j].pos] = min(dp[arr[j].pos],
dp[st[ans]] +
(ll)((a[st[ans]] << 1) + arr[j].pos - st[ans]) * (arr[j].pos - st[ans] - 1) / 2
+ a[arr[j].pos] + b[arr[j].pos]);
}
tmp[k++] = arr[j++];
}
memcpy(arr + l, tmp + l, sizeof(node[r - l + 1]));
}
int work()
{
read(n);
for (int i = 1; i <= n; i++)
read(a[i]), c[i] = a[i] - i;
for (int i = 1; i <= n; i++)
read(b[i]);
a[++n] = INF;
c[n] = INF;
int ans = solve();
write(ans - 1), putchar(' ');
memset(dp, INF, sizeof(ll[n + 1]));
dp[0] = 0;
for (int i = 1; i <= ans; i++)
{
int cnt = 0;
for (int j = 0; j < v[i - 1].size(); j++)
if (dp[v[i - 1][j]] < LINF)
arr[++cnt] = (node){v[i - 1][j], CHANGE};
for (int j = 0; j < v[i].size(); j++)
arr[++cnt] = (node){v[i][j], QUERY};
sort(arr + 1, arr + cnt + 1);
CDQ(1, cnt);
}
write(dp[n] - a[n] - b[n]);
return 0;
}
}
int main()
{
return zyt::work();
}

最新文章

  1. python爬虫实战(一)——实时获取代理ip
  2. MMDrawerController在表视图和导航栏中的使用
  3. step by step install Caffe
  4. POI文件导出至EXCEL,并弹出下载框
  5. hdu 1575 矩阵快速幂模板
  6. Android笔记:java 中的枚举
  7. Python3基础 大于一个数的同时小于一个数
  8. iOS - 文件操作(File Operating)
  9. android 大小写转换
  10. Jquery 实现动态加入table tr 和删除tr 以及checkbox的全选 和 获取加入TR删除TR后的数据
  11. PAT (Advanced Level) 1012. The Best Rank (25)
  12. Akka(7): FSM:通过状态变化来转换运算行为
  13. spring-security doc logout
  14. 【BZOJ 1002】: [FJOI2007]轮状病毒
  15. 【Teradata】块压缩(ferret工具)
  16. source 和 .
  17. zookeeper入门之Curator的使用之几种监听器的使用
  18. echarts的学习
  19. ABAP 自定义排序的思想(不用系统标准的SORT语句)
  20. POSTMAN 数据关联

热门文章

  1. 斯特林公式 hdu1018
  2. textarea 高度调整
  3. 判断项目中是否有slf4j的实现类
  4. 最长上升子序列的回溯 ZOJ 2432
  5. [luoguP2760] 科技庄园(背包DP)
  6. [luoguP2875] [USACO07FEB]牛的词汇The Cow Lexicon(DP)
  7. HDU - 3407 - String-Matching Automata
  8. 【BZOJ2434】阿狸的打字机(fail树,DFS序)
  9. 学一学书里的django是怎么写views.py的
  10. 最小生成树 I - Agri-Net