题意

n个数字的序列a,将i位置向j位置转移x个(a[i]-x,a[j]+x)的花费为\(x\times |i-j|\),最终状态可行的条件为所有a[i]均被K整除(K>1),求最小花费

做法

\(sum=\sum\limits a\),则\(K|sum\)

  • 有\(K1|sum,K2|sum\),若\(K1|K2\),则转移到被K1整除比转移到K2更优。这个是显然的,所以最终可能成为最优解的K个数为\(logsum\le 40\)

对于一个枚举到的K,将\(b[i]=a[i]\% K\)

对于b不为0的位置,我们类似贪心得考虑

  • 仅有前面一个位置pre不合法:因为如果有两个,在之前我们可以合成一个

  • 如果b[i]可以与前一个补满(b[i]可能还会有多余的),考虑从i转移到pre优还是pre转移到i优

  • 如果b[i]不能补满前一个,这个时候我们考虑如果能补满会补到哪里,然后更新一下pre

Code

比赛时调了好久,码风有点奇怪

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long long ll;
const LL maxn=1e6+9;
const ll inf=0x3f3f3f3f3f3f3f3f;
LL Read(){
LL x(0),f(1); char c=getchar();
while(c<'0' || c>'9'){
if(c=='-') f=-1; c=getchar();
}
while(c>='0' && c<='9'){
x=(x<<3)+(x<<1)+c-'0'; c=getchar();
}return x*f;
}
LL n,T,tot;
ll sum;
LL a[maxn],b[maxn];
ll bel[maxn];
bool Check(ll x){
for(LL i=1;i<=tot;++i) if(x%bel[i]==0) return false;
return true;
}
int main(){
n=Read();
for(LL i=1;i<=n;++i) a[i]=Read(),sum+=a[i]; /*srand(time(NULL));
n=1000000;
for(LL i=1;i<=n;++i) a[i]=rand()%1000001,sum+=a[i];
printf("%lld\n",sum);*/ for(LL i=2,up=sqrt(sum);i<=up;++i) if(sum%i==0){ if(Check(i)) bel[++tot]=i;
if(Check(sum/i)) bel[++tot]=sum/i;
}
if(Check(sum)) bel[++tot]=sum;
// printf("%d\n",tot);
if(sum==0 || sum==1){
puts("-1"); return 0;
}
if(!tot){
puts("0"); return 0;
}
ll ans(inf);
for(LL k=1;k<=tot;++k){
ll x(bel[k]),nw(0),ret(0);
LL l(1),pre(0);
for(LL i=1;i<=n;++i){
b[i]=a[i]%x;
if(!b[i]) continue;
if(nw){
if(x-nw>b[i]){
if((x-nw)*(i-pre)>nw*(i-pre)) ret+=nw*(i-pre),pre=i;
else ret+=b[i]*(i-pre);
nw+=b[i];
}else{
ret+=(std::min(x-nw,nw))*(i-pre);
nw=b[i]-(x-nw);
if(nw) pre=i;
}
continue;
}
nw=b[i];
pre=i;
}
ans=std::min(ans,ret);
// printf("(%lld,%lld)\n",x,ret);
}
printf("%lld\n",ans);
return 0;
}

Codeforces1254B2 Send Boxes to Alice (Hard Version)(贪心)的更多相关文章

  1. E1.Send Boxes to Alice(Easy Version)//中位数

    发送盒子给Alice(简单版本) 题意:准备n个盒子放巧克力,从1到n编号,初始的时候,第i个盒子有ai个巧克力. Bob是一个聪明的家伙,他不会送n个空盒子给Alice,换句话说,每个盒子里面都有巧 ...

  2. Codeforces Round #601 (Div. 2) E2. Send Boxes to Alice (Hard Version)

    Codeforces Round #601 (Div. 2) E2. Send Boxes to Alice (Hard Version) N个盒子,每个盒子有a[i]块巧克力,每次操作可以将盒子中的 ...

  3. 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 ...

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

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

  5. E1. Send Boxes to Alice (Easy Version)

    题解: 保存每个1的位置.然后记录1的总个数cnt,如果存在一个k使得这个k是每个集合的倍数,那么为了使操作次数最小,这个k应该是cnt的质因子.(因为都是每个集合的数目1,使每个集合的数目变为2需要 ...

  6. Send Boxes to Alice

    E. Send Boxes to Alice 首先求出每一个位置的前缀和. 对答案进行复杂度为\(\sqrt{a[n]}\)的遍历,因为最后的答案不可能大于\(\sqrt{a[n]}\) for(ll ...

  7. Codeforces 1255E Send Boxes to Alice(前缀和+枚举+数论)

    我们考虑前缀和sum[i],如果将a[i+1]中的一个塞入a[i]中,则不影响sum[i+1],但是sum[i]++,如果将a[i]中的一个塞入a[i+1],则不影响sum[i+1],但是sum[i] ...

  8. Alice and Bob(贪心HDU 4268)

    Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. HDU 4268 Alice and Bob 贪心STL O(nlogn)

    B - Alice and Bob Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u D ...

随机推荐

  1. Apache配置WebSocket代理

    引入mod_proxy_wstunnel.so模块,使其可以解析ws.wss协议 LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel ...

  2. golang中uint8字节切片转字符串

    假如拿到了一个字节切片test1 = {'a', 'b', 'c', 'd', 11} package main import ( "fmt" "reflect" ...

  3. Django:RestFramework之-------序列化器

    8.序列化 功能: 对请求数据进行验证 对Queryset进行序列化 8.1一个简单序列化: import json from api import models from rest_framewor ...

  4. JDK安全证书的一个错误消息 No subject alternative names present的解决办法

    我使用Java消费某网站一个Restful API时,遇到这个错误: 21:31:16.383 [main] DEBUG org.springframework.web.client.RestTemp ...

  5. 下载文件时-修改文件名字 Redis在Windows中安装方法 SVN安装和使用(简单版) WinForm-SQL查询避免UI卡死 Asp.Net MVC Https设置

    下载文件时-修改文件名字   1后台代码 /// <summary> /// 文件下载2 /// </summary> /// <param name="Fil ...

  6. Jquery。

    Jquery: 1.概念:JavaScript的框架.本质上就是一些JS文件,封装了JS的原生代码而已. 2.快速入门:下载Jquery -导入文件-使用. 3.JQ对象和JS对象的区别. * :Jq ...

  7. HBase的部署与其它相关组件(Hive和Phoenix)的集成

    HBase的部署与其它相关组件(Hive和Phoenix)的集成 一.HBase部署 1.1.Zookeeper正常部署 首先保证Zookeeper集群的正常部署,并启动之: /opt/module/ ...

  8. Java面向对象 练习(类、对象、方法)

    知识点:构造方法.继承.方法重载.方法重写 一. 定义一个点(Point)类,用来表示三维空间中的点(有三个坐标),要求如下: 1.可以生成具有特定坐标的点对象(构造方法): 2.提供可以设置三个坐标 ...

  9. Django 之 cookie & session

    Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不会直接影响后面 ...

  10. 基于python3环境使用bandersnatch搭建本地pypi源

     pip3 install bandersnatch # https://pypi.org/project/bandersnatch/#files 查询最新版本 wget https://files. ...