Code: Select all
Removed After AC
Moderator: Board moderators
Code: Select all
Removed After AC
Code: Select all
1
10
3
8
1
5
10
4
9
2
7
6
1. Compute LIS and LDS using DP O(n^2)RC's wrote:By the way, could you share your algorithm here ? Brute force will certainly get Time Limit Exceeded...
Better approach is O(nlogk).wahab wrote: 1. Compute LIS and LDS using DP O(n^2)
wahab wrote:1. Compute LIS and LDS using DP O(n^2)RC's wrote:By the way, could you share your algorithm here ? Brute force will certainly get Time Limit Exceeded...
2. loop and compute max(LIS+LDS-1) for all i
rest wrote:plz.. explain what do you mean by LIS or LDS ?
for the input aboveshiplu_1320 wrote:for the input given by rio above,
LIS[0]=4
LDS[0]=5
LIS ARRAY [0]=3; && LDS ARRAY[0]=2.how it possible?????wahab wrote:shiplu_1320 wrote:for the input given by rio above,
LIS[0]=4
LDS[0]=5
LIS[0]=4
LDS[0]=5
so LIS[0]+LDS[0]-1= 8
but answer should be 5.
for the input above
LIS ARRAY IS 3 2 3 2 1 2 1 2 1 1
LDS ARRAY IS 2 4 1 3 4 2 3 1 2 1
then the best value is at
LDS[1]+LIS[1] = 4 + 2 -1 = 5
Here LIS[1] means how much integers you can take in increasing order taking the value of a[1]saiful_sust wrote: LIS ARRAY [0]=3; && LDS ARRAY[0]=2.how it possible?????
CAN any expain it................
Code: Select all
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
int main(){
int cases, e;
scanf("%d",&cases);
for(e=0; e<cases; e++){
int n, i;
scanf("%d",&n);
int arr[n];
for(i=0; i<n; i++){
scanf("%d",&arr[i]);
}
int best = 0;
int k;
for(k=0; k<n; k++){
// Get longest decreasing sequence for bot
vector<int> top;
vector<int> bot;
int z;
for(z=k; z<n;z++){
if(z>k){
if(arr[z] < arr[k]){
bot.push_back(arr[z]);
} else {
top.push_back(arr[z]);
}
}
}
// Print stuff
//printf("Starting at %d\n",arr[k]);
int t;
//printf("Dec:\n");
//for(t=0; t<bot.size(); t++)
// printf("%d ",bot[t]);
// printf("\nInc:\n");
//for(t=0; t<top.size(); t++)
// printf("%d ",top[t]);
// printf("\n");
int bot_size = bot.size();
int bot_dp[bot_size];
int bot_maxi = 0;
int bot_maxlen = 0;
for(i=k; i<bot_size; i++){
bot_dp[i] = 1;
int j;
for(j=k; j<i; j++){
if(bot[j] > bot[i] && bot_dp[j] + 1 > bot_dp[i]){
bot_dp[i] = bot_dp[j] + 1;
}
if(bot_dp[i] > bot_maxlen){
bot_maxlen = bot_dp[i];
bot_maxi = i;
}
}
}
//printf("Longest for decreasing is %d\n", bot_maxlen);
// Get longest increasing sequence for inc
int top_size = top.size();
int top_dp[top_size];
int top_maxi = 0;
int top_maxlen = 0;
for(i=k; i<top_size; i++){
top_dp[i] = 1;
int j;
for(j=k; j<i; j++){
if(top[i] > top[j] && top_dp[j] + 1 > top_dp[i]){
top_dp[i] = top_dp[j] + 1;
}
if(top_dp[i] > top_maxlen){
top_maxlen = top_dp[i];
top_maxi = i;
}
}
}
//printf("Longest for increasing is %d\n", top_maxlen);
// Remeber to add the starting cart in the sequence
if(1+top_maxlen+bot_maxlen > best){
best = 1+top_maxlen+bot_maxlen;
}
}
printf("%d\n",best);
}
return 0;
}