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 ...
随机推荐
- winform+CefSharp 实现和js交互
1:窗体加载的时候添加 webBrowser.RegisterJsObject("getuserName", new _Event()); 2:注册C#方法为js方法 /// // ...
- 《C++ Primer》学习总结;兼论如何使用'书'这种帮助性资料
6.25~ 6.27,用了3天翻了一遍<C++ Primer>. ▶书的 固有坏处 一句话: 代码比 文字描述 好看多了.————> 直接看习题部分/ 看demo就行了 看文字在描述 ...
- logback配置和使用
简介 logback是由log4j创始人设计的又一个开源日志组件.当前分成三个模块: logback-core是其它两个模块的基础模块. logback-classic是log4j的一个 改良版本.此 ...
- javaScript 一些小技巧
日历 创建过去七天的数组,如果将代码中的减号换成加号,你将得到未来7天的数组集合 // 创建过去七天的数组 [...Array(7).keys()].map(days => new Date(D ...
- css3中的calc的使用
最近在布局的时候遇到一个问题,在页面中的左侧是侧边栏,右边是内容区域,内容区域中有一个固定定位的标签页,在设置固定定位的标签设置宽度的时候应该是内容区域的宽度,而固定定位的时候相对于是窗口的宽度,所以 ...
- 英语muttonfatjade羊脂玉muttonfatjade单词
羊脂玉英文(mutton fat jade) 中文名羊脂玉 外文名muttonfatjade 羊脂玉又称白玉,为软玉中之上品,极为珍贵.主要含有透闪石(95%).阳起石和绿帘石.非常洁白,质地细腻,光 ...
- The Vertu of the Dyamaund钻石
dyamaund and the English words dyamaund The Vertu of the Dyamaund": Gemstones, Knowledge and Va ...
- MySQL 如何优化大分页查询?
一 背景 大部分开发和DBA同行都对分页查询非常非常了解,看帖子翻页需要分页查询,搜索商品也需要分页查询.那么问题来了,遇到上千万或者上亿的数据量怎么快速的拉取全量,比如大商家拉取每月千万级别的订单数 ...
- Java集合学习(4):HashTable
一.概述 和HashMap一样,Hashtable也是一个散列表,它存储的内容是键值对. Hashtable在Java中的定义为: public class Hashtable<K,V> ...
- sqlserver一次性修改多条
修改客户表 编号为 0101007002,0101007003的楼栋号 007-1-102,007-1-201 UPDATE gas_customerSET building= CASEWHEN g ...