Description

\(f(s)\) 表示 \(01\)\(S\) 中多有少个子串中至少有一个 \(1\)。对于长度为 \(n\) 且恰好有 \(m\)\(1\)\(01\) 串,求 \(f\) 的最大值为多少。

Solution

要最大化 \(f\),对于给定的 \(n,m\) 就是要最小化 \(\sum x_i\),其中 \(x_i\) 代表第 \(i\) 段连续 \(0\) 的长度。

即将 \(n-m\)\(0\) 分成 \(m+1\) 份,每份的数量可以是任意非负整数,最小化 \(\sum x_i\),显然尽可能均匀分配是最优的,即保证 \(\max - \min \le 1\)

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

#define int long long 
const int N = 1000005;

int f(int d,int r,int k)
{
    return r*(d+1)*(d+1)+(k-r)*d*d;
}

int g(int s,int k)
{
    return f(s/k,s%k,k);
}

void solve()
{
    int n,m;
    cin>>n>>m;
    cout<<(n*(n+1)-n+m-g(n-m,m+1))/2<<endl;
}

signed main()
{
    int t;
    cin>>t;
    while(t--) solve();
}

相关文章:

  • 2022-01-10
  • 2021-09-27
  • 2021-08-05
  • 2021-09-19
  • 2022-12-23
  • 2021-08-25
猜你喜欢
  • 2022-12-23
  • 2021-07-23
  • 2018-10-23
  • 2021-10-18
  • 2022-12-23
  • 2021-08-19
  • 2021-08-29
相关资源
相似解决方案