i want u to act as the most skilled competetive programmer, u take time to think and…
- User
i want u to act as the most skilled competetive programmer, u take time to think and check if ur solution works and match up with the example tescase, u need to think thoroughly since u r in a contest. use need to use this template, make sure u have lowercase short variable names and NO COMMENTS (in the code), use cpp: #include <bits/stdc++.h> #define int long long using namespace std;
signed main() { cin.tie(0); ios_base::sync_with_stdio(false);
}
problem statement You are given an array a1,a2,…,an of positive integers.
You can color some elements of the array red, but there cannot be two adjacent red elements (i.e., for 1≤i≤n−1 , at least one of ai and ai+1 must not be red).
Your score is the maximum value of a red element plus the number of red elements. Find the maximum score you can get.
Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤500 ). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤100 ) — the length of the array.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤1000 ) — the given array.
Output For each test case, output a single integer: the maximum possible score you can get after coloring some elements red according to the statement.
Example InputCopy 4 3 5 4 5 3 4 5 4 10 3 3 3 3 4 1 2 3 4 5 9 17 89 92 42 29 92 14 70 45 OutputCopy 7 6 10 97 Note In the first test case, you can color the array as follows: [5,4,5] . Your score is max([5,5])+size([5,5])=5+2=7 . This is the maximum score you can get.
In the second test case, you can color the array as follows: [4,5,4] . Your score is max([4,4])+size([4,4])=4+2=6 . This is the maximum score you can get.
In the third test case, you can color the array as follows: [3,3,3,3,4,1,2,3,4,5] . Your score is max([3,3,4,3,5])+size([3,3,4,3,5])=5+5=10 . This is the maximum score you can get.
- Assistant
#include <bits/stdc++.h> #define int long long using namespace std; signed main() { cin.tie(0); ios_base::sync_with_stdio(false); int t; cin>>t; while(t--){ int n; cin>>n; vector<int>a(n); for(auto &x:a) cin>>x; int res=0; vector<int>unique_a; // get unique elements sorted descending vector<int>temp = a; sort(temp.begin(), temp.end(), greater<int>()); temp.erase(unique(temp.begin(), temp.end()), temp.end()); unique_a = temp; for(auto m:unique_a){ // DP // dp[i][prev][has_m] // initialize vector<vector<vector<int>>>dp(n+1, vector<vector<int>>(2, vector<int>(2, -1))); dp[0][0][0]=0; for(int i=0;i<n;i++){ for(int prev=0; prev<=1; prev++){ for(int has_m=0; has_m<=1; has_m++){ if(dp[i][prev][has_m]==-1) continue; // not take dp[i+1][0][has_m] = max(dp[i+1][0][has_m], dp[i][prev][has_m]); // take if(prev==0){ int nm = has_m; if(a[i]==m) nm =1; dp[i+1][1][nm] = max(dp[i+1][1][nm], dp[i][prev][has_m]+1); } } } } int s=0; for(int prev=0; prev<=1; prev++) s = max(s, dp[n][prev][1]); res = max(res, m + s); } cout<<res<<"\n"; } }