• UVa 11054 Wine trading in Gergovia


    题意:

    直线上有n个等距的酒庄,每个酒庄对酒的需求量为ai(正数说明需要买酒,负数需要卖酒),而且保证所有的酒庄供需平衡。

    搬运x个单位的酒到相邻的酒庄需要x个劳动力,求要使所有酒庄供需平衡最少需要多少劳动力。

    分析:

    第一个酒庄要买a1个单位的酒,那么这些酒可能是直接由第二个酒庄生产的,也可能是其他酒庄运到第二个酒庄的。

    但最终一定有a1个 劳动力把酒从酒庄2运到酒庄1.

    于是,问题就转化为:

    有2~n这些酒庄,酒庄2的需求量为a1+a2,其他酒庄的需求量还是ai。且已经使用了|a1|个劳动力,求还需最少劳动力。

    其实,无论正数负数都符合这个转化。

    于是,我们边读边算就可以了。

    因为数据量比较大,于是我机(wei)智(suo)地用了个输入挂,果然快了很多。

     1 #include <cstdio>
     2 #include <cmath>
     3 
     4 long long Scan() {    //输入外挂
     5     int res = 0, flag = 0;
     6     char ch;
     7     if((ch = getchar()) == '-') flag = 1;
     8     else if(ch >= '0' && ch <= '9') res = ch - '0';
     9     while((ch = getchar()) >= '0' && ch <= '9')
    10         res = res * 10 + (ch - '0');
    11     return flag ? -res : res;
    12 }
    13 
    14 int main()
    15 {
    16     //freopen("in.txt", "r", stdin);
    17 
    18     int n;
    19     while(scanf("%d", &n) == 1 && n)
    20     {
    21         getchar();
    22         long long ans = 0, last = 0;
    23         int a;
    24         for(int i = 0; i < n; ++i)
    25         {
    26             a = Scan();
    27             ans += std::abs(last);
    28             last += a;
    29         }
    30 
    31         printf("%lld
    ", ans);
    32     }
    33 
    34     return 0;
    35 }
    代码君
  • 相关阅读:
    NOIP 2017 游记?
    bzoj4596 [Shoi2016]黑暗前的幻想乡
    bzoj2467 [中山市选2010]生成树
    bzoj3534 [Sdoi2014]重建
    bzoj1016 [JSOI2008]最小生成树计数
    bzoj4044 [Cerc2014] Virus synthesis
    bzoj4530 [Bjoi2014]大融合
    bzoj2594 [Wc2006]水管局长数据加强版
    bzoj 2342 [Shoi2011]双倍回文
    bzoj [HNOI2008]Cards
  • 原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/4276682.html
Copyright © 2020-2023  润新知