fork(1) download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. /**
  6.  * Created by arpit on 4/2/17.
  7.  */
  8. class StudentArrangement {
  9.  
  10. static final int MAX =100000;
  11. static int [] a =new int[MAX];
  12. static int[] count =new int[MAX];
  13. static int[]next=new int[MAX];
  14.  
  15. public static void main(String[] args) throws IOException {
  16. int n,m,k;
  17. String[]s;
  18.  
  19. s=br.readLine().split("\\s");
  20. n=Integer.parseInt(s[0]);
  21. m=Integer.parseInt(s[1]);
  22. k=Integer.parseInt(s[2]);
  23.  
  24. s=br.readLine().split("\\s");
  25. for (int i = 0; i < n; i++) {
  26. a[i]=Integer.parseInt(s[i])-1;
  27. }
  28. System.out.println(solve1(a, n, m, k));
  29. }
  30.  
  31. private static int solve1(int[] preference, int n, int m, int k) {
  32. int cnt=0;
  33. if (n>(m*k)){
  34. cnt=n-m*k;
  35. n=m*k;
  36. }
  37. initNext(m);
  38.  
  39. for (int i = 0; i < n; i++) {
  40. if (count[preference[i]]<k)
  41. count[preference[i]]++;
  42. else if (count[next[preference[i]]]<k){
  43. count[next[preference[i]]]++;
  44. cnt++;
  45. }
  46. else {
  47. nextVacantRow(preference[i],k);
  48. count[next[preference[i]]]++;
  49. cnt++;
  50. }
  51. }
  52. return cnt;
  53. }
  54.  
  55. private static int nextVacantRow(int i,int k) {
  56. int temp=next[i];
  57. if (count[temp]<k){
  58. next[i]=temp;
  59. return temp;
  60. }
  61. next[i]=nextVacantRow(next[i],k);
  62. return next[i];
  63. }
  64.  
  65. private static void initNext(int m) {
  66. next[m-1]=0;
  67. for (int i = 0; i < m - 1; i++) {
  68. next[i]=i+1;
  69. }
  70. }
  71.  
  72. private static int solve(int[] preference, int n, int m, int k) {
  73. int cnt=0;
  74. if (n>(m*k)){
  75. cnt=n-m*k;
  76. n=m*k;
  77. }
  78.  
  79. for (int p = 0; p < n; p++) {
  80.  
  81. if (count[preference[p]]<k){
  82. count[preference[p]]++;
  83. }
  84. else {
  85. int temp= nextEmptySit(preference[p] + 1, m, k);
  86. if (temp>=0)
  87. count[temp]++;
  88. cnt++;
  89. }
  90. //System.out.println(ans);
  91. }
  92. return cnt;
  93. }
  94.  
  95. private static int nextEmptySit(int row, int m, int k) {
  96. while (true){
  97. if (count[row%m]<k){
  98. return row%m;
  99. }
  100. row++;
  101. }
  102. }
  103. }
  104.  
Success #stdin #stdout 0.04s 4386816KB
stdin
5 2 2
1 1 2 2 2
stdout
1