DP单调队列--斜率优化P3195
Posted --hpy-7m
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DP单调队列--斜率优化P3195相关的知识,希望对你有一定的参考价值。
题意:https://www.luogu.com.cn/problem/P3195
思路:https://www.luogu.com.cn/problemnew/solution/P3195
1 #define ios ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <iostream>//pair 5 #include <fstream>//freopen("C:\Users\13606\Desktop\Input.txt","r",stdin); 6 #include <bitset> 7 //#include <map> 8 //#include<unordered_map> 9 #include <vector> 10 #include <stack> 11 #include <set> 12 #include <string.h>//strstr substr strcat 13 #include <string> 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9; 15 #include <cmath> 16 #include <deque> 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less 18 #include <vector>//emplace_back 19 //#include <math.h> 20 #include <cassert> 21 #include <iomanip> 22 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor 23 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare) 24 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation 25 //****************** 26 clock_t __START,__END; 27 double __TOTALTIME; 28 void _MS(){__START=clock();} 29 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;} 30 //*********************** 31 #define rint register int 32 #define fo(a,b,c) for(rint a=b;a<=c;++a) 33 #define fr(a,b,c) for(rint a=b;a>=c;--a) 34 #define mem(a,b) memset(a,b,sizeof(a)) 35 #define pr printf 36 #define sc scanf 37 #define ls rt<<1 38 #define rs rt<<1|1 39 typedef pair<int,int> PII; 40 typedef vector<int> VI; 41 typedef unsigned long long ull; 42 typedef long long ll; 43 typedef double db; 44 const double E=2.718281828; 45 const double PI=acos(-1.0); 46 const ll INF=(1LL<<60); 47 const int inf=(1<<30); 48 const double ESP=1e-9; 49 const int mod=(int)1e9+7; 50 const int N=(int)1e6+10; 51 52 int n,L; 53 int a[N]; 54 db dp[N],sum[N]; 55 db A(int x) 56 { 57 return sum[x]+x; 58 } 59 db B(int x) 60 { 61 return sum[x]+x+L+1; 62 } 63 db X(int x) 64 { 65 return B(x); 66 } 67 db Y(int x) 68 { 69 return dp[x]+B(x)*B(x); 70 } 71 double slope(int a,int b) 72 { 73 return 1.0*(Y(a)-Y(b))/(1.0*(X(a)-X(b))); 74 } 75 76 class mydeque{ 77 public: 78 int len=1000000; 79 int p[N]; 80 int l,r,tot; 81 void init() 82 { 83 l=1;r=1; 84 tot=1; 85 } 86 int size() 87 { 88 return tot; 89 } 90 int pos(int x) 91 { 92 return (x+len)%len; 93 } 94 int f() 95 { 96 return p[l]; 97 } 98 int b() 99 { 100 return p[r]; 101 } 102 void pf(int x) 103 { 104 tot++; 105 l=pos(l-1); 106 p[l]=x; 107 } 108 void pb(int x) 109 { 110 tot++; 111 r=pos(r+1); 112 p[r]=x; 113 } 114 void popf() 115 { 116 p[l]=0; 117 l=pos(l+1); 118 tot--; 119 } 120 void popb() 121 { 122 p[r]=0; 123 r=pos(r-1); 124 tot--; 125 } 126 }q; 127 128 int main() 129 { 130 q.init();//初始化有dp[0](l=r=1而不是l=1,r=0,tot=0),因为要用到 131 sc("%d%d",&n,&L); 132 for(int i=1;i<=n;++i)sc("%d",&a[i]),sum[i]=a[i]+sum[i-1]; 133 for(int i=1;i<=n;++i) 134 { 135 while(q.l<q.r&&slope(q.f(),q.p[q.pos(q.l+1)])<2.0*A(i))q.popf(); 136 dp[i]=dp[q.f()]+(A(i)-B(q.f()))*(A(i)-B(q.f())); 137 while(q.l<q.r&&slope(q.p[q.pos(q.r-1)],q.b())>slope(q.p[q.pos(q.r-1)],i))q.popb(); 138 q.pb(i); 139 } 140 pr("%lld ",(ll)dp[n]); 141 return 0; 142 } 143 144 /**************************************************************************************/
以上是关于DP单调队列--斜率优化P3195的主要内容,如果未能解决你的问题,请参考以下文章