RainX_69

Count K increasing subsequences

Feb 4th, 2023 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.26 KB | Source Code | 0 0
  1. https://www.pepcoding.com/resources/data-structures-and-algorithms-in-java-interview-prep/segment-tree/k-increasing-subsequence2-official/ojquestion
  2.  
  3. For the given sequence A with n elements find the number of strictly increasing subsequences with k elements.
  4.  
  5. Note: This question is same as K Increasing Subsequence, but this time array can have numbers from 1 to 10^9 and can have duplicates.
  6.  
  7. Input Format
  8. First line contains two integer n and k
  9. following n lines contains elements of sequence
  10. A[1]
  11. A[2]
  12. ....A[n]
  13.  
  14. Output Format
  15. Print one number the answer to question
  16.  
  17. Constraints
  18. 1. 1 <= n <= 10^5
  19. 2. 1 <= k <= 11
  20. 3. 1 <= A[i] <= 10^9
  21. 5. A can contain duplicates
  22. 6. Output may not fit in 32 bit signed integer
  23.  
  24. Sample Input
  25. 5 2
  26. 1
  27. 1
  28. 3
  29. 8
  30. 2
  31.  
  32. Sample Output
  33. 7
  34.  
  35. -------------------------------------------------------------------------------------------------------------------------------------
  36.  
  37. /*
  38. #include<bits/stdc++.h>
  39. using namespace std;
  40.  
  41. int main(){
  42.     int K,n;
  43.     cin>>n>>K;
  44.     vector<long long> arr(n);
  45.     for(int i=0;i<n;i++){
  46.         cin>>arr[i];
  47.     }
  48.     vector<long long> temp=arr;
  49.     sort(temp.begin(),temp.end());
  50.     unordered_map<long long,long long> mpp;
  51.     long long m=1;
  52.     for(int i=0;i<n;i++){
  53.         if(mpp.find(temp[i])==mpp.end()){
  54.             mpp[temp[i]]=m;
  55.             m++;
  56.         }
  57.     }
  58.     for(int i=0;i<n;i++){
  59.         arr[i]=mpp[arr[i]];
  60.     }
  61.    
  62.     long long dp[n][K+1];
  63.     memset(dp,0,sizeof(dp));
  64.     for(int i=0;i<n;i++){
  65.         dp[i][1]=1;
  66.     }
  67.     for(int end=1;end<n;end++){
  68.         for(int start=0;start<end;start++){
  69.             if(arr[end]>arr[start]){
  70.                 for(int k=2;k<=K;k++){
  71.                     dp[end][k]+=dp[start][k-1];
  72.                 }
  73.             }
  74.         }
  75.     }
  76.     long long res=0;
  77.     for(int i=0;i<n;i++){
  78.         res+=dp[i][K];
  79.     }
  80.     cout<<res;  
  81.     return 0;
  82. }*/ // TLE O(n*n*k)
  83.  
  84.  
  85.  
  86. #include<bits/stdc++.h>
  87. using namespace std;
  88.  
  89. vector<vector<long long>> tree;
  90.  
  91. vector<long long> summation(vector<long long> &left, vector<long long> &right, int k){
  92.     vector<long long> res(k+1,0);
  93.     for(int i=1;i<=k;i++){
  94.         res[i]=left[i]+right[i];
  95.     }
  96.     return res;
  97. }
  98.  
  99. vector<long long> query(int start, int end, int parent, int qstart, int qend, int k){
  100.     if(end<qstart || qend<start){
  101.         return vector<long long>(k+1,0);
  102.     }
  103.     if(qstart<=start && qend>=end){
  104.         return tree[parent];
  105.     }
  106.     int mid=(start+end)/2;
  107.     auto left=query(start,mid,2*parent+1,qstart,qend,k);
  108.     auto right=query(mid+1,end,2*parent+2,qstart,qend,k);
  109.     return summation(left,right,k);
  110. }
  111.  
  112. void update(int start, int end, int parent, int index, vector<long long> &updateThis, int k){
  113.     if(index<start || index>end){
  114.         return;
  115.     }
  116.     if(start==end){
  117.         tree[parent]=summation(updateThis,tree[parent],k);
  118.         return;
  119.     }
  120.     int mid=(start+end)/2;
  121.     if(index>mid){
  122.         update(mid+1,end,2*parent+2,index,updateThis,k);
  123.     }
  124.     else{
  125.         update(start,mid,2*parent+1,index,updateThis,k);
  126.     }
  127.     auto left=tree[2*parent+1];
  128.     auto right=tree[2*parent+2];
  129.     tree[parent]=summation(left,right,k);
  130.     return;
  131. }
  132.  
  133. int main(){
  134.     int k,n;
  135.     cin>>n>>k;
  136.     vector<int> arr(n);
  137.     for(int i=0;i<n;i++){
  138.         cin>>arr[i];
  139.     }
  140.     vector<int> temp=arr;
  141.     sort(temp.begin(),temp.end());
  142.     unordered_map<int,int> mpp;
  143.     int m=1;
  144.     for(int i=0;i<n;i++){
  145.         if(mpp.find(temp[i])==mpp.end()){
  146.             mpp[temp[i]]=m;
  147.             m++;
  148.         }
  149.     }
  150.     for(int i=0;i<n;i++){
  151.         arr[i]=mpp[arr[i]];
  152.     }
  153.     tree.resize(4*m+1,vector<long long>(k+1,0));
  154.     for(int i=0;i<n;i++){
  155.         vector<long long> Karray(k+1,0);
  156.         Karray[1]=1;
  157.         if(arr[i]!=1){ // because if arr[i]==1, we would not find arr[i]-1 that is 0 anywhere cuz every                           elemnent in array has value > 0
  158.             vector<long long> sum=query(1,m,0,1,arr[i]-1,k); /* find the max value in range
  159.                                                                 1 to arr[i]-1 */
  160.             for(int j=2;j<=k;j++){  
  161.                 Karray[j]=sum[j-1];
  162.             }
  163.         }
  164.         update(1,m,0,arr[i],Karray,k);
  165.     }
  166.     cout<<tree[0][k];
  167.     return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment