bzoj 1045 [HAOI2008] 糖果传递——设变量推式子
题目: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 ;
}
为了计算出每个人传递出去的糖果数量,需要先把它们设出来。
不妨指定一个方向。
然后尽量把每个变量用常量表示出来。
希望自己以后也能想出来。
#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] 糖果传递——设变量推式子的更多相关文章
- BZOJ 1045: [HAOI2008] 糖果传递 数学
1045: [HAOI2008] 糖果传递 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1045 Description 有n个小朋友坐 ...
- bzoj 1045: [HAOI2008] 糖果传递 贪心
1045: [HAOI2008] 糖果传递 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1812 Solved: 846[Submit][Stat ...
- [BZOJ 1045] [HAOI2008] 糖果传递
题目链接:BZOJ 1045 Attention:数据范围中 n <= 10^5 ,实际数据范围比这要大,将数组开到 10^6 就没有问题了. 我们先来看一下下面的这个问题. 若 n 个人坐成一 ...
- bzoj 1045 [HAOI2008] 糖果传递 —— 贪心
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1045 好像是贪心...但这是一个环... 看博客:http://hzwer.com/2656 ...
- BZOJ 1045 [HAOI2008]糖果传递 ★(环形等分:中位数)
题意 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. 思路 假设平均数是x,且a1给an了k个(k<0说明是an给a1了-k个),那么总代价就 ...
- bzoj 1045: [HAOI2008] 糖果传递【瞎搞】
感觉我的智商可能不够写题解,就直接截了hzwer的blog 地址http://hzwer.com/2656.html #include<iostream> #include<cstd ...
- 【BZOJ 1045】 1045: [HAOI2008] 糖果传递
1045: [HAOI2008] 糖果传递 Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. Input 第一行一个正整数n& ...
- 1045: [HAOI2008] 糖果传递 - BZOJ
Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1.Input 小朋友个数n 下面n行 aiOutput 求使所有人获得均等糖果的 ...
- 【BZOJ】1045: [HAOI2008]糖果传递(中位数)
http://www.lydsy.com/JudgeOnline/problem.php?id=1045 白书上有讲 没ac的坑点在,数据范围n<=1,000,000 #include < ...
随机推荐
- JDK源码阅读--HashMap
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, ...
- 解析Request和Response
简介: Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象. request和response对象即然代表请求和响应 ...
- windows 遍历目录下的所有文件 FindFirstFile FindNextFile
Windows下遍历文件时用到的就是FindFirstFile 和FindNextFile 首先看一下定义: HANDLE FindFirstFile( LPCTSTR lpFileName, // ...
- #pragma omp parallel for
#pragma omp parallel for是OpenMP中的一个指令,表示接下来的for循环将被多线程执行,另外每次循环之间不能有关系.示例如下: int main(int argc, char ...
- std::unique_lock与std::lock_guard区别示例
std::lock_guard std::lock_guard<std::mutex> lk(frame_mutex); std::unique_lock<std::mutex> ...
- Django WSGI响应过程之WSGIHandler
class WSGIHandler(base.BaseHandler): request_class = WSGIRequest def __init__(self, *args, **kwargs) ...
- vue.js_12_vue的watch和computed
1.watch用来监测指定Vue实例上的数据变动. watch主要用于监控vue实例的变化,它监控的变量当然必须在data里面声明才可以,它可以监控一个变量,也可以是一个对象. 1.>使用wat ...
- 什么情况下要加上【javascript:】
你知道http:// https:// mailto: tencent://这种东西么?这叫url schema,通常是在a的href里的.但a的href里面是不能加脚本的,所以浏览器就创造了一个叫j ...
- JS中的一些函数式编程术语
组合 Composition 组合某种类型(含函数)的两个元素,进而生成一个该类型的新元素: JavaScript 1 2 3 4 5 6 7 let compose = (f ...
- 《DSP using MATLAB》Problem 7.33
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...