码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 1358D - The Best Vacation (贪心)

时间:2020-05-27 12:11:53      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:ons   tput   syn   exactly   space   from   strong   sync   迭代   


题面

time limit per test: 2 seconds

memory limit per test: 256 megabytes

Description

You‘ve been in love with Coronavirus-chan for a long time, but you didn‘t know where she lived until now. And just now you found out that she lives in a faraway place called Naha.

You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly x days and that‘s the exact number of days you will spend visiting your friend. You will spend exactly x consecutive (successive) days visiting Coronavirus-chan.

They use a very unusual calendar in Naha: there are nn months in a year, i-th month lasts exactly di days. Days in the i-th month are numbered from 1 to di. There are no leap years in Naha.

The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get j hugs if you visit Coronavirus-chan on the j-th day of the month.

You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan).

Please note that your trip should not necessarily begin and end in the same year.

Input

The first line of input contains two integers n and x (1≤n≤2?105) — the number of months in the year and the number of days you can spend with your friend.

The second line contains n integers d1,d2,…,dn, didi is the number of days in the i-th month (1≤di≤106).

It is guaranteed that 1≤x≤d1+d2+…+dn.

Output

Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life.

Examples

input 1

3 2
1 3 1

output 1

5

input 2

3 6
3 3 3

output 2

12

input 3

5 6
4 2 3 1 3

output 3

15

Note

In the first test case, the numbers of the days in a year are (indices of days in a corresponding month) {1,1,2,3,1}. Coronavirus-chan will hug you the most if you come on the third day of the year: 2+3=5 hugs.

In the second test case, the numbers of the days are {1,2,3,1,2,3,1,2,3}. You will get the most hugs if you arrive on the third day of the year: 3+1+2+3+1+2=12 hugs.

In the third test case, the numbers of the days are {1,2,3,4,1,2,1,2,3,1,1,2,3}. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you 2+3+1+2+3+4=15 times.




题意

一年有 n 个月,第 i 个月有 d[i] 天,每个月的第 j 天价值为 j

要求选取严格连续的 x 天,使得价值总和最大

也就是将所有的 d[i] 全部展开为 1, 2, ... ,d[i] ,取一段长度为 x 的区间,使得价值最大

允许选取的天数不是同一年(即展开后首尾相连)

※如样例 3 可将 "4 2 3 1 3" 展开为 {1,2,3,4,1,2,1,2,3,1,1,2,3} 再选取长度为 6 的区间求最大和




解题思路

可以发现最优的选取方案肯定会选到某个月的最后一天(展开序列中的某个峰值)

所以可以枚举起始月份 i (从后往前),从第 i 个月最后一天开始向前选择 x 天求和取大


预处理 sum 数组,sum[i] = SUM{1,2,...,i} = (1+i)*i/2 ,或用迭代 sum[i] = sum[i-1]+i

将 x 视为还应该选取的天数,将 x>0作为条件执行 while 循环

取 tmp 为当前选中的月份的价值总和

每多选一个月份 p

则 x -= d[p] ,表示选中了 d[p] 天

tmp += sum[d[p]] ,表示区间总和加上 sum[d[p]]

直到 x<=0 ,开始计算从第 i 个月开始往前选择 x 天的总和

如果 x=0 ,则刚好选择了要求的天数,答案即 tmp

如果 x<0 ,则多选择了 -x 天,tmp 应再减去从 1 到 -x 的和,即 tmp-(1-x)*(-x)/2

计算答案后,取消选择月份 i ,即 x -= d[i] , tmp -= sum[d[i]]




完整程序

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll d[200050],sum[1000050];

void solve()
{
    int n,p;
    ll x,ans=0,tmp=0;
    
    for(int i=1;i<=1000000;i++)
        sum[i]=sum[i-1]+i;
    
    cin>>n>>x;
    for(int i=1;i<=n;i++)
        cin>>d[i];
    
    p=n;
    for(int i=n;i>0;i--)
    {
        while(x>0) //只要还能继续选择
        {
            x-=d[p]; //选中第p个月
            tmp+=sum[d[p]]; //将第i个月的贡献加入总和
            if(p==1) //首尾循环
                p=n;
            else
                p--;
        }
        ans=max(ans,tmp-(1-x)*(-x)/2); //计算选中的实际总和,取大
        tmp-=sum[d[i]];
        x+=d[i];
    }
    cout<<ans<<‘\n‘;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    solve();
    return 0;
}

Codeforces 1358D - The Best Vacation (贪心)

标签:ons   tput   syn   exactly   space   from   strong   sync   迭代   

原文地址:https://www.cnblogs.com/stelayuri/p/12971546.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!