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

Posted qingyuyyyyy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #601 (Div. 2) E1 Send Boxes to Alice (Easy Version)相关的知识,希望对你有一定的参考价值。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
int a[N];
int n;
bool prime(int x) {//判断是否为质数
    for(int i = 2; i*i <= x; i++) {
        if(x%i == 0) return false;
    }
    return true;
}
ll solve(int x) {
    vector<int>b;
    ll ans = 0;
    for(int i = 1; i <= n; i++) {
        if(a[i] == 1 && b.size() < x) b.push_back(i);//存放位置
        if(b.size() == x) {
            for(int j = 0; j < b.size(); j++) {
                ans += (ll)abs(b[j]-b[x/2]);
            }
            b.clear();
        }
    }
    return ans;
}
int main() {
    int sum = 0;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++) {
        scanf("%d",a+i);
        sum += a[i];
    }
    if(sum == 1) {
        printf("-1
");
        return 0;
    }
    ll ans = 0x3f3f3f3f3f;
    for(int i = 2; i <= sum; i++) {
        if(sum%i==0&&prime(i)) {//此处用质因子优化,不加优化也行。 
            ans = min(ans,solve(i));
        }
    }
    printf("%lld
",ans);
    return 0;
}

 

 

 

以上是关于Codeforces Round #601 (Div. 2) E1 Send Boxes to Alice (Easy Version)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #601 (Div. 2)D(蛇形模拟)

Codeforces Round #601 (Div. 2) A-E

Codeforces Round #601 (Div. 2) D Feeding Chicken

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

Codeforces Round #601 (Div. 2)E(寻找质因子,DP)

Codeforces Round #436 E. Fire(背包dp+输出路径)