zoukankan      html  css  js  c++  java
  • [网络流24题] 负载平衡

    741. [网络流24题] 负载平衡

    ★★   输入文件:overload.in   输出文件:overload.out   简单对比
    时间限制:1 s   内存限制:128 MB


    «问题描述:
    G 公司有n 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等。如何用最
    少搬运量可以使n 个仓库的库存数量相同。搬运货物时,只能在相邻的仓库之间搬运。
    «编程任务:
    对于给定的n 个环形排列的仓库的库存量,编程计算使n 个仓库的库存数量相同的最少
    搬运量。
    «数据输入:
    由文件overload.in提供输入数据。文件的第1 行中有1 个正整数n(n<=100),表示有n
    个仓库。第2 行中有n个正整数,表示n个仓库的库存量。
    «结果输出:
    程序运行结束时,将计算出的最少搬运量输出到文件overload.out中。
    输入文件示例 输出文件示例
    overload.in
    5

    17 9 14 16 4

    overload.out

    11
    /*显示代码纯文本
    1. 提供最小费用流思路:
    2. 相邻的仓库之间连双向边,容量无穷大,费用为1。
    3. 如果仓库i中的货物小于平均值,则从i向汇点连边(表示i需要接收货物),容量为average - A[i]。
    4. 如果仓库i中的货物大于平均值,则从源点向i连边(表示i可以输出货物),容量为A[i] - average。
    5.  
    6. 本代码思路:套用DP(平衡序列)
    7. */
    8. #include<cstdio>
    9. #include<algorithm>
    10. const int N=205;
    11. int n,tot,a[N],b[N];
    12. int main(){
    13. freopen("overload.in","r",stdin);
    14. freopen("overload.out","w",stdout);
    15. scanf("%d",&n);
    16. for(int i=1;i<=n;i++) scanf("%d",&a[i]),tot+=a[i];
    17. tot/=n;
    18. for(int i=1;i<=n;i++){
    19. b[i]=tot-a[i];
    20. b[i]+=b[i-1];
    21. }
    22. std::sort(b+1,b+n+1);
    23. int ans=0;
    24. for(int i=1;i<=n;i++) ans+=abs(b[n+1>>1]-b[i]);
    25. printf("%d",ans);
    26. return 0;
    27. }
  • 相关阅读:
    Leetcode Plus One
    Leetcode Swap Nodes in Pairs
    Leetcode Remove Nth Node From End of List
    leetcode Remove Duplicates from Sorted Array
    leetcode Remove Element
    leetcode Container With Most Water
    leetcode String to Integer (atoi)
    leetcode Palindrome Number
    leetcode Roman to Integer
    leetcode ZigZag Conversion
  • 原文地址:https://www.cnblogs.com/shenben/p/6539152.html
Copyright © 2011-2022 走看看