费用流

并没有想出来构图方法 我们设立源汇,其实我们关心的是相邻两个值的差值,如果差值小于0说明需要长高,那么向汇点连边差值,说明需要修改,如果差大于零,那么由源点连边差值,说明可以提供修改空间,再由源点向1和n+1连边inf,因为这两个点是可以无限修改的。然后1-2-3-n+1连双向边,费用为1,容量inf,表明修改差值。正向流是提高后面的值,反向流是降低前面的值。而且得加堆优化迪杰斯特拉,否则跑不过去了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, int> PII;
const int N = ;
const ll inf = ;
struct edge {
int nxt, to;
ll f, c;
} e[N * ];
int inq[N], a[N], b[N], head[N], pree[N], pprev[N];
ll dis[N];
priority_queue<PII, vector<PII>, greater<PII> > q;
int n, source, sink, cnt = ;
inline void link(int u, int v, ll f, ll c)
{
e[++cnt].nxt = head[u];
head[u] = cnt;
e[cnt].to = v;
e[cnt].f = f;
e[cnt].c = c;
}
inline void insert(int u, int v, ll f, ll c)
{
link(u, v, f, c);
link(v, u, , -c);
}
inline bool spfa()
{
int l = , r = ;
for(int i = source; i <= sink; ++i)
dis[i] = inf;
dis[source] = ;
q.push(make_pair(, source));
while(!q.empty())
{
PII x = q.top();
q.pop();
int u = x.second;
if(dis[u] != x.first) continue;
for(int i = head[u]; i; i = e[i].nxt) if(e[i].f && dis[e[i].to] > dis[u] + e[i].c)
{
pree[e[i].to] = i;
pprev[e[i].to] = u;
dis[e[i].to] = dis[u] + e[i].c;
q.push(make_pair(dis[e[i].to], e[i].to));
}
}
return dis[sink] != inf;
}
inline ll getflow()
{
int now = sink;
ll delta = inf;
while(now != source)
{
delta = min(delta, e[pree[now]].f);
now = pprev[now];
}
now = sink;
while(now != source)
{
e[pree[now]].f -= delta;
e[pree[now] ^ ].f += delta;
now = pprev[now];
}
return delta * dis[sink];
}
inline ll mcmf()
{
ll ret = ;
while(spfa()) ret += getflow();
return ret;
}
int main()
{
scanf("%d", &n);
source = ;
sink = n + ;
for(int i = ; i <= n; ++i)
scanf("%d", &a[i]);
for(int i = ; i <= n; ++i)
{
b[i] = a[i] - a[i - ];
if(b[i] > ) insert(i, sink, b[i] - , );
else insert(source, i, -b[i] + , );
}
for(int i = ; i <= n; ++i)
{
insert(i, i + , inf, );
insert(i + , i, inf, );
}
insert(, sink, inf, );
insert(n + , sink, inf, );
printf("%lld\n", mcmf());
return ;
}

713C的更多相关文章

  1. codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)

    题目链接: C. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 ...

  2. Codeforces 713C Sonya and Problem Wihtout a Legend

    题意:给一个序列,可以进行若干次操作,每次操作选择一个数让它+1或-1,问最少要几次操作使得序列变为严格单调递增序列. 题解:首先考虑非严格递增序列,则每个数字最终变成的数字一定是该数组中的某个数.那 ...

  3. Codeforces 713C Sonya and Problem Wihtout a Legend(单调DP)

    [题目链接] http://codeforces.com/problemset/problem/713/C [题目大意] 给出一个数列,请你经过调整使得其成为严格单调递增的数列,调整就是给某些位置加上 ...

  4. Codeforces 713C Sonya and Problem Wihtout a Legend DP

    C. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...

  5. codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)(将一个数组变成严格单增数组的最少步骤)

    E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...

  6. Codeforces 713C Sonya and Problem Wihtout a Legend(DP)

    题目链接   Sonya and Problem Wihtout a Legend 题意  给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$. 求把原序列变成 ...

  7. [20180918]文件格式与sql_id.txt

    [20180918]文件格式与sql_id.txt --//记录测试中遇到的一个问题.这是我在探究SQL*Net more data from client遇到的问题.--//就是实际oracle会把 ...

  8. Week One

    2018.11.21: 1.[BZOJ 4868][SHOI 2017] 从后往前枚举最后位置即可,如果$A<B$,用尽可能多的$A$替换$B$操作 Tip:很大的$C$可能爆$longlong ...

  9. codeforces的dp专题

    1.(467C)http://codeforces.com/problemset/problem/467/C 题意:有一个长为n的序列,选取k个长度为m的子序列(子序列中不能有位置重复),求所取的k个 ...

随机推荐

  1. RHEL7配置中文输入法-智能拼音

    RHEL7配置中文输入法-智能拼音 RHEL7.x(CentOS7.x)系统相对之前的6.x系统变化较大,虽然安装时选择了中文环境,但是进入系统后,在控制台及编辑器中仍无法切换输入法进行中文输入. 原 ...

  2. 扩增子图表解读4曼哈顿图:差异分类级别Taxonomy

    曼哈顿图 Manhattan Plot 曼哈顿图本质上是一个散点图,用于显示大量非零大范围波动数值,最早应用于全基因组关联分析(GWAS)研究展示高度相关位点.它得名源于样式与曼哈顿天际线相似(如下图 ...

  3. Cesium学习笔记(四)Camera ----http://blog.csdn.net/hobhunter/article/details/74909641

    Cesium 相机控制场景中的视野.操作相机的方法有很多,如旋转,缩放,平移和飞到目的地.Cesium具有默认的鼠标和触摸事件处理程序与相机进行交互,还有一个API以编程方式操纵相机. 我们可以使用该 ...

  4. vue中fetch请求

    1. 请求方式:get 请求参数:menuName 返回的结果:data created(){ this._initPageData() }, methods:{ _initPageData(){ f ...

  5. JS对象中,在原型链上找到属性后 最终将值拷贝给原对象 而不是引用

    遇到一个面试题 要求写一个函数A,每次进行new操作时候能输出2,3,4,5... new A() // 输出2 new A() // 输出3 new A() // 输出4 function A() ...

  6. sqlalchemy子查询

    使用subquery() 要使用c来定位上一个子句的属性 s1 = session.query(m.a,m.b).filter().subquery() s2 = session.query(s1.c ...

  7. Vmware在NAT模式下网络配置详解

    Vmware在NAT模式下网络配置详解 Linux中的网络配置对于接触Linux不久的小白菜来说,还是小有难度的,可能是不熟悉这种与windows系列迥然不同的命令行操作,也可能是由于对Linux的结 ...

  8. 清北学堂模拟赛d3t3 c

    分析:一开始拿到这道题真的是无从下手,暴力都很难打出来.但是基本的方向还是要有的,题目问的是方案数,dp不行就考虑数学方法.接下来比较难想.其实对于每一行或者每一列,我们任意打乱顺序其实对答案是没有影 ...

  9. [转]SQLSERVER一些公用DLL的作用解释

    转自:Leo_wlCnBlogs SQLSERVER一些公用DLL的作用解释 如果你的SQLSERVER安装在C盘的话,下面的路径就是相应SQLSERVER版本的公用DLL的存放路径 SQL2005 ...

  10. Clojure:解决korma中mysql utf8的问题

    当使用korma内置的mysql方法时,无法添加utf-8的支持.解决的方法就是重写mysql方法,代码如下: (defn mysql "改编自korma,添加了utf-8的支持" ...