UVA-11054 Wine Trading in Gergovia (Gergovia's liquor trading) (greedy + analog)

Source: Internet
Author: User

Test instructions: There are N (2<=n<=100000) equidistant villages in the line, and each village buys wine or sells liquor. The demand for wine in the first village is Ai ( -1000<=ai<=1000), of which ai>0 means to buy wine and ai<0 to sell alcohol. The balance of supply and demand in all villages, i.e. all Ai sum equals 0. Transporting K-units of wine from one village to the neighboring village requires a workforce of k units. Calculating at least how much labor is needed to meet the needs of all villages.

Analysis: From the left-hand village, whether he buys wine or sells alcohol, there is A0 traffic relative to his neighboring village, so the traffic is constantly accumulating or offsetting, counting to the right-most village.

#pragma COMMENT (linker, "/stack:102400000, 102400000") #include <cstdio> #include <cstring> #include < cstdlib> #include <cctype> #include <cmath> #include <iostream> #include <sstream> #include <iterator> #include <algorithm> #include <string> #include <vector> #include <set># include<map> #include <stack> #include <deque> #include <queue> #include <list> #define Min (A, B) ((a < b) a:b) #define MAX (A, B) ((a < b) b:a) typedef long Long ll;typedef unsigned long long Ull;co NST int int_inf = 0x3f3f3f3f;const int int_m_inf = 0x7f7f7f7f;const ll ll_inf = 0x3f3f3f3f3f3f3f3f;const ll LL_M_INF = 0x7 f7f7f7f7f7f7f7f;const int dr[] = {0, 0,-1, 1,-1,-1, 1, 1};const int dc[] = {-1, 1, 0, 0,-1, 1,-1, 1};const int MOD = 1e9 + 7;const Double pi = ACOs ( -1.0); const double EPS = 1e-8;const int maxn = 100000 + 10;const int maxt = 10000 + 10;usin    G namespace Std;int main () {int n;     while (scanf ("%d", &n) = = 1) {   if (!n) return 0;        LL x, ans = 0, last = 0;            for (int i = 0; i < n; ++i) {scanf ("%lld", &x);            Ans + = ABS (last);        Last + = x;    } printf ("%lld\n", ans); } return 0;}

  

UVA-11054 Wine Trading in Gergovia (Gergovia's liquor trading) (greedy + analog)

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.