Uva10366

Posted 探险家Mr.H

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于Uva10366的主要内容,如果未能解决你的问题,请参考以下文章

UVa 1442 - Cave

CSS jQuery 顺序问题

UVa10410代码

UVa 1593代码对齐

UVa 1593 代码对齐

算法习题---5.1代码对齐(UVa1593)