Codeforces1254B2 Send Boxes to Alice (Hard Version)(贪心)
题意
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)(贪心)的更多相关文章
- E1.Send Boxes to Alice(Easy Version)//中位数
发送盒子给Alice(简单版本) 题意:准备n个盒子放巧克力,从1到n编号,初始的时候,第i个盒子有ai个巧克力. Bob是一个聪明的家伙,他不会送n个空盒子给Alice,换句话说,每个盒子里面都有巧 ...
- 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]块巧克力,每次操作可以将盒子中的 ...
- 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 ...
- E2. Send Boxes to Alice (Hard Version)
秒的有点难以理解:https://blog.csdn.net/weixin_42868863/article/details/103200132 #include<bits/stdc++.h&g ...
- E1. Send Boxes to Alice (Easy Version)
题解: 保存每个1的位置.然后记录1的总个数cnt,如果存在一个k使得这个k是每个集合的倍数,那么为了使操作次数最小,这个k应该是cnt的质因子.(因为都是每个集合的数目1,使每个集合的数目变为2需要 ...
- Send Boxes to Alice
E. Send Boxes to Alice 首先求出每一个位置的前缀和. 对答案进行复杂度为\(\sqrt{a[n]}\)的遍历,因为最后的答案不可能大于\(\sqrt{a[n]}\) for(ll ...
- 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] ...
- Alice and Bob(贪心HDU 4268)
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 4268 Alice and Bob 贪心STL O(nlogn)
B - Alice and Bob Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
随机推荐
- APS.NET MVC + EF (08)---数据注解和验证
对于Web开发人员来说,用户输入验证一直是一个挑战.不仅在客户端浏览器中需要执行验证逻辑,在服务器端也需要执行.如果觉得验证是令人望而生畏的繁杂琐事,ASP.NET MVC框架提供了数据注解的方式帮助 ...
- docker run VS docker exec 的区别
“docker run”和“docker exec”都是 Docker 容器中用于执行的命令.然而,在不同的情况下,它们的使用有着本质上的区别. “docker run”命令 “docker run” ...
- if控制器,如何传参。
- MTSC2019-深圳站 议题征集
议题截止时间 11月初 议题投递地址 topic@testerhome.com 臣一路走来,没有敌人,看见的都是朋友和师长 —司马懿 关于中国移动互联网测试大会 MTSC 大会(中国移动互联网测试 ...
- js学习之面向对象
一.创建对象的方法 1. {} 字面量创建 var person ={ name: "lisi", age: , say: function(){ alert(this.name) ...
- [转]C++类成员修饰const和mutable
const:常量,不变的 mutable:易变的 从意思上理解,可见const和mutable是一对反义词,它们都是C++的关键字. const成员函数不能修改调用它的对象.类的成员函数可以被声明为c ...
- ZooKeeper基本介绍
一.入门 1.1 概述 Zookeeper是一个开源的分布式的,为分布式应用提供协调的Apache项目.可用于服务发现,分布式锁,分布式领导选举,配置管理等. Zookeeper从设计模式角度来理解: ...
- 连接mysql报"ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this MySQL server"
最近在服务器上部署好的应用突然间连接不上mysql数据库,报错“ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this M ...
- 分页查询——Hibernate Criteria实现一次查询取得总记录数和分页后结果集
使用Hibernate criteria进行分页查询时,如何实现一次查询取得总记录数和分页后结果集 - bto310 - ITeye博客 https://bto310.iteye.com/blog/1 ...
- 自定义View(三),仿支付宝芝麻信用自定义控件
仿支付宝的芝麻信用仪表盘 实现的效果 实现的功能: 指针和数字动态改变 背景动态变化 没了... 代码如下 MyCustomView.java package com.example.testcust ...