Advertisement
Nickpips

Untitled

Jan 4th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. ifstream in;
  10. ofstream out;
  11. in.open( "cardgame.in" );
  12. out.open( "cardgame.out" );
  13.  
  14. int n;
  15. in >> n;
  16.  
  17. int* elsie = new int[n];
  18. int* bessie = new int[2*n]; // -1 = doesn't have, 0 = unused, 1 = lost, 2 = won
  19. for( int i = 0; i < 2*n; i++ )
  20. bessie[i] = 0;
  21. for( int i = 0; i < n; i++ )
  22. {
  23. in >> elsie[i];
  24. elsie[i]--;
  25. bessie[elsie[i]] = -1;
  26. }
  27.  
  28. vector<int> lower;
  29. vector<int> higher;
  30.  
  31. for( int i = 0, k = 0; i < 2*n; i++ )
  32. {
  33. if( bessie[i] == 0 )
  34. {
  35. if( k < n/2 )
  36. {
  37. lower.push_back( i );
  38. k++;
  39. }
  40. else
  41. {
  42. higher.push_back( i );
  43. }
  44. }
  45. }
  46.  
  47. for( int i = 0; i < n/2; i++ )
  48. {
  49. if( elsie[i] > higher.back() )
  50. {
  51. bessie[higher.front()] = 1;
  52. higher.erase( higher.begin() );
  53. }
  54. else
  55. {
  56. int j = 0;
  57. for( j = 0; j < higher.size() && higher.at( j ) < elsie[i]; j++ );
  58. bessie[higher.at( j )] = 2;
  59. higher.erase( higher.begin()+j );
  60. }
  61. }
  62.  
  63. for( int i = n/2; i < n; i++ )
  64. {
  65. if( elsie[i] < lower.front() )
  66. {
  67. bessie[lower.back()] = 1;
  68. lower.erase( lower.end()-1 );
  69. }
  70. else
  71. {
  72. int j = 0;
  73. for( j = lower.size()-1; j >= 0 && lower.at( j ) > elsie[i]; j-- );
  74. bessie[lower.at( j )] = 2;
  75. lower.erase( lower.begin()+j );
  76. }
  77. }
  78.  
  79. int ans = 0;
  80. for( int i = 0; i < 2*n; i++ )
  81. if( bessie[i] == 2 )
  82. ans++;
  83.  
  84. out << ans << endl;
  85.  
  86. in.close();
  87. out.close();
  88. return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement