zoukankan      html  css  js  c++  java
  • codeforces C. Functions again

      题意:给定了一个公式,让你找到一对(l,r),求解出公式给定的F值。

      当时没有想到,我把(-1)^(i-l)看成(-1)^i,然后思路就完全错了。其实这道题是个简单的dp+最长连续子序列。

      O(n)求最长连续子序列代码

        ll maxx=0, sum=0, now=0;
        for (int i=1; i<n; i++) {    //数列1-n
            sum+=dp1[i];
            maxx=max(maxx, sum);
            if (sum<0)  sum=0;
        }

      其实我们可以发现,其实正负是交错的,那么我们只要用两个dp(正负相反)的数组来存,再求一次最长连续子序列就好了。

    /*  gyt
           Live up to every day            */
    #include <stdio.h>
    #include <cstdio>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <stack>
    #include <cstring>3
    #include <queue>
    #include <set>
    #include <string>
    #include <map>
    #include <time.h>
    #define PI acos(-1)
    using namespace std;
    typedef long long ll;
    typedef double db;
    const int maxn = 1e5+10;
    const ll maxm = 1e7;
    const int mod = 1000000007;
    const int INF = 1<<30;
    const db eps = 1e-9;
    const ll Max=1e19;
    ll a[maxn], dp1[maxn], dp2[maxn];
    
    void solve() {
        int n;  scanf("%d", &n);
        for (int i=1; i<=n; i++) {
            scanf("%lld", &a[i]);
        }
        memset(dp1, 0, sizeof(dp1));
        memset(dp2, 0, sizeof(dp2));
        int f=1;
        for (int i=1; i<=n-1; i++) {
            dp1[i]=abs(a[i]-a[i+1])*f;
            dp2[i]=abs(a[i]-a[i+1])*(-f);
            f = -f;
        }
        ll maxx=0, sum=0, now=0;
        for (int i=1; i<n; i++) {
            sum+=dp1[i];
            maxx=max(maxx, sum);
            if (sum<0)  sum=0;
        }
        sum=0;
        for (int i=1; i<n; i++) {
            sum+=dp2[i];
            maxx=max(maxx, sum);
            if (sum<0)  sum=0;
        }
        cout<<maxx<<endl;
    }
    int main() {
        int t=1;
        //freopen("in.txt", "r", stdin);
        //scanf("%d", &t);
        for (int T=1; T<=t; T++) {
            solve();
        }
    }
  • 相关阅读:
    nmake不是内部或外部命令,也不是可运行的程序
    MinGW下载和安装教程
    Qt接单
    C++ web 框架
    原型链
    ssh: Could not resolve hostname的一种解决方案
    oracle客户端安装配置
    linux安装go
    golang 为什么结构体方法要写成指针传递
    traceback模块
  • 原文地址:https://www.cnblogs.com/gggyt/p/7286882.html
Copyright © 2011-2022 走看看