fork download
  1. #include<stdio.h>
  2. #include<vector>
  3. #include<queue>
  4. #include<algorithm>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. struct xy {
  10. int x, y;
  11. bool operator <(const xy a)const {
  12. if (a.x == x)return a.y < y;
  13. return a.x > x;
  14. }
  15. };
  16. priority_queue<xy>Q;
  17. int a[212121];
  18. vector<int>ansx;
  19. vector<int>ansy;
  20. vector<int>ansz;
  21.  
  22. int main() {
  23. int n;
  24. int ans = 0;
  25. scanf("%d", &n);
  26. int i;
  27. for (i = 0; i < n; i++) {
  28. scanf("%d", &a[i]);
  29. }
  30. sort(a, a + n);
  31. int cnt = 1;
  32. int now = a[0];
  33. for (i = 1; i < n; i++) {
  34. if (a[i] == a[i - 1]) {
  35. cnt++;
  36. }
  37. else {
  38. Q.push({ cnt,now });
  39. cnt = 1;
  40. now = a[i];
  41. }
  42. }
  43. Q.push({ cnt,now });
  44. while (Q.size() >= 3) {
  45. xy t1 = Q.top();
  46. Q.pop();
  47. xy t2 = Q.top();
  48. Q.pop();
  49. xy t3 = Q.top();
  50. Q.pop();
  51. t1.x--;
  52. t2.x--;
  53. t3.x--;
  54. if (t1.x>0)Q.push(t1);
  55. if (t2.x>0)Q.push(t2);
  56. if (t3.x>0)Q.push(t3);
  57. ans++;
  58. ansx.push_back(t1.y);
  59. ansy.push_back(t2.y);
  60. ansz.push_back(t3.y);
  61. }
  62. printf("%d\n", ans);
  63. for (i = 0; i < ansx.size(); i++) {
  64. int temp[3];
  65. temp[0] = ansx[i];
  66. temp[1] = ansy[i];
  67. temp[2] = ansz[i];
  68. sort(temp, temp + 3);
  69. printf("%d %d %d\n", temp[2], temp[1], temp[0]);
  70. }
  71. return 0;
  72. }
Success #stdin #stdout 0s 16072KB
stdin
Standard input is empty
stdout
0