题解

斜率优化裸题。

有个很玄学的事情,就是我用\(f[i]=min\{f[j-1]+p[j].y*p[i].x\}\) 会很奇怪的Wa 。 明明和\(f[i]=min\{f[j]+p[j+1].y*p[i].x\}\)一模一样的呀!

如果有dalao愿意帮忙看一下就感激不尽了。

附上正确代码和错误代码

正确代码:

  1. #include<bits/stdc++.h>
  2. typedef long long ll;
  3. using namespace std;
  4. const int maxn = 5e4+10;
  5. const ll inf = 10000000000000LL;
  6. struct qwq{
  7. ll x,y;
  8. const bool operator < (const qwq rhs) const {
  9. if(this->x==rhs.x)
  10. return this->y<rhs.y;
  11. return this->x<rhs.x;
  12. }
  13. } tmp[maxn],p[maxn];
  14. ll f[maxn];
  15. int tot=0,n,q[maxn],l,r;
  16. long double slope(int a,int b) {
  17. long double tmp1 = f[b]-f[a],tmp2 = -p[b+1].y+p[a+1].y;
  18. return tmp1/tmp2;
  19. }
  20. int main() {
  21. ios::sync_with_stdio(false);
  22. cin.tie(0);cout.tie(0);
  23. cin>>n;
  24. for(int i = 1;i<=n;++i)
  25. cin>>tmp[i].x>>tmp[i].y;
  26. sort(tmp+1,tmp+1+n);
  27. ll mxy=0;
  28. for(int i = n;i;--i) {
  29. if(tmp[i].y>mxy) {
  30. mxy=tmp[i].y;
  31. p[++tot]=tmp[i];
  32. }
  33. }
  34. reverse(p+1,p+1+tot);
  35. #ifdef force
  36. for(int i = 1;i<=tot;++i) {
  37. f[i]=inf;
  38. for(int j = 0;j<i;++j) {
  39. f[i]=min(f[i],f[j]+p[j+1].y*p[i].x);
  40. }
  41. }
  42. #endif
  43. #ifndef force
  44. for(int i = 1;i<=tot;++i) {
  45. while(l<r&&slope(q[l],q[l+1])<=p[i].x) ++l;
  46. int j = q[l];
  47. f[i]=f[j]+p[j+1].y*p[i].x;
  48. while(l<r&&slope(q[r-1],q[r])>=slope(q[r],i)) --r;
  49. q[++r]=i;
  50. }
  51. #endif
  52. cout<<f[tot]<<endl;
  53. return 0;
  54. }

错误代码

  1. #include<bits/stdc++.h>
  2. typedef long long ll;
  3. using namespace std;
  4. const int maxn = 5e4+10;
  5. const ll inf = 10000000000000LL;
  6. double esp = 1e-6;
  7. struct qwq{
  8. ll x,y;
  9. const bool operator < (const qwq rhs) const {
  10. if(this->x==rhs.x)
  11. return this->y<rhs.y;
  12. return this->x<rhs.x;
  13. }
  14. } tmp[maxn],p[maxn];
  15. ll f[maxn];
  16. int tot=0,n,q[maxn],l,r;
  17. double slope(int a,int b) {
  18. double tmp1 = f[b-1]-f[a-1],tmp2 = -p[b].y+p[a].y;
  19. return tmp1/tmp2;
  20. }
  21. int main() {
  22. ios::sync_with_stdio(false);
  23. cin.tie(0);cout.tie(0);
  24. cin>>n;
  25. for(int i = 1;i<=n;++i)
  26. cin>>tmp[i].x>>tmp[i].y;
  27. sort(tmp+1,tmp+1+n);
  28. ll mxy=0;
  29. for(int i = n;i;--i) {
  30. if(tmp[i].y>mxy) {
  31. mxy=tmp[i].y;
  32. p[++tot]=tmp[i];
  33. }
  34. }
  35. reverse(p+1,p+1+tot);
  36. #ifdef force
  37. for(int i = 1;i<=tot;++i) {
  38. f[i]=inf;
  39. for(int j = 1;j<=i;++j) {
  40. f[i]=min(f[i],f[j-1]+p[j].y*p[i].x);
  41. }
  42. }
  43. #endif
  44. #ifndef force
  45. l=r=1;
  46. q[1]=1;
  47. for(int i = 1;i<=tot;++i) {
  48. while(l<r&&slope(q[l],q[l+1])<=p[i].x) ++l;
  49. int j = q[l];
  50. f[i]=f[j-1]+p[j].y*p[i].x;
  51. while(l<r&&slope(q[r-1],q[r])>=slope(q[r],i)) --r;
  52. q[++r]=i+1;
  53. }
  54. #endif
  55. cout<<f[tot]<<endl;
  56. return 0;
  57. }

