fork(3) download
  1. /*
  2.  *
  3.  ********************************************************************************************
  4.  * AUTHOR : Vijju123 *
  5.  * Language: C++14 *
  6.  * Purpose: - *
  7.  * IDE used: Codechef IDE. *
  8.  ********************************************************************************************
  9.  *
  10.  Comments will be included in practice problems if it helps ^^
  11.  */
  12.  
  13.  
  14.  
  15. #include <iostream>
  16. #include<bits/stdc++.h>
  17. using namespace std;
  18.  
  19. int main() {
  20. // your code goes here
  21. #ifdef JUDGE
  22. freopen("input.txt", "rt", stdin);
  23. freopen("output.txt", "wt", stdout);
  24. #endif
  25. ios_base::sync_with_stdio(0);
  26. cin.tie(NULL);
  27. cout.tie(NULL);
  28. int t;
  29. cin>>t;
  30. printf("%dt\n",t);
  31. while(t--)
  32. {
  33. int n;
  34. printf("\n");
  35. cin>>n;
  36. printf("%dn\n",n);
  37. int a[n],b[n],ans=0;
  38. int i,j;
  39. //Input
  40. for(i=0;i<n;i++)
  41. {
  42. cin>>a[i];
  43. }
  44. for(i=0;i<n;i++)
  45. {
  46. cin>>b[i];
  47. }
  48. printf("Input -");
  49. printf("\na : ");
  50. for(i=0;i<n;i++)
  51. {
  52. printf("%d ",a[i]);
  53. }
  54. printf("\nb : ");
  55. for(i=0;i<n;i++)
  56. {
  57. printf("%d ",b[i]);
  58. }
  59. printf("\n");
  60. deque<int> q;
  61. deque<int> :: iterator it;
  62. int possible=1;
  63. //Check for case of -1 first.
  64. for(i=0;i<n;i++)
  65. {
  66. if(a[i]<b[i])
  67. {
  68. possible=0;
  69. printf("\nImpossible\n%di %dai %dbi\n",i,a[i],b[i]);
  70. break;
  71. }
  72. }
  73. if(possible==0)printf("ans : %d\n",-1);
  74. else
  75. {
  76. printf("\n");
  77. for(i=0;i<n;i++)
  78. {
  79. if(q.empty())
  80. {
  81. printf("%di %dai %dbi qEmpty\n",i,a[i],b[i]);
  82. }
  83. else
  84. {
  85. printf("%di %dqFront %dai %dbi %dqBack\n",i,q.front(),a[i],b[i],q.back());
  86. }
  87. while(!q.empty() and q.back()<b[i])
  88. {
  89. printf("Queue popBack %d\n",q.back());
  90. q.pop_back();
  91. }
  92. while(!q.empty() and q.front()>a[i])
  93. {
  94. printf("Queue popFront %d\n",q.front());
  95. q.pop_front();
  96. }
  97. printf("Queue After pop/push : ");
  98. for(it = q.begin();it!=q.end();++it)
  99. {
  100. printf("%d ",*it);
  101. }
  102. printf("\n");
  103. if( a[i]!=b[i] and (q.empty() or b[i]!=q.back()) )
  104. {
  105. ans++;
  106. printf("%d ans++\n",ans);
  107. printf("Queue pushBack %d\n",b[i]);
  108. q.push_back(b[i]);
  109. }
  110. printf("Queue After complete of %di : ",i);
  111. for(it = q.begin();it!=q.end();++it)
  112. {
  113. printf("%d ",*it);
  114. }
  115. printf("\n\n");
  116. }
  117. printf("ans : %d\n",ans);
  118. }
  119. }
  120.  
  121. return 0;
  122. }
Success #stdin #stdout 0s 4332KB
stdin
4
3
3 1 3
2 1 2
7
1 3 4 5 1 2 3
1 2 1 2 1 1 1
3
2 3 9
2 3 9
2
1 2
2 1
stdout
4t

3n
Input -
a : 3 1 3 
b : 2 1 2 

0i 3ai 2bi qEmpty
Queue After pop/push : 
1 ans++
Queue pushBack 2
Queue After complete of 0i : 2 

1i 2qFront 1ai 1bi 2qBack
Queue popFront 2
Queue After pop/push : 
Queue After complete of 1i : 

2i 3ai 2bi qEmpty
Queue After pop/push : 
2 ans++
Queue pushBack 2
Queue After complete of 2i : 2 

ans : 2

7n
Input -
a : 1 3 4 5 1 2 3 
b : 1 2 1 2 1 1 1 

0i 1ai 1bi qEmpty
Queue After pop/push : 
Queue After complete of 0i : 

1i 3ai 2bi qEmpty
Queue After pop/push : 
1 ans++
Queue pushBack 2
Queue After complete of 1i : 2 

2i 2qFront 4ai 1bi 2qBack
Queue After pop/push : 2 
2 ans++
Queue pushBack 1
Queue After complete of 2i : 2 1 

3i 2qFront 5ai 2bi 1qBack
Queue popBack 1
Queue After pop/push : 2 
Queue After complete of 3i : 2 

4i 2qFront 1ai 1bi 2qBack
Queue popFront 2
Queue After pop/push : 
Queue After complete of 4i : 

5i 2ai 1bi qEmpty
Queue After pop/push : 
3 ans++
Queue pushBack 1
Queue After complete of 5i : 1 

6i 1qFront 3ai 1bi 1qBack
Queue After pop/push : 1 
Queue After complete of 6i : 1 

ans : 3

3n
Input -
a : 2 3 9 
b : 2 3 9 

0i 2ai 2bi qEmpty
Queue After pop/push : 
Queue After complete of 0i : 

1i 3ai 3bi qEmpty
Queue After pop/push : 
Queue After complete of 1i : 

2i 9ai 9bi qEmpty
Queue After pop/push : 
Queue After complete of 2i : 

ans : 0

2n
Input -
a : 1 2 
b : 2 1 

Impossible
0i 1ai 2bi
ans : -1