[题解] [CF 1250J] The Parade
题面
题目大意:
给定一个 \(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的更多相关文章
- 竞赛题解 - CF Round #524 Div.2
CF Round #524 Div.2 - 竞赛题解 不容易CF有一场下午的比赛,开心的和一个神犇一起报了名 被虐爆--前两题水过去,第三题卡了好久,第四题毫无头绪QwQ Codeforces 传送门 ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T5(思维)
还是dfs? 好像自己写的有锅 过不去 看了题解修改了才过qwq #include <cstdio> #include <algorithm> #include <cst ...
- 竞赛题解 - [CF 1080D]Olya and magical square
Olya and magical square - 竞赛题解 借鉴了一下神犇tly的博客QwQ(还是打一下广告) 终于弄懂了 Codeforces 传送门 『题目』(直接上翻译了) 给一个边长为 \( ...
- 题解 CF 1372 B
题目 传送门 题意 给出 \(n\),输出 \(a\) ,\(b\) (\(0 < a \leq b < n\)),使\(a+b=n\)且 \(\operatorname{lcm}(a,b ...
- CodeForces - 1250J The Parade 二分
题目 题意: 一共n种身高,每一个士兵有一个身高.你需要把他们安排成k行(士兵不需要全部安排),每一行士兵身高差距小于等于1.你要找出来最多能安排多少士兵 题解: 这道题很容易就能看出来就是一道二分, ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T4(模拟)
随便模拟下就过了qwq 然后忘了特判WA了QwQ #include <cstdio> #include <algorithm> #include <cstring> ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T3(贪心)
是一道水题 虽然看起来像是DP,但其实是贪心 扫一遍就A了 QwQ #include <cstdio> #include <algorithm> #include <cs ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T2(模拟)
题目要求很简单,做法很粗暴 直接扫一遍即可 注意结果会爆int #include <cstdio> #include <algorithm> #include <cstr ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T1(找规律)
就是找一下规律 但是奈何昨天晚上脑子抽 推错了一项QwQ 然后重新一想 A掉了QwQ #include <cstdio> #include <algorithm> #inclu ...
随机推荐
- DotNet跨平台 - docker部署.net core2.0项目
参考文档: https://docs.docker.com/install/linux/docker-ce/centos/ http://www.dockerinfo.net/document htt ...
- (详细)JAVA使用JDBC连接MySQL数据库(2)- MySQL Connectors
欢迎任何形式的转载,但请务必注明出处. 本节内容 mysql connectors介绍 下载安装 在java中配置 点击进入官网下载 一.mysql connectors介绍 mysql connec ...
- 如何用SAP WebIDE的Fiori创建向导基于ABAP OData service快速创建UI5应用
如果我们手上已经有可以正常工作的OData服务,无论位于ABAP on-premise系统还是public上的internet OData service,都可以用SAP WebIDE里的Fiori创 ...
- Ubuntu-Python2.7安装 scipy,numpy,matplotlib (转)
sudo apt-get install python-scipy sudo apt-get install python-numpy sudo apt-get install python-matp ...
- 一线互联网常见的Java面试题,你颤抖了吗程序员
跳槽不算频繁,但参加过不少面试(电话面试.face to face面试),面过大/小公司.互联网/传统软件公司,面糊过(眼高手低,缺乏实战经验,挂掉),也面过人,所幸未因失败而气馁,在此过程中不断查缺 ...
- 【转】Linux下常用压缩 解压命令和压缩比率对比
https://www.cnblogs.com/joshua317/p/6170839.html 常用的格式有:tar, tar.gz(tgz), tar.bz2, 不同方式,压缩和解压方式所耗CPU ...
- ASP.NET MVC Liu_Cabbage 个人博客
RightControl_Blog 介绍 前台使用燕十三博客前端模板,后台基于RightControl .NET通用角色权限管理系统搭建,已完成.项目地址:http://www.baocaige.to ...
- ES使用org.elasticsearch.client.transport.NoNodeAvailableException: No node available
1) 端口错 client = new TransportClient().addTransportAddress(new InetSocketTransportAddress(ipAddress, ...
- 遍历windows窗口
原文 1. GetDesktopWindow GetNextWindow HWND hAll = ::GetDesktopWindow(); HWND hCurrent = ::GetNextWind ...
- FastDFS+Nginx+Module
1.安装libevent wget https://cloud.github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.g ...