【CodeForces 1255E1 --- Send Boxes to Alice [Easy Version]】

Description

This is the easier version of the problem. In this version, 1≤n≤105 and 0≤ai≤1. You can hack this problem only if you solve and lock both problems.

Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains ai chocolate pieces.

Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a1,a2,…,an is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k>1 such that the number of pieces in each box is divisible by k. Note that Alice won’t mind if there exists some empty boxes.

Charlie, Alice’s boyfriend, also is Bob’s second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i−1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.

Input

The first line contains a single integer n (1≤n≤105) — the number of chocolate boxes.

The second line contains n integers a1,a2,…,an (0≤ai≤1) — the number of chocolate pieces in the i-th box.

It is guaranteed that at least one of a1,a2,…,an is positive.

Output

If there is no way for Charlie to make Alice happy, print −1.

Otherwise, print a single integer x — the minimum number of seconds for Charlie to help Bob make Alice happy.

Sample Input

3
1 0 1

Sample Output

2

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define endl '\n'
using ll = long long;
const int MAXN = 1e6+5;
const ll INF = (1LL<<60);
int arr[MAXN],b[MAXN];
vector<int> v;
ll fun(int x)
{int mid=x>>1,len=v.size();ll sum=0;for(int i=0;i<len;i+=x)for(int j=0;j<x;j++) sum+=abs(v[i+j]-v[i+mid]);return sum;
}int main()
{SIS;ll n,sum=0,x,ans=INF;cin >> n;for(int i=0;i<n;i++){cin >> arr[i];sum+=arr[i];if(arr[i]) v.emplace_back(i);}if(sum<2){cout << -1 << endl;return 0;}for(int i=2;i<=sum;i++)if(sum%i==0) ans=min(ans,fun(i));cout << ans << endl;return 0;
}

【CodeForces 1255E1 --- Send Boxes to Alice [Easy Version]】相关推荐

  1. Codeforces Round #601 (Div. 2) E2. Send Boxes to Alice (Hard Version) 思维 + 质因子

    传送门 文章目录 题意: 思路: 题意: 大体题意跟easyeasyeasy版本差不多,就是hardhardhard版本的aaa范围更大.见这里Codeforces Round #601 (Div. ...

  2. CodeForces - 1543D1 RPD and Rap Sheet (Easy Version)(异或+交互)

    题目链接:点击查看 题目大意:交互题猜密码,设原密码为 xxx,猜的密码为 yyy,如果没猜到,密码会自适应变成 zzz,满足 x⊕z=yx \oplus z=yx⊕z=y ,最多猜 nnn 次 题目 ...

  3. 【Codeforces Round #514 (Div. 2) D. Nature Reserve】 三分+推公式

    题目链接 Codeforces Round #514 (Div. 2) D. Nature Reserve 题意 给你一些二维平面上的点,找一个与x轴相切的半径最小的圆包含所有点. 做法 首先如果两边 ...

  4. 【Codeforces 851D Arpa and a list of numbers】

    Arpa的数列要根据GCD变成好数列. ·英文题,述大意:       给出一个长度为n(n<=5000000)的序列,其中的元素a[i]<=106,然后输入两个数x,y(x,y<= ...

  5. 【codeforces round#800 D题 Fake Plastic Trees】树上贪心

    题目链接 题意: 给你一棵树,树上有n个节点,树的根节点是1,一开始树上的每个节点的权值都是0,现在有一种操作,选择一个节点,使得根节点到这个节点的路径上的所有节点的权值都增加,增加的幅度从根节点到这 ...

  6. 【codeforces Round#801 Div2 D题 Tree Queries】树形贪心结论

    题目链接 题意: 给你一棵树,你需要执行多次询问来确定一个节点x的位置,对于每一次询问,你需要选择一个节点,能得到这个节点与x节点的距离是多少,问至少需要多少次询问才能确定x的位置. 分析: 有一个结 ...

  7. 【Codeforces Round #523 (Div. 2) D.TV Shows】 贪心+二分

    D. TV Shows 题意 给你要看的n个电视节目的 l i 和 r i l_i和r_i li​和ri​,每次要申请一个新的电视的花费是 x + ( r i − l i ) ∗ y x+(r_i-l ...

  8. C1. Make Nonzero Sum (easy version)【Codeforces Round #829 (Div. 2】

    Codeforces Round #829 (Div. 2)中C1题目 Codeforces比赛记录 文章目录 题目链接: 一.C1. Make Nonzero Sum (easy version) ...

  9. 【Codeforces】Codeforces之丰【部分题解】

    Codeforces之丰 [by_041] 文章目录 Codeforces之丰 [by_041] @[toc] Codeforces Round #721 (Div. 2) A. And Then T ...

最新文章

  1. “九韶杯”河科院程序设计协会第一届程序设计竞赛 【前六题解析】
  2. 驰骋表单设计器 设计表单案例演示
  3. python数据集的预处理_关于Pytorch的MNIST数据集的预处理详解
  4. 14、DNS正反向解析、主从复制、子域授权、区域转发 学习笔记
  5. Prometheus监控(二)
  6. [转载]修改SDE权限造成无法在ArcMap中绘制图形的解决办法
  7. mysql配置文件(完整版)
  8. 正则表达式科学计数法_数据科学家的正则表达式
  9. 怎么把图片内存变小尺寸保持不变呢。
  10. 求圆周长,圆面积,圆球表面积、圆球体积,圆柱体积
  11. vs2013 c++小代码运行完了不退出的方法
  12. lte tm模式_请教大家个问题,LTE传输模式TM1-TM8中哪种属于MIM.. - 通信技术你问我答 - 纯技术讨论者的天地 - Powered by C114...
  13. Kubernetes部署策略:重建、滚动更新、蓝绿部署、金丝雀部署
  14. VSCode,插件安装失败,解决方法
  15. 【CV系列】照度和最低照度相关概念
  16. DBSCAN聚类——Python实现
  17. linaro gcc 交叉编译链编译
  18. htc sensation软解锁、获取root权限、解网络锁
  19. 千亿元宇宙市场,Soul、映客的新动力
  20. ue5 lyra探索分析1

热门文章

  1. 帮北航小妹妹做的一道她的C++的作业题.
  2. 5G无人机,到底有什么特别?
  3. 阿里云李克:阿里云边缘云计算的技术和实践
  4. SQL查询重复数据和清除重复数据
  5. JAVA集合框架之List HashSet去重及TreeSet排序详解
  6. java的switch_Java中Switch用法代码示例
  7. 修改注册表,更改Win10版本,解决升级时无法“保留个人文件和应用”的问题
  8. python把英语句子成分字母_英语句子成分和英语句子结构讲解及练习
  9. 交叉编译工具链的设置和命名规则
  10. 联合作战态势可视化决策系统