zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    tcl/tk字符串操作【一】
    tcl/t字符串操作【二】
    [转]VS2010 语法错误: 标识符“__RPC__out_xcount_part” 解决方法
    [转]_MSC_VER
    从FireFox中抓取当前网页内容
    [转]vs2008设置边界线
    [转]使用SetUnhandledExceptionFilter让程序优雅的崩溃
    [转]10种提高自由职业者工作效率的方式
    通过HWND获得IHTMLDocument2
    mfcs90d.lib(xxx.obj):error LNK2005:_DllMain@12 already defined in MSVCRTD.lib(xxx.obj)
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6930225.html
Copyright © 2011-2022 走看看