[关闭]
@zsh-o 2018-12-22T18:33:01.000000Z 字数 1216 阅读 820

讯飞

算法


1.png-43.9kB

单纯用排序会超时,采用桶的思想,需要注意的是多种情况要满足字典序最小

  1. #include <stdio.h>
  2. #include <string.h>
  3. struct ListNode{
  4. int value;
  5. int index;
  6. ListNode* next;
  7. ListNode(){
  8. value = 0; index = 0;
  9. next = NULL;
  10. }
  11. };
  12. struct List{
  13. int n;
  14. ListNode* next;
  15. List(){
  16. n = 0; next = NULL;
  17. }
  18. };
  19. const int MAXN = 1000000;
  20. const int MAXA = 10;
  21. int n, na, nb;
  22. List L[MAXA];
  23. int S[MAXN];
  24. void Insert(List* L, int a, int i){
  25. ListNode* t = new ListNode();
  26. t->index = i;
  27. t->value = a;
  28. t->next = L[a].next;
  29. L[a].next = t;
  30. L[a].n++;
  31. }
  32. int main(){
  33. int a;
  34. scanf("%d%d%d", &n, &na, &nb);
  35. for(int i=0; i<n; i++){
  36. scanf("%d", &a);
  37. Insert(L, a, i);
  38. }
  39. int n1 = na < nb ? na : nb;
  40. int n2 = na + nb - n1;
  41. int i = MAXA;
  42. ListNode* p = L[i].next;
  43. while(n1 > 0){
  44. while(p && n1>0){
  45. if(na < nb){
  46. S[p->index] = 1;
  47. }else{
  48. S[p->index] = 2;
  49. }
  50. p = p->next;
  51. n1--;
  52. }
  53. if(n1!=0){
  54. i--;
  55. p = L[i].next;
  56. if(n1 < L[i].n){
  57. break;
  58. }
  59. }
  60. }
  61. int rn1, rn2;
  62. if(na < nb){
  63. rn1 = n1;
  64. rn2 = L[i].n - n1;
  65. while(rn2 > 0){
  66. S[p->index] = 2;
  67. rn2--;
  68. n2--;
  69. p = p->next;
  70. }
  71. while(rn1 > 0){
  72. S[p->index] = 1;
  73. rn1--;
  74. n1--;
  75. p = p->next;
  76. }
  77. }else{
  78. rn1 = L[i].n - n1;
  79. rn2 = n1;
  80. while(rn2 > 0){
  81. S[p->index] = 2;
  82. rn2--;
  83. n2--;
  84. p = p->next;
  85. }
  86. while(rn1>0){
  87. S[p->index] = 1;
  88. rn1--;
  89. n1--;
  90. p = p->next;
  91. }
  92. }
  93. while(n2>0){
  94. while(p && n2>0){
  95. if(na < nb){
  96. S[p->index] = 2;
  97. }else{
  98. S[p->index] = 1;
  99. }
  100. p = p->next;
  101. n2--;
  102. }
  103. if(n2!=0){
  104. i--;
  105. p = L[i].next;
  106. if(n2 < L[i].n){
  107. break;
  108. }
  109. }
  110. }
  111. printf("\n");
  112. for(int i=0; i<n; i++){
  113. printf("%d ", S[i]);
  114. }
  115. printf("\n");
  116. }

  1. 5
  2. 3 2
  3. 4 4 5 4 4
  4. 1 1 2 1 2
  5. ---------------------------
  6. 5
  7. 2 3
  8. 4 4 5 4 4
  9. 1 2 1 2 2
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注