首页 写随笔

cdcq(本博客废弃!现用博客:https://www.cnblogs.com/cdcq/)

本博客废弃!现用博客:https://www.cnblogs.com/cdcq/

导航

codeforces 788A Functions again

……

原题:

Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:

In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.

2 ≤ n ≤ 105

 

-109 ≤ ai ≤ 109

一句话题意:

给一个数列a,求一个l和r使得上述函数最大

 

从i开始推dp,r==i的话f的值还和l有关,但是l对f的影响只和l的奇偶性有关

所以f[i][0]表示l在奇数,f[i][1]表示l在偶数,g[i][0]和g[i][1]表示f[1~i][0]和f[1~i][1]的最小值

然后推f,维护g,更新答案即可

注意longlong

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<ctime>
 7 using namespace std;
 8 const int oo=(1<<31)-1;
 9 int rd(){int z=0,mk=1;  char ch=getchar();
10     while(ch<'0'||ch>'9'){if(ch=='-')mk=-1;  ch=getchar();}
11     while(ch>='0'&&ch<='9'){z=(z<<3)+(z<<1)+ch-'0';  ch=getchar();}
12     return z*mk;
13 }
14 int n,a[110000];
15 long long f[110000][2],g[110000][2];
16 long long mx=0;
17 int main(){//freopen("ddd.in","r",stdin);
18     cin>>n;
19     for(int i=1;i<=n;++i)  a[i]=rd();
20     f[0][1]=a[1]-a[2];
21     for(int i=1;i<n;++i){
22         f[i][0]=f[i-1][0]+abs(a[i]-a[i+1])*(i&1?1:-1);
23         f[i][1]=f[i-1][1]+abs(a[i]-a[i+1])*(i&1?-1:1);
24         //cout<<(a[i]-a[i+1])*(i&1?1:-1)<<" "<<(a[i]-a[i+1])*(i&1?-1:1)<<endl;
25         g[i][0]=min(g[i-1][0],f[i][0]);
26         g[i][1]=min(g[i-1][1],f[i][1]);
27         mx=max(mx,f[i][0]-g[i][0]);
28         mx=max(mx,f[i][1]-g[i][1]);
29         //cout<<f[i][0]<<" "<<f[i][1]<<" "<<g[i][0]<<" "<<g[i][1]<<endl;
30     }
31     cout<<mx<<endl;
32     return 0;
33 }
View Code

 

posted on 2017-03-31 11:09  cdcq_old  阅读(410)  评论(0编辑  收藏  举报