Advertisement
Guest User

mole

a guest
May 19th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. #include "mole.h"
  2. #include <cstdio>
  3. #include <utility>
  4.  
  5. void makeOneCycle(std::vector<int> &perm) {
  6. int nrswaps = ask(perm);
  7. for(int i = 0; i < perm.size() - 1; ++i) {
  8. std::swap(perm[i], perm[i + 1]);
  9. for(int i = 0; i < perm.size(); ++i)
  10. printf("%d ", perm[i]);
  11. printf("-> query\n");
  12. if(ask(perm) < nrswaps)
  13. std::swap(perm[i], perm[i + 1]);
  14. else
  15. nrswaps++;
  16. }
  17. }
  18.  
  19. int getTriangleSwaps(std::vector<int> perm, int a, int b, int c) {
  20. int aux = perm[c];
  21. perm[c] = perm[b];
  22. perm[b] = perm[a];
  23. perm[a] = aux;
  24.  
  25. return ask(perm);
  26. }
  27.  
  28. std::vector<int> find_standings(int N) {
  29. std::vector<int> perm;
  30. std::vector<int> cycleOrder;
  31. for(int i = 1; i <= N; ++i)
  32. perm.push_back(i);
  33.  
  34. makeOneCycle(perm);
  35.  
  36. cycleOrder.push_back(perm[0], perm[1]);
  37.  
  38. for(int i = 2; i < perm.size(); ++i) {
  39. int st = 1, dr = cycleOrder.size() + 1;
  40. while(dr - st > 1) {
  41. int mid = (st + dr) / 2;
  42. if(getTriangleSwaps(perm, perm[0], perm[cycleOrder[mid]], perm[i]))
  43. dr = mid;
  44. else
  45. st = mid;
  46. }
  47.  
  48. cycleOrder.insert(cycleOrder.begin(), st, perm[i]);
  49. }
  50.  
  51. return rez;
  52. }
  53.  
  54.  
  55. grader_mole
  56.  
  57. #include <iostream>
  58. #include <string>
  59. #include "mole.h"
  60.  
  61. using namespace std;
  62.  
  63. int counter;
  64.  
  65. std::vector<int> rez;
  66.  
  67. int ask(const vector<int> &perm) {
  68. int nswaps = 0;
  69.  
  70. if(perm.size() != rez.size()) {
  71. printf("Wrong size!");
  72. exit(1);
  73. }
  74.  
  75. vector<bool> fr(perm.size() + 1, false);
  76. for(int i = 0; i < perm.size(); ++i)
  77. if((perm[i] <= 0 || perm[i] > perm.size()) || fr[perm[i]] == true)
  78. exit(1);
  79. else
  80. fr[perm[i]] = true;
  81.  
  82. ++counter;
  83. vector<int> poz(perm.size() + 1, 0);
  84. vector<bool> viz(perm.size() + 1, false);
  85.  
  86. for(int i = 0; i < rez.size(); ++i)
  87. poz[rez[i]] = i;
  88.  
  89. for(int i = 0; i < rez.size(); ++i)
  90. if(!viz[perm[i]]) {
  91. nswaps++;
  92. int nod = i;
  93. while(!viz[perm[nod]]) {
  94. viz[perm[nod]] = true;
  95. nod = poz[perm[nod]];
  96. }
  97. }
  98.  
  99. return rez.size() - nswaps;
  100. }
  101.  
  102. void returnAnswer(std::string str, int score) {
  103. cerr << str;
  104. cout << score;
  105. }
  106.  
  107. void checkResult(const std::vector<int> &officialPerm, const std::vector<int> &participantPerm) {
  108. int i = 0;
  109. if(participantPerm.size() != officialPerm.size()) {
  110. cerr << "Wrong size!";
  111. fprintf(stderr, "Wrong size!");
  112. printf("0");
  113. }
  114.  
  115. while(i < participantPerm.size() && participantPerm[i] == officialPerm[i])
  116. ++i;
  117.  
  118. if(i < participantPerm.size())
  119. returnAnswer("Wrong perm!", 0);
  120. else
  121. returnAnswer("OK! [" + std::to_string(counter) + "]", 1);
  122. }
  123.  
  124. int main() {
  125. int N;
  126.  
  127. cin >> N;
  128. rez.resize(N);
  129. for(int i = 0; i < N; ++i)
  130. cin >> rez[i];
  131.  
  132. vector<int> participantOutput;
  133. participantOutput = find_standings(N);
  134.  
  135. checkResult(participantOutput, rez);
  136.  
  137. return 0;
  138. }
  139.  
  140.  
  141.  
  142. mole.h
  143.  
  144. #include <vector>
  145.  
  146. int ask(const std::vector<int> &guess);
  147. std::vector<int> find_standings(int N);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement