题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1045

费用流TLE。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define ll long long
using namespace std;
const int N=1e6+;
const ll INF=1e15;
int n,a[N],hd[N],xnt=,pre[N],dis[N];
ll ans,base;
bool vis[N];
struct Ed{
int nxt,to,w;
ll cap;
Ed(int n=,int t=,ll c=,int w=):nxt(n),to(t),cap(c),w(w) {}
}ed[N<<];
int rdn()
{
int ret=;char ch=getchar();
while(ch>''||ch<'')ch=getchar();
while(ch>=''&&ch<='')(ret*=)+=ch-'',ch=getchar();
return ret;
}
void add(int x,int y,ll cap)
{
int k=;if(!x||y==n+)k=;
ed[++xnt]=Ed(hd[x],y,cap,k);hd[x]=xnt;
ed[++xnt]=Ed(hd[y],x,,-k);hd[y]=xnt;
}
queue<int> q;
bool spfa()
{
q.push();vis[]=;
memset(dis,0x3f,sizeof dis);dis[]=;
while(q.size())
{
int k=q.front();q.pop();vis[k]=;
for(int i=hd[k],v;i;i=ed[i].nxt)
if(ed[i].cap&&dis[v=ed[i].to]>dis[k]+ed[i].w)
{
dis[v]=dis[k]+ed[i].w;
pre[v]=i;
if(!vis[v])vis[v]=,q.push(v);
}
}
return dis[n+]<0x3f3f3f3f;
}
void ek()
{
ll flow=INF;
for(int i=n+;i;i=ed[pre[i]^].to)
flow=min(flow,ed[pre[i]].cap);
for(int i=n+;i;i=ed[pre[i]^].to)
ed[pre[i]].cap-=flow,ed[pre[i]^].cap+=flow;
ans+=flow*dis[n+];
}
int main()
{
n=rdn();
for(int i=;i<=n;i++)a[i]=rdn(),base+=a[i];
base/=n;
for(int i=;i<=n;i++)
{
if(a[i]>base)add(,i,a[i]-base);
if(a[i]<base)add(i,n+,base-a[i]);
add(i,i-?i-:n,INF);add(i,i+<=n?i+:,INF);
}
while(spfa())ek();
printf("%lld\n",ans);
return ;
}

TJ:http://hzwer.com/2656.html

为了计算出每个人传递出去的糖果数量,需要先把它们设出来。

不妨指定一个方向。

然后尽量把每个变量用常量表示出来。

希望自己以后也能想出来。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int N=1e6+;
int n;
ll a[N],base,c[N],ans;
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lld",&a[i]),base+=a[i];
base/=n;
for(int i=;i<n;i++)c[i]=c[i-]+a[i]-base;
sort(c+,c+n);
ll x=c[(n+)>>];
for(int i=;i<n;i++)ans+=abs(x-c[i]);
printf("%lld\n",ans);
return ;
}

bzoj 1045 [HAOI2008] 糖果传递——设变量推式子的更多相关文章

  1. BZOJ 1045: [HAOI2008] 糖果传递 数学

    1045: [HAOI2008] 糖果传递 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1045 Description 有n个小朋友坐 ...

  2. bzoj 1045: [HAOI2008] 糖果传递 贪心

    1045: [HAOI2008] 糖果传递 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1812  Solved: 846[Submit][Stat ...

  3. [BZOJ 1045] [HAOI2008] 糖果传递

    题目链接:BZOJ 1045 Attention:数据范围中 n <= 10^5 ,实际数据范围比这要大,将数组开到 10^6 就没有问题了. 我们先来看一下下面的这个问题. 若 n 个人坐成一 ...

  4. bzoj 1045 [HAOI2008] 糖果传递 —— 贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1045 好像是贪心...但这是一个环... 看博客:http://hzwer.com/2656 ...

  5. BZOJ 1045 [HAOI2008]糖果传递 ★(环形等分:中位数)

    题意 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. 思路 假设平均数是x,且a1给an了k个(k<0说明是an给a1了-k个),那么总代价就 ...

  6. bzoj 1045: [HAOI2008] 糖果传递【瞎搞】

    感觉我的智商可能不够写题解,就直接截了hzwer的blog 地址http://hzwer.com/2656.html #include<iostream> #include<cstd ...

  7. 【BZOJ 1045】 1045: [HAOI2008] 糖果传递

    1045: [HAOI2008] 糖果传递 Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. Input 第一行一个正整数n& ...

  8. 1045: [HAOI2008] 糖果传递 - BZOJ

    Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1.Input 小朋友个数n 下面n行 aiOutput 求使所有人获得均等糖果的 ...

  9. 【BZOJ】1045: [HAOI2008]糖果传递(中位数)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1045 白书上有讲 没ac的坑点在,数据范围n<=1,000,000 #include < ...

随机推荐

  1. 微信开发SDK支持小程序 ,Jeewx-Api 1.3.1 版本发布

    JEEWX-API 是一款JAVA版的微信开发SDK,支持微信公众号.小程序.微信企业号.支付宝生活号SDK和微博SDK.你可以基于她 快速的傻瓜化的进行微信开发.支付窗开发和微博开发. 基于jeew ...

  2. rocketmq 延时消息

    rocketmq  的延时消息不能支持任意延时,她定义了18 个延时等级,并且我们可以指定这18 个延时等级的延时时间. 发送消息的时候只需在消息中指定 当前消息的 延时等级即可,并且这个延时消息不是 ...

  3. PAT甲级——A1007 Maximum Subsequence Sum

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  4. elasticsearch 中文API river

    river-jdbc 安装 ./bin/plugin --install jdbc --url http://xbib.org/repository/org/xbib/elasticsearch/pl ...

  5. Codeforces 938G 线段树分治 线性基 可撤销并查集

    Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...

  6. 廖雪峰Java10加密与安全-3摘要算法-3SHA-1算法

    1.SHA-1算法 SHA-1算法也是一种哈希算法. 输出160 bits/20bytes 由美国国家安全局开发 SHA-0/SHA-1/SHA-256/SHA-512 * SHA-0有问题,已经作废 ...

  7. string主要操作函数

    参考博客:http://blog.csdn.net/zhenyusoso/article/details/7286456 std::string illegal(" \t\f\v\n\r\\ ...

  8. 洛谷P3749 [六省联考2017]寿司餐厅

    传送门 题解 这几道都是上周llj讲的题,题解也写得十分好了,所以直接贴了几个链接和代码. //Achen #include<algorithm> #include<iostream ...

  9. PHP--自动回调接口,分批修改数据

    /** * 修复 a表 生日格式问题 * @author qin */ public function update_birthday_one() { $this->load->model ...

  10. mysql利用MySQLWorkbench生成数据表之间的关系图

    先看结果,默认是展开的,我手动把表折叠了 那么如何实现呢 先点击这里 然后通过向导来创建即可,一直到finish就行了