【文文殿下】 [USACO08MAR]土地征用 题解的更多相关文章

  1. 洛谷 P2900 [USACO08MAR]土地征用Land Acquisition 解题报告

    P2900 [USACO08MAR]土地征用Land Acquisition 题目描述 约翰准备扩大他的农场,眼前他正在考虑购买N块长方形的土地.如果约翰单买一块土 地,价格就是土地的面积.但他可以选 ...

  2. 【文文殿下】 [SDOI2013]保护出题人 题解

    题解 我们把伤害-时间图像画出来.然后维护一下僵尸血量的前缀和.最好情况肯定是有一个僵尸恰好死在戴夫家门口.我们把原点到其他n个点的斜率最大的一个累积到答案. 发现每添加一个点,其他所有点的坐标都变了 ...

  3. 【文文殿下】[CEOI2004]锯木厂选址 题解

    题解 我们枚举建厂的位置,发现有个\(n^2\)的DP.随手搞个斜率优化到\(O(n)\). #include<bits/stdc++.h> using namespace std; ty ...

  4. 【文文殿下】CF1098C Construct a tree 题解

    题解 挺水的一道题. Rating $ \color{orange} {2300}$ 以下送命题. 首先我们知道,所有子树大小之和就是节点个数加上从根到所有节点的路径长度之和. 他要求度数尽可能小,所 ...

  5. 【文文殿下】[APIO2010]特别行动队 题解

    基本上是一个斜率优化裸题了 #include<bits/stdc++.h> using namespace std; typedef long long ll; const int max ...

  6. 【文文殿下】【CF724C】Ray Tracing (中国剩余定理)

    题解 我们考虑将棋盘扩大一倍,这样相当于取膜.然后,我们只要对x,y,的位置分类讨论,做四次crt就行.具体细节看文文代码. #include<cstdio> #include<al ...

  7. 【文文殿下】CF1029F Multicolored Markers

    这道题考场上卡了文文相当长的时间,所以写个题解泄泄愤QAQ 题意:给你$a$块红瓷砖,$b$块白瓷砖,在一个无限大的地上拼装,要求整体是一个矩形,并且至少有一种颜色是一个矩形,求最小周长. 题解: 首 ...

  8. 洛谷P2900 [USACO08MAR]土地征用Land Acquisition(动态规划,斜率优化,决策单调性,线性规划,单调队列)

    洛谷题目传送门 用两种不一样的思路立体地理解斜率优化,你值得拥有. 题意分析 既然所有的土地都要买,那么我们可以考虑到,如果一块土地的宽和高(其实是蒟蒻把长方形立在了平面上)都比另一块要小,那么肯定是 ...

  9. 【文文殿下】WC2019游记

    Day0 今天早上三点半才睡着,五点起床,前往省城郑州.与省实验常老师汇合,坐上高铁,下午三点半多才到广州二中. 下午随便找了一个教室进去敲一敲代码,发现自己越来越菜了. 和一大堆网上的dalao面基 ...

随机推荐

  1. CXF wsdl2java (转载)

    2011-03-28 14:27 9735人阅读 评论(2) 收藏 举报 servicewebserviceinterfacejavastringserver CXF wsdl2Java 一.  简介 ...

  2. Java环境编写

    首先安装jdk,本系统中jdk安装在D:\jdk:jre安装在D:\Jre: 然后开始配置环境变量: JAVA_HOME:D:\jdk; JRE_HOME:D:\jre; CLASSPATH:.;%J ...

  3. MySQL基准测试--innodb_buffer_pool_instances

    http://blog.chinaunix.net/uid-26896862-id-3345441.html 目的 根据现有硬件环境下,测试MySQL单实例下,在数据量小于innodb_buffer_ ...

  4. TensorFlow实现的激活函数可视化

    书上的代码: # coding: utf-8 # In[1]: import matplotlib.pyplot as plt import numpy as np import tensorflow ...

  5. MyBatis中log4j 和 参数 和 分页和别名 功能

    1.配置全局文件,注意各个配置标签的顺序 properties?, settings?, typeAliases?, typeHandlers?, objectFactory?,   objectWr ...

  6. idea如何将项目以eclipse保存

    会生成 提交到svn     eclipse 导入 首先使用TortoiseSVN下载要导入的项目 导入 已经存在的maven 项目       clean install -DskipTests t ...

  7. 2018.12.30 洛谷P4238 【模板】多项式求逆

    传送门 多项式求逆模板题. 简单讲讲? 多项式求逆 定义: 对于一个多项式A(x)A(x)A(x),如果存在一个多项式B(x)B(x)B(x),满足B(x)B(x)B(x)的次数小于等于A(x)A(x ...

  8. 2018.11.28 poj3294 Life Forms(后缀数组+双指针)

    传送门 后缀数组经典题目. 我们先把所有的字符串都接在一起. 然后求出hththt数组和sasasa数组. 然后对于sasasa数组跑双指针统计答案. 如果双指针包括进去的属于不同字符串的数量达到了题 ...

  9. java学习3创建学生属性:学号、姓名、电话 完全实现对象集合的增删改查。(控制台来做)

    首先创建一个student类其中包括get,set与构造函数 /** * */package work2; /** * @author Administrator * */public final c ...

  10. LoadIcon

    1.LoadIcon(HINSTANCE hInstance,LPCSTR lpIconName);该函数从与 hInstance 模块相关联的可执行文件中装入lpIconName指定的图标资源,仅当 ...