zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    MySQL系列(三) MySQL的约束
    mysql 下载 国内 镜像
    ckeditor
    比较时间
    远程获取文件
    多线程一例
    requests
    json传递对象字典
    pymysql和mysqldb的区别
    sql
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/11946511.html
Copyright © 2011-2022 走看看