• hdu5248 序列变换


    百度之星的题。其实最简单的方法是二分答案,我竟然没想到,直接去想O(n)的去了,最后导致滚粗。。。

    题意就是给一个数列,要求把它处理成递增序列。

    首先我想到了O(n^2)的算法,然后再优化成O(n)过的。

    n^2的做法是,弄一个尾指针e,从后往前扫,一旦发现a[e-1]>=a[e],说明a[e]之后的所有数都需要加一个数tmp了。

    确定这个tmp的简单方法是直接看a[e-1]。比如1 3 4 2 10 2 3 5 8 9这个序列,当a[e]=2时,通过a[e-1]发现a[e]及之后的数都至少需要加上tmp=5

    而对于a[e]之前的数,a[0]必然减去tmp,之后一直到a[e-1]的每一个数都分情况讨论,确定是加上[-tmp,tmp]之间的哪一个数。比如上面这个例子,第一轮处理后的数列应该是

    -4 6 7 8 9 7 (8 10 13 14) 括号之后的数是不用实际算出来的,标在这里是方便理解。

    每一轮处理是O(n),最多有n轮,所以复杂度是O(n^2)

    在这个过程中可以发现存在许多冗余计算。

    换一个例子,对于10 9 8 7 6 5 4 3 2 1,a[e]=1时第一轮处理之后是9 10 9 8 7 6 5 4 3 2,第二轮处理之后是8 9 10 9 8 7 6 5 4 3,第三轮处理之后是7 8 9 10 9 8 7 6 5 4……

    发现前面也不用全都处理。

    于是再引入一个头指针s,同时,在确定tmp时,不仅看a[e-1],还看一看a[s],得到最大的tmp,例如10 9 8 7 6 5 4 3 2 1,当a[e]=1时,通过a[s]=10可以知道tmp至少是9,于是这一轮这后数列成了1 2 3 4 5 6 7 8 9 10。每一轮要么e前移一位,要么s后移一位,所以最多还是n轮。更进一步,对a[s+1]...a[e-1]的处理不必每一轮都进行,处理a[s]时,只需要看a[s+1],并标记,省掉这里O(n)的循环。于是整个算法就变成了O(n)了。

    当然,因为我做题的时候是在n^2算法的基础上改的,改一会儿就提交一次,还没等我完全改成O(n)的算法(没有去掉那个O(n)的处理过程),就已经过了。所以下面的代码仅供参考。

    /*
     * Author    : ben
     */
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <set>
    #include <map>
    #include <stack>
    #include <string>
    #include <vector>
    #include <deque>
    #include <list>
    #include <functional>
    #include <numeric>
    #include <cctype>
    using namespace std;
    typec get_int() {
        typec res = 0, ch;
        while (!((ch = getchar()) >= '0' && ch <= '9')) {
            if (ch == EOF)
                return -1;
        }
        res = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9')
            res = res * 10 + (ch - '0');
        return res;
    }
    const int MAXN = 100010;
    int data[MAXN];
    
    int main() {
        int T, N;
        T = get_int();
        for (int t = 1; t <= T; t++) {
            N = get_int();
            for (int i = 0; i < N; i++) {
                data[i] = get_int();
            }
            int ans = 0, tmp, last = N - 1, start = 0, tmp2;
            while (last > start) {
                if (data[last] - data[last - 1] > 0) {
                    last--;
                    continue;
                }
                tmp = ceil((data[last - 1] - data[last] + 1) / 2.0);
                tmp2 = ceil((data[start] - data[last] + last - start) / 2.0);
                tmp = tmp > tmp2 ? tmp : tmp2;
                ans += tmp;
                data[last] += tmp;
                data[start] -= tmp;
                for (int i = start + 1; i < last; i++) {
                    if (data[i] - tmp > data[i - 1]) {
                        data[i] -= tmp;
                    } else     if (data[i] + tmp > data[i - 1]) {
                        data[i] = data[i - 1] + 1;
                    } else {
                        data[i] += tmp;
                    }
                }
            }
            printf("Case #%d:
    %d
    ", t, ans);
        }
        return 0;
    }
  • 相关阅读:
    JQuery 绑定单击事件到某个函数的的方法
    一刻校园足球管理平台亮相安徽打造智慧足球解决方案
    jQuery 获取不到 kindeditor 内容 的解决方法
    【视频】 ASP.NET MVC5&微信公众平台整合开发实战
    Set authorization for a whole area
    生成Area URL链接
    在ASP.NET MVC5应用程序中快速接入QQ和新浪微博OAuth
    IOS 监听通讯录是否改变
    ios 读取通讯录数据
    iOS中assign、copy 、retain等关键字的含义
  • 原文地址:https://www.cnblogs.com/moonbay/p/4540993.html
Copyright © 2020-2023  润新知