UVa 11054 Wine Trading in Gergovia

Source: Internet
Author: User

Test instructions

There are n equidistant wineries in a straight line, each winery's demand for wine is AI (a positive number indicates the need to buy wine, a negative number is required to sell alcohol), and the balance of supply and demand is ensured for all wineries.

Transporting X-units of wine to neighboring wineries requires X labor, which requires a minimum of labor to balance the supply and demand of all wineries.

Analysis:

The first winery buys A1 units of wine, which may be produced directly from a second winery or another winery to a second winery.

But in the end there must be A1 a labor force from the winery 2 to the winery 1.

As a result, the problem is translated into:

There are 2~n these wineries, the winery 2 demand for A1+A2, the demand for other wineries or AI. and has used the |a1| labor force, the need for a minimum of labor.

In fact, both positive and negative numbers correspond to this conversion.

So, we can read the side count on it.

Because of the large amount of data, so I Machine (Wei) wisdom (suo) to use an input hanging, sure enough a lot faster.

1#include <cstdio>2#include <cmath>3 4 Long LongScan () {//input Plug -in5     intres =0, flag =0;6     Charch;7     if(ch = getchar ()) = ='-') flag =1;8     Else if(Ch >='0'&& CH <='9') res = CH-'0';9      while(ch = getchar ()) >='0'&& CH <='9')Tenres = res *Ten+ (CH-'0'); One     returnFlag? -Res:res; A } -  - intMain () the { -     //freopen ("In.txt", "R", stdin); -  -     intN; +      while(SCANF ("%d", &n) = =1&&N) -     { + GetChar (); A         Long LongAns =0, last =0; at         intA; -          for(inti =0; I < n; ++i) -         { -A =Scan (); -Ans + =Std::abs (last); -Last + =A; in         } -  toprintf"%lld\n", ans); +     } -  the     return 0; *}
code June

UVa 11054 Wine Trading in Gergovia

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.