fork download
  1. #include <iostream>
  2. #include <set>
  3. #include <vector>
  4. #include <map>
  5. #include <climits>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. int main() {
  10.  
  11. int n, x;cin >> n >> x;
  12. vector<int> v;
  13. for (int i = 1;i <= n;i++) {
  14. int value;cin >> value;
  15. v.push_back(value);
  16. }
  17.  
  18. // []: không lấy giá trị bên ngoài
  19. // [=]: lấy tất cả giá trị bên ngoài bằng truyền tham trị
  20. // [&]: lấy tất cả giá trị bên ngoài bằng truyền tham chiếu
  21. // [x]: lấy giá trị x bằng tham trị
  22. // [&x]: lấy giá trị x bằng tham chiếu
  23. sort(v.begin(), v.end(), [x](const int& a, const int& b) {
  24. int da = abs(a - x);
  25. int db = abs(b - x);
  26. if (da != db) {
  27. return da < db;
  28. }
  29. return a < b;
  30. });
  31.  
  32. for (auto value : v) {
  33. cout << value << " ";
  34. }
  35.  
  36. cout << endl;
  37.  
  38. sort(v.begin(), v.end(), [](const int& a, const int& b) {
  39. if (a % 2 == 0 && b % 2 == 0) {
  40. return a < b;
  41. }
  42. if (a % 2 == 0 && b % 2 != 0) {
  43. return true;
  44. }
  45. if (a % 2 != 0 && b % 2 != 0) {
  46. return a > b;
  47. }
  48. if (a % 2 != 0 && b % 2 == 0) {
  49. return false;
  50. }
  51. });
  52. for (auto value : v) {
  53. cout << value << " ";
  54. }
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0s 544KB
stdin
Standard input is empty
stdout