zoukankan      html  css  js  c++  java
  • C2. Pokémon Army (hard version) CF #672


    题意:给你一个序列,让你任意选出一个子序列,使得奇数位和减去偶数位和最大。同时有q个询问,输出每次交换完a[l]和a[r]后的上述最大值。

    思路:首先肯定可以确定选出来的子序列长度为奇数,因为偶数位只会让我们的值减少。
    我们先从简单的情况开始入手,当一个序列极大值只有一个时(只有一个波峰):

    那我肯定只选那个波峰(极大值)就好了,只选一个。


    当这个序列有多个波峰时:

    首先我还是会选一个波峰,a[(x{_1})]。但是发现光只选这个好像很亏。我们肯定想尽量拿多点波峰,但是我们下次选择是偶数位,是负贡献,所以想让它尽量小,所以在知道后面还有波峰的情况下,我们就再选一个a[(x{_2})],然后再选a[(x{_3})]。这个时候就比只选a[(x{_1})]更赚了。同理,我们后面还会继续选a[(x{_4})]、a[(x{_5})]、a[(x{_6})]、a[(x{_7})]。注意最后一个a[n]大可不必选了,因为后面再没有波峰,选了白减去一个数。


    所以问题就变成了求波峰波谷的问题,我们只需要将每一次的波峰之和减去波谷之和即是答案(除去边界两点)。

    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<map>
    #include <queue>
    #include<sstream>
    #include <stack>
    #include <set>
    #include <bitset>
    #include<vector>
    #define FAST ios::sync_with_stdio(false)
    #define abs(a) ((a)>=0?(a):-(a))
    #define sz(x) ((int)(x).size())
    #define all(x) (x).begin(),(x).end()
    #define mem(a,b) memset(a,b,sizeof(a))
    #define max(a,b) ((a)>(b)?(a):(b))
    #define min(a,b) ((a)<(b)?(a):(b))
    #define rep(i,a,n) for(int i=a;i<=n;++i)
    #define per(i,n,a) for(int i=n;i>=a;--i)
    #define endl '
    '
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    using namespace std;
    typedef long long ll;
    typedef pair<ll,ll> PII;
    const int maxn = 3e5+200;
    const int inf=0x3f3f3f3f;
    const double eps = 1e-7;
    const double pi=acos(-1.0);
    const int mod = 1e9+7;
    inline int lowbit(int x){return x&(-x);}
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
    inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
    inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
    inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
    inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
    int dir[3] = { -1, 0, 1 };
    
    ll a[maxn];
    ll ans = 0;
    ll n,q;
    
    void check(int pos)
    {
        if(pos>n||pos<1) return;
        if(a[pos]>a[pos-1]&&a[pos]>a[pos+1]) ans += a[pos];
        if(a[pos]<a[pos-1]&&a[pos]<a[pos+1]) ans -= a[pos];
    }
    
    void Delete(int pos)
    {
        if(pos>n||pos<1) return;
        if(a[pos]>a[pos-1]&&a[pos]>a[pos+1]) ans -= a[pos];
        if(a[pos]<a[pos-1]&&a[pos]<a[pos+1]) ans += a[pos];
    }
    
    
    
    int main()
    {
        int kase;
        cin>>kase;
        while(kase--)
        {
            n = read(), q = read(), ans = 0;
            a[0] = a[n+1] = 0;
            rep(i,1,n) a[i] = read();
            rep(i,1,n) check(i);
            cout<<ans<<endl;
            rep(i,1,q)
            {
                ll l = read(), r = read();
                map<int,int> Map;
                rep(j,0,2) if(!Map[l+dir[j]]) Map[l+dir[j]] = 1, Delete(l+dir[j]);
                rep(j,0,2) if(!Map[r+dir[j]]) Map[r+dir[j]] = 1, Delete(r+dir[j]);
                Map.clear();
                swap(a[l], a[r]);
                rep(j,0,2) if(!Map[l+dir[j]]) Map[l+dir[j]] = 1, check(l+dir[j]);
                rep(j,0,2) if(!Map[r+dir[j]]) Map[r+dir[j]] = 1, check(r+dir[j]);
                cout<<ans<<endl;
            }
        }
        return 0;
    }
    
    
  • 相关阅读:
    securefile allocation chunks
    脚本:Segment Space Usage Explorer
    Script:10g中不用EM显示Active Session Count by Wait Class
    理解IMPDP ORA19505、ORA31640错误
    了解ocssd.bin如何控制RAC节点重启
    Oracle等待事件:NULL EVENT
    LGWR TRACE Warning: log write time
    MySql隔离级别多线程并发读取数据时的正确性
    Oracle事务处理—隔离级别
    Oracle函数详解
  • 原文地址:https://www.cnblogs.com/Bgwithcode/p/13764566.html
Copyright © 2011-2022 走看看