Cod sursa(job #3146053)

Utilizator Luca07Nicolae Luca Luca07 Data 18 august 2023 13:18:32
Problema Xor Max Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <fstream>
#include<vector>
using namespace std;

ifstream cin("xormax.in");
ofstream cout("xormax.out");

struct Pos {
    int index, value;
};

vector<pair<Pos, Pos>> dp;
vector<int> v;

int main()
{
    int i, j, n, nr,v1,v2,k,maxx,st,end;

    cin >> n;
    for (i = 0; i < n; i++) {
        cin >> nr;
        v.push_back(nr);
    }

    dp.push_back({ { 0,v[0] }, { 0,v[0] } });

    maxx = v[0];
    st=end = 0;

    for (i = 1; i < n; i++) {
        pair<Pos, Pos> p;
        v1 = dp[i - 1].first.value ^ v[i];
        v2 = dp[i - 1].second.value ^ v[i];

        if (v1 > v2) {
            p.first.value = v1;
            p.first.index = dp[i - 1].first.index;
        }
        else {
            p.first.value = v2;
            p.first.index = dp[i - 1].second.index;
        }
        p.second.index = i;
        p.second.value = v[i];

        if (maxx < p.first.value) {
            maxx = p.first.value;
            st = p.first.index;
            end = i;
            if (maxx == p.second.value) {
                st = p.second.index;
            }
        }
        
        if (maxx < p.second.value) {
            maxx = p.second.value;
            st = p.second.index;
            end = i;
        }

        dp.push_back(p);
    }

    cout << maxx << " " << st+1<< " " << end+1;

    return 0;
}