#include <bits/stdc++.h> #define ll long long #define pll pair<ll,ll> using namespace std; ll n, k; ll a[10000001]; ll dp[100001][101]; vector<ll> mp[1000001]; ll tknp( ll index, ll val ) { ll l=0; ll r=mp[index].size()-1; ll ans=1e18; while ( l <= r ) { ll m = ( l + r ) / 2; if ( mp[index][m] > val ) { ans = m; r = m - 1; } else { l = m +1; } } if (ans!=1e18) return mp[index][ans]; return 1e18; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); cin >> n >> k; for (int i=0;i<=n;i++) { for ( int len = 1; len <= k+1; len++ ) { dp[i][len] = 1e18; } } // n=rand()%(20-10+1) + 10; //k=rand()% (20-10+1) + 10; //cout << n <<" "<< k << endl; for ( int i = 1; i <= n; i++ ) { cin >> a[i]; // a[i]=rand() % (100-1 +1) + 1; dp[i-1][1] = i; dp[i][2]=1e18; dp[i-1][2]=1e18; mp[a[i]].push_back(i); } ll res = 1; for ( int i = n-2; i>=1; i--) { dp[i][2] = 1e18; ll tmp = tknp( a[i+1], i+1 ); dp[i][2] = min( dp[i+1][2], tmp ); if ( dp[i][2] != 1e18 ) res = 2; } for ( int len = 3; len <= k; len++ ) { for ( int i = n-len; i>=0; i-- ) { dp[i][len]=1e18; ll tmp = tknp( a[i+1], dp[i+1][len-2]); dp[i][len]=min(dp[i+1][len], tmp); if ( dp[i][len] != 1e18 ) res = len; } } cout << res; return 0; } //dadauThanhHieu //TUWR_team //Written_by_ZuY /* 18 19 35 1 70 25 79 59 63 65 6 46 82 28 62 92 96 43 28 37 18 */
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}