UVA-11054 (Scanning method)

Source: Internet
Author: User

Test instructions

N Equidistant villages, each village either buys wine or sells liquor, and the K-units of wine are transported to neighboring villages to require K-units of labor, asking at least how much labor is needed to meet the requirements of all villages;

Ideas:

Last time did a ring, this is a straight line, is a flood problem;

The first village of wine can only come from the second village, so the transfer of a[1] to a[2], now is the equivalent of a sub-problem;

AC Code:

#include <bits/stdc++.h>#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;#definefor (i,j,n) for (int i=j;i<=n;i++)#defineMST (SS,B) memset (ss,b,sizeof (ss));typedefLong Longll;template<classT>voidRead (t&num) {    CharCH;BOOLf=false;  for(Ch=getchar (); ch<'0'|| Ch>'9'; f= ch=='-', ch=GetChar ());  for(num=0; ch>='0'&&ch<='9'; num=num*Ten+ch-'0', ch=GetChar ()); F&& (num=-num);}intstk[ -], Tp;template<classT> Inlinevoidprint (T p) {if(!p) {Puts ("0");return; }  while(p) stk[++ TP] = p%Ten, p/=Ten;  while(TP) Putchar (stk[tp--] +'0'); Putchar ('\ n');}ConstLL mod=1e9+7;Const DoublePi=acos (-1.0);Const intinf=1e9;Const intn=12e4+Ten;Const intmaxn=1e3+Ten;Const Doubleeps=1e-Ten;intN,a[n];intMain () { while(1) {read (n); if(!n) Break; For (I,1, N) read (a[i]); LL ans=0; For (I,1, N) {ans=ans+ABS (A[i]); A[i+1]=a[i+1]+A[i]; } cout<<ans<<Endl; }                return 0;}

UVA-11054 (Scanning method)

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.