Uva10366

模拟乱搞

要说算法的话...乱搞算法?

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
using namespace std;

const int N=1005;
int l,r,x1[N],x2[N];
int L,R,idl,idr;

void init() 
{
    R=L=0;
    for (int i=l;i<=r;i += 2)
    {
        if (i<0)
        {
            scanf("%d", &x1[(-i)/2]);
            if (L <= x1[(-i)/2]) {
                L = x1[(-i)/2]; idl = (-i)/2;
            }
        } else {
            scanf("%d", &x2[i/2]);
            if (R < x2[i/2]) {
                R = x2[i/2]; idr = i/2;
            }
        }
    }
}

int del(int a, int b) {
    if (a <= b) return 2 * a;
    else return a + b;
}

int solve() {
    l = (-l) / 2; r = r / 2;

    if (R == L) {
        int k = 0, t = 0;
        int tmp = x1[l];
        for (int i = l; i > idl; i--) {
            k += tmp; tmp = max(tmp, x1[i-1]);
        }
        tmp = x2[r];
        for (int i = r; i > idr; i--) {
            t += tmp; tmp = max(tmp, x2[i-1]);
        }

        return (idl + idr + 1) * R * 2 + min(k, t) * 2 * 2;
    } else {
        int T = min(R, L);
        int p = 0, q = 0, k = 0, t = 0;
        while (p < l && x1[p] < T) p++;
        while (q < r && x2[q] < T) q++;

        if (R > L) {
            int tmp = x2[q];
            for (int i = q; x2[i] <= L; i++) {
                k += tmp; tmp = max(tmp, x2[i+1]);
            }
            tmp = x1[l];
            for (int i = l; i > p; i--) {
                t += tmp; tmp = max(tmp, x1[i-1]);
            }

        } else {
            int tmp = x1[p];
            for (int i = p; x1[i] <= R; i++) {
                k += tmp; tmp = max(tmp, x1[i+1]);
            }
            tmp = x2[r];
            for (int i = r; i > q; i--) {
                t += tmp; tmp = max(tmp, x2[i-1]);
            }
        }
        int ans = t > k ? t + k : 2 * t;
        return ans * 2 + (p + q + 1) * T * 2;
    }
}

int main()
{
    while (scanf("%d%d", &l, &r) == 2 && l && r)
    {
        init();
        printf("%d\n", solve());
    }
    return 0;
}
View Code

 

posted @ 2017-10-02 15:19  探险家Mr.H  阅读(282)  评论(0编辑  收藏  举报