题目: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. 表单控件绑定v-model

    <!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...

  2. PAT甲级——A1074 Reversing Linked List

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  3. NTP时钟同步学习记录

    --1 要点回顾 . 1. NTP唯一配置文件:/etc/ntp.conf . 2. NTP系统日志记录:/var/log/ntp . 3. ntp.conf简要介绍 - 利用 restrict 来管 ...

  4. mysql limit 偏移量过大效率解决方式 转贴

    原文地址:https://www.jianshu.com/p/f8d81df7ab28 SELECT * FROM product , ) limit SELECT * FROM product a ...

  5. <每日一题>题目8:文件备份V1.0

    import os #备份文件的路径 file_address = input("输入需要备份文件所在的路径:") os.chdir(file_address) #备份文件命名 f ...

  6. 01-从这里开始js

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. CSS 的overflow:hidden (清除浮动)

    verflow:hidden这个CSS样式是大家常用到的CSS样式,但是大多数人对这个样式的理解仅仅局限于隐藏溢出,而对于清除浮动这个含义不是很 了解.一提到清除浮动,我们就会想到另外一个CSS样式: ...

  8. occ+vtk显示igs模型

    使用Opencascade读取igs文件内模型,使用vtk进行显示. 本案例环境:Opencascade6.6.0 +  vtk-5.10 + VS2005(win32) 使用CMake管理工程. C ...

  9. 不小心使用vcpkg之后再使用conan,一直报链接错误

    原来是使用vcpkg的时候,不小心使用了.\vcpkg integrate install命令,把vcpkg到所有的vs项目(这个不需要什么其他的引用,但是容易起冲突) 然后卸载掉就好了,这篇文章真是 ...

  10. Unity 用代码设置UGUI的渲染层级

    用代码设置UGUI渲染无非和三个API有关: 1.SetAsFirstSibling(); 2.SetAsLastSibling(); 3.SetSiblingIndex(n) SetAsFirstS ...