fork(2) download
  1. #include <vector>
  2. #include <list>
  3. #include <map>
  4. #include <set>
  5. #include <queue>
  6. #include <deque>
  7. #include <stack>
  8. #include <bitset>
  9. #include <algorithm>
  10. #include <functional>
  11. #include <numeric>
  12. #include <utility>
  13. #include <sstream>
  14. #include <iostream>
  15. #include <iomanip>
  16. #include <cstdio>
  17. #include <cmath>
  18. #include <cstdlib>
  19. #include <ctime>
  20. #include <fstream>
  21.  
  22. #define sz(X) ((int)X.size())
  23. #define FOR(i,x,y) for(int i=x; i<y; ++i)
  24.  
  25. using namespace std;
  26. int main()
  27. {
  28. #ifndef ONLINE_JUDGE
  29. fstream cin ("input.txt");
  30. #endif
  31.  
  32. int T; cin>>T;
  33. cin.ignore();
  34. cin.ignore();
  35.  
  36. FOR(t,0,T)
  37. {
  38. if(t)
  39. cout<<endl;
  40.  
  41. vector<int> V;
  42. string ch;
  43. int a;
  44.  
  45. while (getline(cin, ch) && ch.length() > 0)
  46. {
  47. a = atoi(ch.c_str());
  48. V.push_back(a);
  49. }
  50.  
  51. //LIS algorithm
  52. int longest, N = sz(V), end;
  53.  
  54. if(N) longest = 1, end = 0; //if some values are there in the input then atleast ans is 1 which is the first value
  55. else longest = 0, end = -1; //if there was a blank line in the input
  56.  
  57. vector<int> L(N,1), P(N,-1);
  58.  
  59. FOR(i,1,N)
  60. FOR(j,0,i)
  61. {
  62. if(V[j] < V[i]) //don't know if required ???
  63. {
  64. if(L[i] < L[j] + 1)
  65. {
  66. L[i] = L[j] + 1;
  67. P[i] = j; //previous index
  68. longest = max(longest, L[i]);
  69. end = i;
  70. }
  71. }
  72. }
  73.  
  74. printf("Max hits: %d\n", longest);
  75.  
  76. //print the values
  77. int e = end;
  78. stack<int> S;
  79. while (e != -1)
  80. {
  81. S.push(V[e]); e = P[e];
  82. }
  83.  
  84. while (!S.empty())
  85. {
  86. cout<<S.top()<<endl; S.pop();
  87. }
  88. }
  89. return 0;
  90. }
Success #stdin #stdout 0s 3436KB
stdin
1

935
323
314
59
484
455
939
783
567
159
176
633
327
755
236
375
743
804
916
192
771
547
311
966
370
876
100
584
337
617
571
623
940
236
33
775
43
972
557
961
130
84
593
808
838
181
535
932
984
450
124
107
996
786
424
717
661
524
300
350
492
222
972
431
810
5
557
204
328
113
164
809
548
108
968
386
288
502
317
624
951
792
82
298
577
505
15
590
380
666
939
872
240
262
654
49
266
563
252
593
675
767
753
223
874
721
960
514
222
628
489
525
420
570
822
348
74
188
289
806
854
227
29
445
489
682
493
106
244
96
51
271
214
803
845
439
523
156
952
97
783
440
621
554
9
794
254
435
334
542
592
187
121
620
983
609
653
475
66
249
922
116
871
135
271
67
573
793
222
877
241
356
316
213
910
677
7
163
463
340
56
54
878
176
25
860
136
677
stdout
Max hits: 21
33
43
84
107
113
164
288
317
380
420
445
489
493
523
554
592
620
653
677