codeforces 1358 D 尺区

想不到二分的解法

大佬尺区的思维真清晰,而且模拟的不复杂。。。

题意:给了n个月,每月有di天,选取连续的x天,在这x天产生的幸福度sum为这一天是这个月的第几天的和。让sum最大。

思路:很明显从每个月的最后一天开始选择,然后往前选x天,尺区搞搞。

  我最先是想从1-n模拟尺区的,发现太多的细节模拟不好,看了大佬的代码真简单。

  从n往前模拟,每次选完取这一个月,如果选多了一定多出了1到-x的等差数组和(如果恰好选完则等差数组和为0)。对于i计算完之后直接用sum减即可。

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <vector> 
// #include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sp ' '
#define endl '
'
#define inf  0x3f3f3f3f;
#define FOR(i,a,b) for( int i = a;i <= b;++i)
#define bug cout<<"--------------"<<endl
#define P pair<int, int>
#define fi first
#define se second
#define pb(x) push_back(x)
#define ppb() pop_back()
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define sca3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define sca2(a,b) scanf("%d %d",&(a),&(b))
#define sca(a) scanf("%d",&(a));
#define sca3ll(a,b,c) scanf("%lld %lld %lld",&(a),&(b),&(c))
#define sca2ll(a,b) scanf("%lld %lld",&(a),&(b))
#define scall(a) scanf("%lld",&(a));


using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a, ll b, ll mod){ll sum = 1;while (b) {if (b & 1) {sum = (sum * a) % mod;b--;}b /= 2;a = a * a % mod;}return sum;}

const double Pi = acos(-1.0);
const double epsilon = Pi/180.0;
const int maxn = 2e5+10;
ll n,x;
ll d[maxn];
ll c[1000100];
int main()
{
    //freopen("input.txt", "r", stdin);
    cin>>n>>x;
    ll maxx = 0;
    rep(i,1,n){
        cin>>d[i];
        maxx = max(maxx,d[i]);
    }
    rep(i,1,maxx){
        c[i] = c[i-1] + i;
    }
    int mon = n;
    ll ans = 0,sum = 0;
    repd(i,n,1){
        while(x > 0){
            x -= d[mon];
            sum += c[d[mon]];
            mon--;
            if(mon <= 0) mon = n;
        }        
        ans = max(ans,sum-(x-1)*x/2);
        sum -= c[d[i]];
        x += d[i];
    }
    cout<<ans<<endl;


}
原文地址:https://www.cnblogs.com/jrfr/p/13301876.html