题意

N个盒子,每个盒子有a[i]块巧克力,每次操作可以将盒子中的一块巧克力左移或右移,要求移动后的每个盒子中的巧克力数量都能被k整除(无视空盒子),求最小的操作数。(1<=N<=1e6,0<=a[i]<=1e6)

思路

  • k只需考虑巧克力总数的质因子
  • 考虑每个盒子的贡献,盒子中可以保存k的整数倍(k*i)块巧克力,从左向右递推,每个盒子保留最大数目的巧克力,假设剩余的巧克力数目为x,则有两种情况,一是,x全部给后一个位置的盒子,二是,由后一个位置的盒子给他(k-x)块巧克力。而无论是哪种情况,都不会影响后一个盒子剩余巧克力的数量。
  • 分治的考虑这道题,将n个盒子视为两部分,每个部分能解决完k的整数倍(k*i)块巧克力,而两部分交换巧克力(距离为1)的最小代价就是对两部分的剩余巧克力取最小值,而任意两部分剩余的巧克力之和总为0或k
  • 读入不能用cin

代码

#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 10;
int a[maxn],n;
ll sum[maxn];
inline ll solve(ll k){
ll res=0;
for(int i=1;i<=n;i++){
ll x=sum[i]%k;
res+=min(x,k-x);
}
return res;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",a+i);
sum[i]=a[i]+sum[i-1];
}
ll tot=sum[n],ans=LLONG_MAX;
for(ll i=2;i*i<=tot;i++){
if(tot%i) continue;
ans=min(ans,solve(i));
while (tot%i==0) {
tot/=i;
}
}
if(tot!=1) ans=min(ans,solve(tot));
if(ans!=LLONG_MAX)cout<<ans<<endl;
else cout<<-1<<endl;
}

Codeforces Round #601 (Div. 2) E2. Send Boxes to Alice (Hard Version)的更多相关文章

  1. Codeforces Round #601 (Div. 2) E1 Send Boxes to Alice (Easy Version)

    #include <bits/stdc++.h> using namespace std; typedef long long ll; ; int a[N]; int n; bool pr ...

  2. E2. Send Boxes to Alice (Hard Version)

    秒的有点难以理解:https://blog.csdn.net/weixin_42868863/article/details/103200132 #include<bits/stdc++.h&g ...

  3. Codeforces Round #601 (Div. 2)

    传送门 A. Changing Volume 签到. Code /* * Author: heyuhhh * Created Time: 2019/11/19 22:37:33 */ #include ...

  4. 【cf比赛记录】Codeforces Round #601 (Div. 2)

    Codeforces Round #601 (Div. 2) ---- 比赛传送门 周二晚因为身体不适鸽了,补题补题 A // http://codeforces.com/contest/1255/p ...

  5. Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】

    传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...

  6. CodeForces -Codeforces Round #496 (Div. 3) E2. Median on Segments (General Case Edition)

    参考:http://www.cnblogs.com/widsom/p/9290269.html 传送门:http://codeforces.com/contest/1005/problem/E2 题意 ...

  7. Codeforces Round #567 (Div. 2) E2 A Story of One Country (Hard)

    https://codeforces.com/contest/1181/problem/E2 想到了划分的方法跟题解一样,但是没理清楚复杂度,很难受. 看了题解觉得很有道理,还是自己太菜了. 然后直接 ...

  8. Codeforces Round #496 (Div. 3) E2 - Median on Segments (General Case Edition)

    E2 - Median on Segments (General Case Edition) 题目大意:给你一个数组,求以m为中位数的区间个数. 思路:很巧秒的转换,我们把<= m 数记为1, ...

  9. Codeforces Round #278 (Div. 2) B. Candy Boxes [brute force+constructive algorithms]

    哎,最近弱爆了,,,不过这题还是不错滴~~ 要考虑完整各种情况 8795058                 2014-11-22 06:52:58     njczy2010     B - Ca ...

随机推荐

  1. vue-echarts在vue中的使用

    安装依赖: [win]npm install echarts vue-echarts [mac]sudo npm install echarts vue-echarts Vue-ECharts 默认在 ...

  2. 【操作系统之四】Linux常用命令之awk

    一.概念awk是一个报告生成器,拥有强大的文本格式化能力. 数据可以来自标准输入(stdin).一个或多个文件,或其它命令的输出: 依次对每一行进行处理,然后输出: 它在命令行中使用,但更多是作为脚本 ...

  3. python cython c 性能对比

    我们用以下方法计算百万以上float型数据的标准偏差,以估计各个方法的计算性能: 原始python numpy cython c(由cython调用) python 原始方法: # File: Std ...

  4. Alpha冲刺(11/10)——2019.5.3

    作业描述 课程 软件工程1916|W(福州大学) 团队名称 修!咻咻! 作业要求 项目Alpha冲刺(团队) 团队目标 切实可行的计算机协会维修预约平台 开发工具 Eclipse 团队信息 队员学号 ...

  5. Python【每日一问】27

    问: [基础题1]:将一个正整数分解质因数.例如:输入 90, 打印出 90=2*3*3*5 . [基础题2]:一个数如果恰好等于它的因子之和,这个数就称为“完数” .例如6=1+2+3.请找出 10 ...

  6. ArcSOC进程数不断增长导致oracle processes溢出原因分析

    现场出现了一个问题,oracle运行一段时间之后,process个数会溢出,然后新的连接会失败.通过分析,发现Arcgis Server 的ArcSOC进程在不段增长.ArcSOC是arcgis se ...

  7. PowerBuilder学习笔记之2PowerScript语言(一)

    教材链接:https://wenku.baidu.com/view/1e82d26925c52cc58ad6be05.html?sxts=1565679996440 2.1PowerScript基础 ...

  8. groovy常用语法及实战

    groovy语言简介 一种基于JVM的敏捷开发语言,作为编程语言可编译成java字节码,也可以作为脚本语言解释执行. 结合了Python.Ruby和Smalltalk的许多强大的特性 支持面向对象编程 ...

  9. FRP represents an intersection of two programming paradigms.

    FRP represents an intersection of two programming paradigms. Functional programming Functional progr ...

  10. c#栈的用法

    栈是一种重要的线性结构,栈和队列是限定插入和删除只能在表的“端点”进行的线性表 –栈的元素必须“后进先出”. –栈的操作只能在这个线性表的表尾进行. –注:对于栈来说,这个表尾称为栈的栈顶(top), ...