CF 265B (simplified line tree Version)

Source: Internet
Author: User

B. roadside Trees (Simplified Edition) time limit per test 2 secondsmemory limit per test 256 megabytesinput standard inputoutput standard output has n Trees from west to east, numbered 1 to n, and the tree has nuts at the top. I tree height hi. liss wants to eat all nuts. liss is 1 in the height of the 1st trees. liss takes 1 s to do any of the following: Move 1 cell above or below the tree. eat nut on the top of the tree. jump to the east side of the tree (cannot jump to the west), the height remains unchanged, note that Liss cannot jump from the high to the low. Calculate the shortest time for Liss to eat all nuts. the first behavior of Input is n (1 rows less than or equal to limit less than n rows less than or equal to limit 105 ). the next n behavior sequence hi (1 Gbit/s ≤ Gbit/s hi Gbit/s ≤ Mbit/s 104 ). output calculates the shortest time for Liss to eat all nuts. sample test (s) input212output5input521211output14 note that you cannot jump to the west. (I thought it was okay at first. Check the question carefully !) [Cpp] # include <cstdio> # include <iostream> # include <cstdlib> # include <cstring> # include <cmath> # include <functional> # include <algorithm> # include <cctype> using namespace std; # define MAXN (100000 + 10) # define MAXHi (10000 + 10) int n, h [MAXN]; int main () {www.2cto.com cin> n; for (int I = 1; I <= n; I ++) cin> h [I]; h [0] = 1; int ans = 0; for (int I = 1; I <= n; I ++) ans + = abs (h [I]-h [I-1]); /* int hmin = h [n]; for (int I = n-1; I> = 1; I --) {ans = min (ans, ans-abs (h [I]-h [I-1])-abs (h [I + 1]-h [I]) + abs (h [I-1]-h [I + 1]) + n-I + abs (hmin-h [I]) + abs (hmin-h [n]);} */ans + = 2 * n; cout <ans <endl; return 0 ;}

Related Article

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.