fork download
  1. /*#include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5. int n,i,x,y;
  6. scanf("%d%d%d",&n,&x,&y);
  7. int a[n+1],b[n+1],who[n+1],howmuch[n+1];
  8. for(i=1;i<=n;i++){
  9. scanf("%d",&a[i]);
  10. }
  11. for(i=1;i<=n;i++){
  12. scanf("%d",&b[i]);
  13. }
  14. int one=0,two=0;
  15. vector<pair<int,pair<int ,int> > >vp;
  16. for(i=1;i<=n;i++){
  17. //~ if(a[i]>=b[i]){
  18. //~ who[i]=1;
  19. //~ one++;
  20. //~ howmuch[i]=a[i];
  21. //~ vp.push_back(make_pair(a[i],1));
  22. //~ }
  23. //~ else {
  24. //~ who[i]=2;
  25. //~ two++;
  26. //~ howmuch[i]=b[i];
  27. //~ vp.push_back(make_pair(b[i],2));
  28. //~ }
  29. vp.push_back(make_pair(abs(a[i]-b[i]),make_pair(a[i],b[i])));
  30. }
  31. int ans=0;
  32. sort(vp,vp+sizeof(vp));
  33. for(i=1;i<=n;i++){
  34. if(vp[i].second.first > vp[i].second.second && x>0){
  35. x--;
  36. ans+=vp[i].second.first;
  37. }
  38. else if(y>0){
  39. y--;
  40. ans+=vp[i].second.second;
  41. }
  42. }
  43. printf("%d",ans);
  44. return 0;
  45. }
  46. */
  47.  
  48.  
  49. #include <bits/stdc++.h>
  50. using namespace std;
  51. #define maxi 1001
  52. class Stack
  53. {
  54. public:
  55. int data[maxi];
  56. int top,length;
  57. Stack(int);
  58. //~Stack();
  59.  
  60. void push(int);
  61. int pop();
  62. int func();
  63. void display();
  64. void Swap(Stack &);
  65. bool isEmpty();
  66. bool isFull();
  67. };
  68.  
  69. Stack::Stack(int size)
  70. {
  71. top=-1;
  72. length=size;
  73. }
  74.  
  75. //Stack::~Stack(){
  76. // delete [] data;
  77. //}
  78.  
  79. void Stack::push(int elem)
  80. {
  81.  
  82. if(top==(length-1)) //If the top reaches to the maximum stack size
  83. {
  84. cout<<"\nCannot push "<<elem<<", Stack full"<<endl;
  85. return;
  86. }
  87. else
  88. {
  89. //for(int i=0;i<length;i++){
  90. //cin>>elem;
  91. top++;
  92. data[top]=elem;
  93. //}
  94. }
  95. }
  96. int Stack::pop()
  97. {
  98. if(top==-1)
  99. {
  100. cout<<"Stack empty!";
  101. return -1;
  102. }
  103. int ret=data[top];
  104. top--;
  105. return ret;
  106. }
  107.  
  108. void Stack::display()
  109. {
  110. for(int i = 0; i <= top; i++)
  111. cout<<data[i]<<" ";
  112. cout<<endl;
  113. }
  114.  
  115. bool Stack::isEmpty()
  116. {
  117. if(top==-1){return 1;}
  118. else return 0;
  119. }
  120. bool Stack::isFull()
  121. {
  122. if (top==length){return 1;}
  123. else return 0;
  124. }
  125.  
  126. void Stack ::Swap(Stack &x){
  127. int sz=x.length,mytop,mybottom;
  128. mytop=x.pop();
  129. int tmp[sz-1],i=0;
  130. while(!x.isEmpty()){
  131. mybottom=x.pop();
  132. tmp[i++]=mybottom;
  133. }
  134. Stack returnIt(sz);
  135. returnIt.push(mybottom);
  136. for(i=0;i<=sz-3;i++){
  137. returnIt.push(tmp[i]);
  138. }
  139. returnIt.push(mytop);
  140. x.length=sz;
  141. x.top=-1;
  142. while(!returnIt.isEmpty()){
  143. int tt=returnIt.pop();
  144. x.push(tt);
  145. }
  146. }
  147. int main()
  148. {
  149. int len;
  150. cout<<"Enter length of stack : ";
  151. cin>>len;
  152. Stack s1(len);
  153. for(int i=0;i<len;i++){
  154. int element;
  155. cout<<"Enter element to push into stack : ";
  156. cin>>element;
  157. s1.push(element);
  158. }
  159. //s1.push(1);
  160. //s1.push(2);
  161. //s1.push(3);
  162. //s1.push(4);
  163. //s1.push(5);
  164. //s1.push(6);
  165. //s1.push(7);
  166. //s1.push(8);
  167. //s1.push(9);
  168. cout<<"\nStack before swapping : ";
  169. s1.display();
  170. s1.Swap(s1);
  171. cout<<"\nStack after swapping : ";
  172. s1.display();
  173. }
  174.  
Success #stdin #stdout 0s 3420KB
stdin
4
1 2 3 4
stdout
Enter length of stack : Enter element to push into stack : Enter element to push into stack : Enter element to push into stack : Enter element to push into stack : 
Stack before swapping : 1 2 3 4 

Stack after swapping : 4 2 3 1