题面

题目大意:

给定一个 \(n\) , 所有军人的数量均在 \([1, n]\)

给定 \(a_i\) 代表高度为 \(i\) 的军人的个数

你要将这些军人分成 \(k\) 行, 满足下面两个条件

  • 每行人数相等
  • 同一行任意两个军人的高度差的绝对值不超过 1

问你最多能够选多少个军人分成 \(k\) 行

题解

题目满足单调性, 可以二分

我们二分一个 \(mid\) 表示每一行有 \(mid\) 个军人

不难证得首先将高度相等的分成一行, 再分高度不相等的是最优的

简要证明 : 可将不是上述方案的方案调整为是上述方案的方案, 结果不会更差

那么现在我们就是要看怎么分是最优的

我们将高度为 \(i + 1\) 的并到 \(i\) 上去等价于把 \(i\) 的并到 \(i + 1\) 上去

假如说 \(a_{i + 1} >= a_i \% mid\) , 我们就可以将 \(a_{i + 1}\) 减去 \(a_i \% mod\) , 再将 \(ans\) 加一

正确性:

首先如果不用 \(a_i\) 剩下的, 和这部分剩下的配对的 \(a_{i + 1}\) 肯定是要跟自己或者 \(a_{i + 2}\) 配对的

也就是说这一部分肯定是要用的, 如果用在自己和后面身上, 可能会导致后面的无法配对

也就是说

这一部分总会配对, 跟前面配对的不会对后面造成影响, 因为后面的总数没变

跟后面的配对可能会对后面的答案造成影响, 因为总数变了

由此可见这样配对不会更差

Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
typedef long long ll;
const int N = 30005;
using namespace std; int T, n;
ll k, a[N], b[N], ans, sum; template < typename T >
inline T read()
{
T x = 0, w = 1; char c = getchar();
while(c < '0' || c > '9') { if(c == '-') w = -1; c = getchar(); }
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * w;
} bool check(ll x)
{
ll res = 0; b[1] = a[1];
for(int i = 1; i <= n; i++)
{
res += b[i] / x;
b[i + 1] = a[i + 1];
if(b[i + 1] >= x - (b[i] % x))
b[i + 1] -= x - (b[i] % x), res++;
}
return res >= k;
} int main()
{
T = read <int> ();
while(T--)
{
ans = sum = 0; n = read <int> (), k = read <ll> ();
for(int i = 1; i <= n; i++)
sum += (a[i] = read <ll> ());
a[n + 1] = 0;
ll l = 1, r = sum;
while(l <= r)
{
ll mid = (l + r) >> 1;
if(check(mid)) ans = mid, l = mid + 1;
else r = mid - 1;
}
printf("%lld\n", 1ll * ans * k);
}
return 0;
}

[题解] [CF 1250J] The Parade的更多相关文章

  1. 竞赛题解 - CF Round #524 Div.2

    CF Round #524 Div.2 - 竞赛题解 不容易CF有一场下午的比赛,开心的和一个神犇一起报了名 被虐爆--前两题水过去,第三题卡了好久,第四题毫无头绪QwQ Codeforces 传送门 ...

  2. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T5(思维)

    还是dfs? 好像自己写的有锅 过不去 看了题解修改了才过qwq #include <cstdio> #include <algorithm> #include <cst ...

  3. 竞赛题解 - [CF 1080D]Olya and magical square

    Olya and magical square - 竞赛题解 借鉴了一下神犇tly的博客QwQ(还是打一下广告) 终于弄懂了 Codeforces 传送门 『题目』(直接上翻译了) 给一个边长为 \( ...

  4. 题解 CF 1372 B

    题目 传送门 题意 给出 \(n\),输出 \(a\) ,\(b\) (\(0 < a \leq b < n\)),使\(a+b=n\)且 \(\operatorname{lcm}(a,b ...

  5. CodeForces - 1250J The Parade 二分

    题目 题意: 一共n种身高,每一个士兵有一个身高.你需要把他们安排成k行(士兵不需要全部安排),每一行士兵身高差距小于等于1.你要找出来最多能安排多少士兵 题解: 这道题很容易就能看出来就是一道二分, ...

  6. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T4(模拟)

    随便模拟下就过了qwq 然后忘了特判WA了QwQ #include <cstdio> #include <algorithm> #include <cstring> ...

  7. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T3(贪心)

    是一道水题 虽然看起来像是DP,但其实是贪心 扫一遍就A了 QwQ #include <cstdio> #include <algorithm> #include <cs ...

  8. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T2(模拟)

    题目要求很简单,做法很粗暴 直接扫一遍即可 注意结果会爆int #include <cstdio> #include <algorithm> #include <cstr ...

  9. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T1(找规律)

    就是找一下规律 但是奈何昨天晚上脑子抽 推错了一项QwQ 然后重新一想 A掉了QwQ #include <cstdio> #include <algorithm> #inclu ...

随机推荐

  1. Xamarin(Android)制作启动画面

    1.将启动图片保存到Drawable文件夹下 2.在Drawable文件夹下创建splashscreen.xml <?xml version="1.0" encoding=& ...

  2. DataGridView绑定数据、删除数据

    定义学生类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  3. nc 命令

    目录 nc 命令 一.简介 二.案例 1.端口扫描 2.聊天 3.文件传输 4.目录传输 5.加密网络发送的数据 6.流视频 7.克隆一个设备 8.打开一个shell 9.反向shell 10.指定端 ...

  4. springboot和Redis集群版的整合

    此篇接上一个文章springboot和Redis单机版的整合 https://www.cnblogs.com/lin530/p/12019023.html 下面接着介绍和Redis集群版的整合. 1. ...

  5. Image Processing and Analysis_15_Image Registration:Mutual-Information-Based Registration of Medical Survey——2003

    此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...

  6. 【异常】java.sql.SQLException: Could not retrieve transaction read-only status from server Query

    1 详细异常 java.sql.SQLException: Could not retrieve transaction read-only status , ], [ChargingOrderRea ...

  7. 01 Windows编程——Hello World

    源码 #include "stdafx.h" #include<Windows.h> int WINAPI WinMain(HINSTANCE hInst,HINSTA ...

  8. Go语言——概念

    静态类型.动态类型.潜在类型 静态类型:指在变量声明中示出的那个类型.绝大多数类型都只有静态类型.唯独接口类型的变量例外,他除了拥有静态类型之外,还拥有动态类型. 动态类型:指在运行时与该变量绑定在一 ...

  9. C# Winfrom DataGridView常用设置

    DataGridView常用设置 using System; using System.Collections.Generic; using System.Drawing; using System. ...

  10. Canal的简单使用(监控数据库数据的变化)

    原文:https://www.cnblogs.com/java-spring/p/8930740.html canal可以用来监控数据库数据的变化,从而获得新增数据,或者修改的数据,用于实际工作中,比 ...