日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces - 1358D The Best Vacation(前缀和+尺取)

發布時間:2024/4/11 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 1358D The Best Vacation(前缀和+尺取) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:給出 n 個數組成的數列,每個元素都可以展開為 1 , 2 , 3 .... a[ n ] ,現在將數列首尾相接,要求選取一段長度為 x 的連續數列,使得元素和最大

題目分析:因為 n 是 2e5 , a[ i ] 是 1e6 ,暴力展開肯定不行,但是仔細樣例觀察不難發現,最終選擇的這段數列,結尾一定是 a[ i ] ,那么我們可以 O( n ) 枚舉作為結尾的 a[ i ] ,然后尺取定位到起點,每次維護最大值就是答案了

代碼:
?

#include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<unordered_map> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=2e5+100;LL a[N<<1],cnt[N<<1],sum[N<<1];LL cal(LL l,LL r)//計算 l+(l+1)+...+(r-1)+r {return (l+r)*(r-l+1)/2; }int main() { #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif // ios::sync_with_stdio(false);int n;LL x;scanf("%d%lld",&n,&x);for(int i=1;i<=n;i++){scanf("%lld",a+i);a[i+n]=cnt[i]=cnt[i+n]=a[i];sum[i]=sum[i+n]=cal(1,a[i]);}for(int i=1;i<=2*n;i++){cnt[i]+=cnt[i-1];sum[i]+=sum[i-1];}LL ans=0;for(int i=2*n,j=2*n;i>n;i--)//枚舉終點{while(j>0&&cnt[i]-cnt[j]<=x)j--;ans=max(ans,sum[i]-sum[j]-cal(1,cnt[i]-cnt[j]-x));}printf("%lld\n",ans);return 0; }

?

總結

以上是生活随笔為你收集整理的CodeForces - 1358D The Best Vacation(前缀和+尺取)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。