• C. Vladik and Memorable Trip DP


    C. Vladik and Memorable Trip
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Vladik often travels by trains. He remembered some of his trips especially well and I would like to tell you about one of these trips:

    Vladik is at initial train station, and now n people (including Vladik) want to get on the train. They are already lined up in some order, and for each of them the city code ai is known (the code of the city in which they are going to).

    Train chief selects some number of disjoint segments of the original sequence of people (covering entire sequence by segments is not necessary). People who are in the same segment will be in the same train carriage. The segments are selected in such way that if at least one person travels to the city x, then all people who are going to city x should be in the same railway carriage. This means that they can’t belong to different segments. Note, that all people who travel to the city x, either go to it and in the same railway carriage, or do not go anywhere at all.

    Comfort of a train trip with people on segment from position l to position r is equal to XOR of all distinct codes of cities for people on the segment from position l to position rXOR operation also known as exclusive OR.

    Total comfort of a train trip is equal to sum of comfort for each segment.

    Help Vladik to know maximal possible total comfort.

    Input

    First line contains single integer n (1 ≤ n ≤ 5000) — number of people.

    Second line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 5000), where ai denotes code of the city to which i-th person is going.

    Output

    The output should contain a single integer — maximal possible total comfort.

    Examples
    input
    6
    4 4 2 5 2 3
    output
    14
    input
    9
    5 1 3 1 5 2 4 2 5
    output
    9
    Note

    In the first test case best partition into segments is: [4, 4] [2, 5, 2] [3], answer is calculated as follows: 4 + (2 xor5) + 3 = 4 + 7 + 3 = 14

    In the second test case best partition into segments is: [3] [2, 4, 2] 5, answer calculated as follows: 3 + (2 xor 4) = 3 + 6 = 9.

     一开始妄图使用记忆化搜索! n 10^3 的时候基本就不是回溯法

    从前到后 由前面的状态更新后面的(刷表法)

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<sstream>
    #include<algorithm>
    #include<queue>
    #include<deque>
    #include<iomanip>
    #include<vector>
    #include<cmath>
    #include<map>
    #include<stack>
    #include<set>
    #include<fstream>
    #include<memory>
    #include<list>
    #include<string>
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    #define MAXN 5005
    #define MOD 1000000
    #define INF 1000000009
    #define eps 0.00000001
    using namespace std;
    
    /*
    dp[i]表示元素a[i]之前的最大comfort
    */
    int dp[MAXN], l[MAXN], r[MAXN], a[MAXN], n;
    bool been[MAXN];
    int main()
    {
        scanf("%d", &n);
        memset(l, INF, sizeof(l));
        memset(r, -INF, sizeof(r));
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &a[i]);
            l[a[i]] = min(i, l[a[i]]);
            r[a[i]] = max(i, r[a[i]]);
        }
        for (int i = 0; i <= n; i++)
            dp[i] = -INF;
        dp[0] = 0;
        for (int i = 0; i < n; i++)
            if (dp[i] != -INF)
            {
                dp[i + 1] = max(dp[i + 1], dp[i]);
                int L = i, R = i, sum = 0;
                memset(been, false, sizeof(been));
                for (int j = i; j <= R; ++j)
                {
                    L = min(L, l[a[j]]);
                    R = max(R, r[a[j]]);
                    if (!been[a[j]])
                    {
                        sum ^= a[j];
                        been[a[j]] = true;
                    }
                }
                if (L == i)
                    dp[R + 1] = max(dp[R + 1], dp[i] + sum);
            }
        printf("%d
    ", dp[n]);
        return 0;
    }
  • 相关阅读:
    [转]CTO谈豆瓣网和校内网技术架构变迁
    Hashtable Dictionary[必看]
    DotFuscator 小记
    博客园随笔添加自己的版权信息 [转]
    [转]关于支付宝API开发的一点心得
    .NET下实现分布式缓存系统Memcached
    4.9 利用对应的泛型替换Hashtable[转]
    dllhost.exe 解释
    C#命名规范,SqlServer命名规范
    用XenoCode 2006 加密dll(.NET
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6930225.html
Copyright © 2020-2023  润新知