algoskipper's blog

By algoskipper, history, 23 hours ago, In English

Can anyone hep me with this question I can think of LIS but am not able to solve it

Code implement with LIS

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(n) for(ll i=0;i<n;i++)
int main(){
  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
#endif
ll t;
cin>>t;
while(t--){

    ll n;cin>>n;
    vector<pair<ll,ll>>a(n);
   f(n)cin>>a[i].first;
   f(n){cin>>a[i].second;}
   sort(a.begin(),a.end(),[&](pair<ll,ll>a,pair<ll,ll>b){
    if(a.first==b.first)return a.second>b.second;
    return a.first<b.first;
   });
   vector<ll>b;
   vector<pair<ll,ll>>lp;
   ll ans=1;
   lp.push_back({-1,-1});
   b.push_back(-1);
   for(ll i=0;i<n;i++){
if(lp[lp.size()-1].first==a[i].first)continue;
 auto it= lower_bound(b.begin(), b.end(), a[i].second);
        if (it == b.end()) {
            b.push_back(a[i].second);
            lp.push_back(a[i]);
        }
        else {
          lp[it-b.begin()]=a[i];
            b[it-b.begin()] = a[i].second;
        }
}    
       cout<<lp.size()-1<<endl;
}
}
  • Vote: I like it
  • +3
  • Vote: I do not like it

»
20 hours ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it

you could sort the boxes based on height and then find the LIS of the widths of the boxes since then all the boxes chosen will have height in sorted order as well as width in sorted order

like in the example , sort based on height

h : 6 10 10 12

w : 6 5 10 12

and now you could find the LIS of w in this case it will be 3 -> 6 10 12

  • »
    »
    16 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I tried it but its giving wrong answer Below is my code if yoy can find any testcase or logic error

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define f(n) for(ll i=0;i<n;i++)
    int main(){
      #ifndef ONLINE_JUDGE
      freopen("input.txt","r",stdin);
      freopen("output.txt","w",stdout);
    #endif
    ll t;
    cin>>t;
    while(t--){
    
        ll n;cin>>n;
        vector<pair<ll,ll>>a(n);
       f(n)cin>>a[i].first;
       f(n){cin>>a[i].second;}
       sort(a.begin(),a.end(),[&](pair<ll,ll>a,pair<ll,ll>b){
        if(a.first==b.first)return a.second>b.second;
        return a.first<b.first;
       });
       vector<ll>b;
       vector<pair<ll,ll>>lp;
       ll ans=1;
       lp.push_back({-1,-1});
       b.push_back(-1);
       for(ll i=0;i<n;i++){
    if(lp[lp.size()-1].first==a[i].first)continue;
     auto it= lower_bound(b.begin(), b.end(), a[i].second);
            if (it == b.end()) {
                b.push_back(a[i].second);
                lp.push_back(a[i]);
            }
            else {
              lp[it-b.begin()]=a[i];
                b[it-b.begin()] = a[i].second;
            }
    }    
           cout<<lp.size()-1<<endl;
    }
    }
    
    • »
      »
      »
      15 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      im a python user so cpp is a bit difficult for me ;-; , could you send me the question link , i will try solving it after todays contest.

    • »
      »
      »
      11 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Here is the code in python , i have found the LIS using segment tree.

      Spoiler

      This is my solution.

      The problem link — link.

      It is quite similar to the problem you showed, try submitting your answer here, it will show you what test case your answer failed for.

»
15 hours ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it