UVALive-4621 Cav greedy + analysis

Source: Internet
Author: User

There is a map of the cave, to store water in this cave, requires water can not touch the top of the cave. Now give the top position and ground height of each position. How much water can I put up?

Problem-solving ideas: According to the physical theorem, each section has a continuous interval of water, the water level must be equal
So we can find the height of the water level in the same continuous interval, the height of which is equal to the height of the lowest cave top. Based on this, from left to right update, and then from right to left to update, you can get the height of each position of the water level

#include <cstdio>#include <algorithm>using namespace STD;Const intN =1000010;Const intINF =0x3f3f3f3f;intS[n], p[n], N;voidInit () {scanf("%d", &n); for(inti =0; I < n; i++) {scanf("%d", &p[i]); } for(inti =0; I < n; i++) {scanf("%d", &s[i]); }}intSolve () {intt = INF; for(inti =0; I < n;        i++) {t = min (t, s[i]);        t = max (T, P[i]);    S[i] = t; } t = INF; for(inti = n-1; I >=0;        i--) {t = min (t, s[i]);        t = max (T, P[i]);    S[i] = t; }intAns =0; for(inti =0; I < n; i++) ans + = s[i]-p[i];returnAns;}intMain () {intTestscanf("%d", &test); while(test--) {init ();printf("%d\n", solve ()); }return 0;}

UVALive-4621 Cav greedy + analysis

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.