「雜題亂刷2」CF1372D

wangmarui發表於2024-10-05

題目連結

CF1372D Omkar and Circle(*2100)

解題思路

發現問題等價於在環上砍一刀形成一個序列然後取其中不相鄰的數字使得和最大。

如果這是一個序列,我們只需要取奇數位上的數字和和偶數位上的數字和的最大值即可。

我們發現你砍掉一刀等價於把字尾拿到最前面來。

於是我們可以直接前字尾和維護一下然後列舉斷點即可。

時間複雜度 \(O(n)\)

參考程式碼

點選檢視程式碼
#include<bits/stdc++.h>
using namespace std;
//#define map unordered_map
#define re register
#define ll long long
#define forl(i,a,b) for(re ll i=a;i<=b;i++)
#define forr(i,a,b) for(re ll i=a;i>=b;i--)
#define forll(i,a,b,c) for(re ll i=a;i<=b;i+=c)
#define forrr(i,a,b,c) for(re ll i=a;i>=b;i-=c)
#define pii pair<ll,ll>
#define mid ((l+r)>>1)
#define lowbit(x) (x&-x)
#define pb push_back
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
#define db long double
#define ull unsigned long long
#define lcm(x,y) (1ll*(x)/__gcd(x,y)*(y))
#define Sum(x,y) (1ll*((x)+(y))*((y)-(x)+1)/2)
#define x first
#define y second
#define aty cout<<"Yes\n";
#define atn cout<<"No\n";
#define cfy cout<<"YES\n";
#define cfn cout<<"NO\n";
#define xxy cout<<"yes\n";
#define xxn cout<<"no\n";
#define printcf(x) x?cout<<"YES\n":cout<<"NO\n";
#define printat(x) x?cout<<"Yes\n":cout<<"No\n";
#define printxx(x) x?cout<<"yes\n":cout<<"no\n";
#define maxqueue priority_queue<ll>
#define minqueue priority_queue<ll,vector<ll>,greater<ll>>
#define bug cout<<"---------------------------------------\n";
//ll pw(ll x,ll y,ll mod){if(y==0)return 1;if(x==0)return 0;ll an=1,tmp=x;while(y){if(y&1)an=(an*tmp)%mod;tmp=(tmp*tmp)%mod;y>>=1;}return an;}
template<typename T1,typename T2>bool Max(T1&x,T2 y){if(y>x)return x=y,1;return 0;}
template<typename T1,typename T2>bool Min(T1&x,T2 y){if(y<x)return x=y,1;return 0;}
ll _t_;
void _clear(){}
ll n;
ll a[200010],b[200010],ans;
ll pre[200010],suf[200010];
void solve()
{
    _clear();
	cin>>n;
	forl(i,1,n)
		cin>>a[i];
	if(n==1)
	{
		cout<<a[1]<<endl;
		return ;
	}
	pre[1]=a[1];
	forl(i,2,n)
		pre[i]=pre[i-2]+a[i];
	forr(i,n,2)
		suf[i]=suf[i+2]+a[i];
	Max(ans,max(pre[n],pre[n-1]));
	forl(i,1,n)
		Max(ans,pre[i]+suf[i+1+((i%2)==(n-i)%2)]);
	cout<<ans<<endl; 
}
int main()
{
//    freopen("tst.txt","r",stdin);
//    freopen("sans.txt","w",stdout);
    IOS;
    _t_=1;
  //  cin>>_t_;
    while(_t_--)
        solve();
    QwQ;
}