Editorial for Nước đọng


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của flashmt

#include<iostream>
#include<algorithm>
#define fr(a,b,c) for (a=b;a<=c;a++)
#define frr(a,b,c) for (a=b;a>=c;a--)
#define maxn 100100
using namespace std;

int n,a[maxn],l[maxn],r[maxn];
long long re=0;

int main()
{
    int i,x;
    cin >> n;
    fr(i,1,n) scanf("%d",&a[i]);
    fr(i,2,n) l[i]=max(l[i-1],a[i-1]);
    frr(i,n-1,1) r[i]=max(r[i+1],a[i+1]);
    fr(i,1,n)
    {
       x=min(l[i],r[i]);
       if (x>a[i]) re+=x-a[i];
    }
    cout << re << endl;
    return 0;
}

Code mẫu của happyboy99x

#include<cstdio>
#include<algorithm>
#include<stack>
using namespace std;

#define N 100005

int h[N], hl[N], hr[N], n;

int main() {
    scanf("%d",&n); for(int i = 0; i < n;++i) scanf("%d",h+i);
    stack<int> st;
    int mx = 0;
    for(int i=0;i<n;++i) {
        mx = max(mx, h[i]);
        hl[i] = mx;
        /*while(!st.empty() && st.top() <= h[i]) st.pop();
        hl[i] = st.empty() ? h[i] : st.top();
        st.push(h[i]);*/
    }
    //while(!st.empty()) st.pop();
    mx = 0;
    for(int i=n-1;i>=0;--i) {
        mx = max(mx, h[i]);
        hr[i] = mx;
        /*while(!st.empty() && st.top() <= h[i]) st.pop();
        hr[i] = st.empty() ? h[i] : st.top();
        st.push(h[i]);*/
    }
#ifndef ONLINE_JUDGE
    for(int i = 0; i < n; ++i) printf("%d ", hl[i]); putchar(10);
    for(int i = 0; i < n; ++i) printf("%d ", hr[i]); putchar(10);
#endif
    long long res = 0;
    for(int i = 0; i < n; ++i) res += min(hl[i],hr[i]) - h[i];
    printf("%lld\n", res);
    return 0;
}

Code mẫu của ladpro98

program v11water;
uses    math;
var     a,m1,m2:array[0..123456] of longint;
        n:longint;
        res:int64;

procedure input;
var     inp:text;
        i:longint;
begin
        assign(inp,'');
        reset(inp);
        readln(inp,n);
        for i:=1 to n do
        read(inp,a[i]);
        close(inp);
end;

procedure init;
var     i:longint;
begin
        m1[1]:=a[1];
        for i:=2 to n do m1[i]:=max(m1[i-1],a[i]);
        m2[n]:=a[n];
        for i:=n-1 downto 1 do m2[i]:=max(m2[i+1],a[i]);
end;

procedure calc;
var     i,m:longint;
begin
        for i:=2 to n-1 do
        begin
                m:=min(m1[i-1],m2[i+1]);
                if m>a[i] then
                res:=res+m-a[i];
        end;
end;

begin
        input;
        init;
        calc;
        write(res);
end.

Code mẫu của RR

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <deque>
#include <complex>

#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair
using namespace std;

const double PI = acos(-1.0);

int n, h[100111], r[100111], l[100111];

int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    scanf("%d", &n);
    FOR(i,1,n) scanf("%d", &h[i]);
    FOR(i,1,n) l[i] = max(l[i-1], h[i]);
    FORD(i,n,1) r[i] = max(r[i+1], h[i]);
    ll res = 0;
    FOR(i,1,n)
        res += min(l[i], r[i]) - h[i];
    cout << res;
    return 0;
}

Code mẫu của hieult

#include <stdio.h>
//#include <conio.h>

long long a[100001],n,maxl[100001],maxr[100001],max,maxi;
long long A[100001],KQ;

long long min(long long a, long long b)
{
     if(a<b) return a;
     return b;
}

int main()
{
   // freopen("V11WATER.in","r",stdin);
    scanf("%lld",&n);
    for(int i = 1;i<=n;i++)
        scanf("%lld",&a[i]);
    maxl[1] = 1;
    max = a[1];
    for(int i = 2;i<=n;i++)
    {
        if(a[i]>max)
        {
            maxl[i] = i;
            max = a[i];
        }
        else maxl[i] = maxl[i-1];
    }
    maxr[n] = n;
    max = a[n];
    for(int i = n-1;i>=1;i--)
    {
        if(a[i]>max)
        {
            maxr[i] = i;
            max = a[i];
        }
        else maxr[i] = maxr[i+1];
    }
    for(int i = 1;i<=n;i++)
    {
        if(a[i]<min(a[maxl[i]],a[maxr[i]]))
             KQ = KQ + min(a[maxl[i]],a[maxr[i]]) - a[i];
    }
    printf("%lld",KQ);
   // getch();
}

Comments

Please read the guidelines before commenting.


There are no comments at the moment.