// Problem: 负载平衡问题// Contest: AcWing// URL: https://www.acwing.com/problem/content/2196/// Memory Limit: 64 MB// Time Limit: 1000 ms// // Powered by CP Editor (https://cpeditor.org)// %%%Skyqwq#include<bits/stdc++.h>//#define int long long#define help {cin.tie(NULL); cout.tie(NULL);}#define pb push_back#define fi first#define se second#define mkp make_pairusingnamespace std;
typedeflonglong LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
template <typename T> boolchkMax(T &x, T y){ return (y > x) ? x = y, 1 : 0; }
template <typename T> boolchkMin(T &x, T y){ return (y < x) ? x = y, 1 : 0; }
template <typename T> voidinlineread(T &x){
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}
constint N=105,M=N*6,inf=1e9;
int n,a[N],S,T;
int h[N],e[M],w[M],f[M],ne[M],idx;
int incf[N],d[N],pre[N],q[N];
bool st[N];
voidadd(int a,int b,int c,int d){
e[idx]=b,f[idx]=c,w[idx]=d,ne[idx]=h[a],h[a]=idx++;
e[idx]=a,f[idx]=0,w[idx]=-d,ne[idx]=h[b],h[b]=idx++;
}
boolspfa(){
memset(d,0x3f,sizeof d);
memset(incf,0,sizeof incf);
d[S]=0,incf[S]=inf,q[0]=S;
int hh=0,tt=1;
while(hh!=tt)
{
int x=q[hh++];
if(hh==N)hh=0;
st[x]=false;
for(int i=h[x];~i;i=ne[i])
{
int y=e[i];
if(d[y]>d[x]+w[i]&&f[i])
{
d[y]=d[x]+w[i];
pre[y]=i;
incf[y]=min(incf[x],f[i]);
if(!st[y])
{
q[tt++]=y;
if(tt==N)tt=0;
st[y]=true;
}
}
}
}
return incf[T]>0;
}
intEK(){
int cost=0;
while(spfa())
{
int t=incf[T];
cost+=t*d[T];
for(int i=T;i!=S;i=e[pre[i]^1])f[pre[i]]-=t,f[pre[i]^1]+=t;
}
return cost;
}
intmain(){
memset(h,-1,sizeof h);
scanf("%d",&n);
int s=0;
S=0,T=n+1;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
s+=a[i];
}
s/=n;
for(int i=1;i<=n;i++)
{
if(a[i]>s)add(S,i,a[i]-s,0);
elseif(a[i]<s)add(i,T,s-a[i],0);
add(i,i<n?i+1:1,inf,1);
add(i,i>1?i-1:n,inf,1);
}
printf("%d",EK());
return0;
}
贪心
// Problem: 负载平衡问题// Contest: AcWing// URL: https://www.acwing.com/problem/content/2196/// Memory Limit: 64 MB// Time Limit: 1000 ms// // Powered by CP Editor (https://cpeditor.org)// %%%Skyqwq#include<bits/stdc++.h>//#define int long long#define help {cin.tie(NULL); cout.tie(NULL);}#define pb push_back#define fi first#define se second#define mkp make_pairusingnamespace std;
typedeflonglong LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
template <typename T> boolchkMax(T &x, T y){ return (y > x) ? x = y, 1 : 0; }
template <typename T> boolchkMin(T &x, T y){ return (y < x) ? x = y, 1 : 0; }
template <typename T> voidinlineread(T &x){
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}
constint N=105;
int n,a[N],b[N];
intmain(){
scanf("%d",&n);
int s=0,avg=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
s+=a[i];
}
avg=s/n;
for(int i=2;i<=n;i++)b[i]=b[i-1]+a[i]-avg;
nth_element(b+1,b+(n+1)/2,b+1+n);
int res=0;
for(int i=1;i<=n;i++)
res+=abs(b[i]-b[(n+1)/2]);
printf("%d",res);
return0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!