UVA-1442 Cav

Source: Internet
Author: User

The main idea: a cave with a width of n (n<=10^6) fragments. The ground height in the section I is pi and the top height is si (0=<pi<si<=1000), which requires storing as much fuel (liquid) in the hole as possible, so that the position of the fuel does not touch the top, but can be approached infinitely.


Problem-Solving ideas: Set the fuel height of each fragment is L, the initial value is s[0] or s[n-1], if the current fragment of the ground height pi>l, then l=pi, the top height si<l, then l=si, each fragment of the L exist in the array. Sweep from right to left, then sweep from left to right, and then subtract the smaller l from the fragment, the fuel of the fragment, and all that is to be asked.


#include <cstdio> #include <algorithm>using namespace Std;int N, p[1000010], s[1000010], H[1000010];int main () {int z;scanf ("%d", &z), while (z--) {scanf ("%d", &n), for (int i = 0; i < n; i++) scanf ("%d", &p[i]); for (i NT i = 0; I < n; i++) scanf ("%d", &s[i]), int level = S[0];for (int i = 0; i < n; i++) {if (Level > S[i]) level = S[i];if (Level &L T P[i]) level = p[i];h[i] = level;} Level = S[n-1];int ans = 0;for (int i = n-1; I >= 0; i--) {if (Level > S[i]) level = S[i];if (Level < p[i]) Lev el = P[i];ans + = min (level, h[i])-p[i];} printf ("%d\n", ans);} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVA-1442 Cav

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.