「杂题乱刷2」CF1372D

题目链接

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;
}
posted @ 2024-10-05 14:27  wangmarui  阅读(5)  评论(0编辑  收藏  举报