爱奇艺编程题:幸运子序列

题目:https://www.nowcoder.com/practice/872919272a33406a9c5ddc8b2f7532f4?tpId=90&tqId=30804&rp=2&ru=/ta/2018test&qru=/ta/2018test/question-ranking

暴力解法,遍历所有子区间,超时,80%通过率

#include

using namespace std;

int func(vector &num){

int n = num.size();

int ret = -1;

int a ,b;

for(auto i = 0;i

a = max(num[i],num[i+1]);

b = min(num[i],num[i+1]);

ret = max(ret,a^b);

for(auto j = i+2;j

if(num[j] > a){

b = a;

a = num[j];

}

else if(num[j] < b){

;

}

else{

b = num[j];

}

ret = max(ret,a ^ b);

}

}

return ret;

}

int main()

{

std::ios::sync_with_stdio(false);

std::cin.tie(0);

int n;

cin >> n;

vector num(n);

for(auto i = 0;i < n;++i)

cin >> num[i]; 

cout<

return 0;

}

AC的代码:

#include

using namespace std;

int func(vector &num){

int n = num.size();

int ret = -1;

for(auto i = 0;i

for(auto j = i-1;j>=0;--j){

if(num[j] > num[i]){

ret = max(num[i] ^ num[j],ret);

break;

}

}

for(auto j = i+1;j

if(num[j] > num[i]){

ret = max(num[i] ^ num[j],ret);

break;

}

}

}

return ret;

}

int main()

{

std::ios::sync_with_stdio(false);

std::cin.tie(0);

int n;

cin >> n;

vector num(n);

for(auto i = 0;i < n;++i)

cin >> num[i]; 

cout<

return 0;

}

你可能感兴趣的:(爱奇艺编程题:幸运子序列)