费用流

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

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef pair<ll, int> PII;
  5. const int N = ;
  6. const ll inf = ;
  7. struct edge {
  8. int nxt, to;
  9. ll f, c;
  10. } e[N * ];
  11. int inq[N], a[N], b[N], head[N], pree[N], pprev[N];
  12. ll dis[N];
  13. priority_queue<PII, vector<PII>, greater<PII> > q;
  14. int n, source, sink, cnt = ;
  15. inline void link(int u, int v, ll f, ll c)
  16. {
  17. e[++cnt].nxt = head[u];
  18. head[u] = cnt;
  19. e[cnt].to = v;
  20. e[cnt].f = f;
  21. e[cnt].c = c;
  22. }
  23. inline void insert(int u, int v, ll f, ll c)
  24. {
  25. link(u, v, f, c);
  26. link(v, u, , -c);
  27. }
  28. inline bool spfa()
  29. {
  30. int l = , r = ;
  31. for(int i = source; i <= sink; ++i)
  32. dis[i] = inf;
  33. dis[source] = ;
  34. q.push(make_pair(, source));
  35. while(!q.empty())
  36. {
  37. PII x = q.top();
  38. q.pop();
  39. int u = x.second;
  40. if(dis[u] != x.first) continue;
  41. for(int i = head[u]; i; i = e[i].nxt) if(e[i].f && dis[e[i].to] > dis[u] + e[i].c)
  42. {
  43. pree[e[i].to] = i;
  44. pprev[e[i].to] = u;
  45. dis[e[i].to] = dis[u] + e[i].c;
  46. q.push(make_pair(dis[e[i].to], e[i].to));
  47. }
  48. }
  49. return dis[sink] != inf;
  50. }
  51. inline ll getflow()
  52. {
  53. int now = sink;
  54. ll delta = inf;
  55. while(now != source)
  56. {
  57. delta = min(delta, e[pree[now]].f);
  58. now = pprev[now];
  59. }
  60. now = sink;
  61. while(now != source)
  62. {
  63. e[pree[now]].f -= delta;
  64. e[pree[now] ^ ].f += delta;
  65. now = pprev[now];
  66. }
  67. return delta * dis[sink];
  68. }
  69. inline ll mcmf()
  70. {
  71. ll ret = ;
  72. while(spfa()) ret += getflow();
  73. return ret;
  74. }
  75. int main()
  76. {
  77. scanf("%d", &n);
  78. source = ;
  79. sink = n + ;
  80. for(int i = ; i <= n; ++i)
  81. scanf("%d", &a[i]);
  82. for(int i = ; i <= n; ++i)
  83. {
  84. b[i] = a[i] - a[i - ];
  85. if(b[i] > ) insert(i, sink, b[i] - , );
  86. else insert(source, i, -b[i] + , );
  87. }
  88. for(int i = ; i <= n; ++i)
  89. {
  90. insert(i, i + , inf, );
  91. insert(i + , i, inf, );
  92. }
  93. insert(, sink, inf, );
  94. insert(n + , sink, inf, );
  95. printf("%lld\n", mcmf());
  96. return ;
  97. }

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. C#——简单工厂

    简单工厂的方法实现过程核心就是之前介绍的接口应用.所以直接上代码: public interface IPerson { void Say(); } public class Student : IP ...

  2. HTML地理位置定位

    最近公司项目需要做一个类似微信朋友圈的互动交友功能,需要显示用户位置信息,因此在网上查了部分资料,记下demo供以后查看学习:(用到了百度api来实现定位功能) <!DOCTYPE html&g ...

  3. NetBeans将java项目编译成jar包

    1.找到file选项下的build.xml.

  4. 删除ListView item数据 页面不刷新

    最近碰到一个匪夷所思的事情.就是我删除listView中一条item数据  网络请求成功了 但是页面不成功,一番折腾 ,找到问题 ,原来我给item 添加了北京点击事假,又给listView 被禁设置 ...

  5. tac

    功能说明:反向显示文件内容. 参数选项: -b 在行前而非行尾加分隔标志. -r 将分隔标志视作正则表达式来解析. -s 使用指定字符串代替换行作为分隔标志.   cat命令与tac命令的对比:

  6. mvc 类中对应数据库属性

    [StringLength()] //可空 对应数据库可空 [DefaultValue("")] [DisplayName("添加人用户名")] public ...

  7. map集合遍历,放入id

    背景,需要从电脑导入excel表格到网页上然后表格中公司需要对应数据库的id 通过key-value方法来对应id Office office = new Office();office.setG00 ...

  8. 1 Web 知识基础

    一.什么是跨域访问举个栗子:在A网站中,我们希望使用Ajax来获得B网站中的特定内容.如果A网站与B网站不在同一个域中,那么就出现了跨域访问问题.你可以理解为两个域名之间不能跨过域名来发送请求或者请求 ...

  9. Mac 执行 gulp 报错 -bash: gulp: command not found

    在mac系统下安装gulp,之后执行gulp 报如下错误: -bash: gulp: command not found 回溯安装过程发现问题如下 1.执行 npm root: Application ...

  10. python利用7z批量解压rar

    一开始我使用了rarfile这个库,奈何对于含有密码的压缩包支持不好,在linux上不抛出异常:之后有又尝试了unrar..比rarfile还费劲.. 所以用了调用系统命令的方法,用7z来解压 通过a ...