Advertisement
flash_7

LISS

Mar 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.47 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define pb push_back
  5. #define nl puts ("")
  6. #define sp printf ( " " )
  7. #define phl printf ( "hello\n" )
  8. #define ff first
  9. #define ss second
  10. #define POPCOUNT __builtin_popcountll
  11. #define RIGHTMOST __builtin_ctzll
  12. #define LEFTMOST(x) (63-__builtin_clzll((x)))
  13. #define MP make_pair
  14. #define FOR(i,x,y) for(vlong i = (x) ; i <= (y) ; ++i)
  15. #define ROF(i,x,y) for(vlong i = (y) ; i >= (x) ; --i)
  16. #define CLR(x,y) memset(x,y,sizeof(x))
  17. #define UNIQUE(V) (V).erase(unique((V).begin(),(V).end()),(V).end())
  18. #define MIN(a,b) ((a)<(b)?(a):(b))
  19. #define MAX(a,b) ((a)>(b)?(a):(b))
  20. #define NUMDIGIT(x,y) (((vlong)(log10((x))/log10((y))))+1)
  21. #define SQ(x) ((x)*(x))
  22. #define ABS(x) ((x)<0?-(x):(x))
  23. #define FABS(x) ((x)+eps<0?-(x):(x))
  24. #define ALL(x) (x).begin(),(x).end()
  25. #define LCM(x,y) (((x)/gcd((x),(y)))*(y))
  26. #define SZ(x) ((vlong)(x).size())
  27. #define NORM(x) if(x>=mod) x-=mod;if(x<0) x+=mod;
  28. #define MOD(x,y) (((x)*(y))%mod)
  29. #define ODD(x) (((x)&1)==0?(0):(1))
  30. #define Set(N,cur) N=(N|(1<<cur))
  31. #define Reset(N,cur) N=(N&(~(1<<cur)))
  32. #define Check(N,cur) (!((N&(1<<cur))==0))
  33. #define dbgA(A,i) debug("At pos: ",i," = ",A[i])
  34. #define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)
  35.  
  36. #ifdef forthright48
  37.      #include <ctime>
  38.      clock_t tStart = clock();
  39.      #define debug(args...) {dbg,args; cerr<<endl;}
  40.      #define timeStamp debug ("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)
  41.      #define bug printf("%d\n",__LINE__);
  42.  
  43. #else
  44.      #define debug(args...)  // Just strip off all debug tokens
  45.      #define timeStamp
  46. #endif
  47.  
  48. struct debugger{
  49.     template<typename T> debugger& operator , (const T& v){
  50.         cerr<<v<<" ";
  51.         return *this;
  52.     }
  53. }dbg;
  54.  
  55. #define LL long long
  56. #define LLU long long unsigned int
  57. typedef long long vlong;
  58. typedef unsigned long long uvlong;
  59. typedef pair < int, int > pii;
  60. typedef pair < vlong, vlong > pll;
  61.  
  62. inline vlong gcd ( vlong a, vlong b ) {
  63.     a = ABS ( a ); b = ABS ( b );
  64.     while ( b ) { a = a % b; swap ( a, b ); } return a;
  65. }
  66.  
  67. vlong ext_gcd ( vlong A, vlong B, vlong *X, vlong *Y ){
  68.     vlong x2, y2, x1, y1, x, y, r2, r1, q, r;
  69.     x2 = 1; y2 = 0; x1 = 0; y1 = 1;
  70.     for (r2 = A, r1 = B; r1 != 0; r2 = r1, r1 = r, x2 = x1, y2 = y1, x1 = x, y1 = y ) {
  71.         q = r2 / r1; r = r2 % r1;
  72.         x = x2 - (q * x1); y = y2 - (q * y1);
  73.     }
  74.     *X = x2; *Y = y2;
  75.     return r2;
  76. }
  77.  
  78. inline vlong modInv ( vlong a, vlong m ) {
  79.     vlong x, y;
  80.     ext_gcd( a, m, &x, &y );
  81.     x %= m;
  82.     if ( x < 0 ) x += m;
  83.     return x;
  84. }
  85.  
  86. inline vlong power ( vlong a, vlong p ) {
  87.     vlong res = 1, x = a;
  88.     while ( p ) {
  89.         if ( p & 1 ) res = ( res * x );
  90.         x = ( x * x ); p >>= 1;
  91.     }
  92.     return res;
  93. }
  94.  
  95. inline vlong bigmod ( vlong a, vlong p, vlong m ) {
  96.     vlong res = 1 % m, x = a % m;
  97.     while ( p ) {
  98.         if ( p & 1 ) res = ( res * x ) % m;
  99.         x = ( x * x ) % m; p >>= 1;
  100.     }
  101.     return res;
  102. }
  103.  
  104. inline int STRLEN(char *s){
  105.     int p = 0; while(s[p]) p++; return p;
  106. }
  107.  
  108. inline LL SQRT(LL a){
  109.     LL v = sqrt(a);
  110.     if(SQ(v) == a) return v;
  111.     else if(SQ(v+1) == a) return v+1;
  112.     else if(SQ(v+2) == a) return v+2;
  113.     else return -1; /// a can no be written as p^2
  114. }
  115.  
  116. //int knightDir[8][2] = { {-2,1},{-1,2},{1,2},{2,1},{2,-1},{-1,-2},{1,-2},{-2,-1} };
  117. //int dir4[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
  118. //int dir8[8][2] = {{-1,0},{0,1},{1,0},{0,-1},{-1,-1},{1,1},{1,-1},{-1,1}};
  119.  
  120. const int inf = 100000000;
  121. const double pi = 2 * acos ( 0.0 );
  122. const double eps = 1e-9;
  123.  
  124. ///=========================  TEMPLATE ENDS HERE  ========================///
  125. ///=======================================================================///
  126.  
  127. const int Size = 5005;
  128.  
  129. int N,M;
  130. int A1[Size];
  131. int A2[Size];
  132. int DP[Size]; /// DP[j] = LISS till j in A2 (i in A1 can be anywhere).
  133. vector<pii> POS[Size]; /// POS[j] store all possible i such that A1[i] = A2[j] and LISS till then.
  134. vector<int> res;
  135. int ans = 0;
  136.  
  137. void printLISS(){
  138.     if(ans == 0) return;
  139.     int prvI = inf;
  140.     int prvVal = inf;
  141.     for(int j = M-1;j>=0;j--){
  142.         if(A2[j] >= prvVal) continue;
  143.         for(int c = 0;c<POS[j].size();c++){
  144.             int i = POS[j][c].ff;
  145.             int mxCnt = POS[j][c].ss;
  146.             if(mxCnt == ans && i<prvI){
  147.                 ans--;
  148.                 prvI = i;
  149.                 prvVal = A2[j];
  150.                 res.pb(A2[j]);
  151.                 break;
  152.             }
  153.         }
  154.     }
  155.     reverse(ALL(res));
  156.     for(int i = 0;i<res.size();i++){
  157.         if(i != 0) printf(" ");
  158.         printf("%d",res[i]);
  159.     }
  160.     printf("\n");
  161. }
  162.  
  163. void LISS(){
  164.     for(int i = 0;i<N;i++){
  165.         int mxCnt = 0;
  166.         for(int j = 0;j<M;j++){
  167.             if(A1[i] == A2[j]){ /// 1 new match
  168.                 DP[j] = max(DP[j], mxCnt+1);
  169.                 ans = max(ans, mxCnt+1);
  170.                 POS[j].pb(MP(i, mxCnt+1));
  171.             }else if(A2[j] < A1[i]){
  172.                 /// As A2[j] is smaller now, so it's possible to increase j for fixed i
  173.                 mxCnt = max(mxCnt, DP[j]); /// Update best LISS till now.
  174.             }
  175.         }
  176.     }
  177.     printf("%d\n",ans);
  178.     printLISS();
  179. }
  180.  
  181. int main(){
  182.     #ifdef forthright48
  183.     freopen ( "input.txt", "r", stdin );
  184.     //freopen ( "output.txt", "w", stdout );
  185.     #endif // forthright48
  186.  
  187.     scanf("%d",&N);
  188.     for(int i = 0;i<N;i++){
  189.         scanf("%d",&A1[i]);
  190.     }
  191.  
  192.     scanf("%d",&M);
  193.     for(int i = 0;i<M;i++){
  194.         scanf("%d",&A2[i]);
  195.     }
  196.     LISS();
  197.  
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement