fork(1) download
  1. # include <stdio.h>
  2. # include <stdlib.h>
  3. # include <stdbool.h>
  4. typedef struct two_together{
  5. int x;
  6. int y;
  7. int count;
  8.  
  9. }complex_array;
  10.  
  11. int comparefunc(const void *a,const void *b)
  12. {
  13.  
  14. complex_array aa = *(complex_array*)a;
  15. complex_array bb = *(complex_array*)b;
  16. if (aa.x>bb.x)
  17. {
  18.  
  19. return -1;
  20. }
  21. else if(aa.x == bb.x)
  22. {
  23.  
  24. if(aa.y>bb.y)return 1;
  25. else if(aa.y<bb.y)return -1;
  26. else return 0;
  27. }
  28. else return 1;
  29. }
  30.  
  31. int main(void)
  32. {
  33. complex_array hold[100000+10] = {{0},{0},{0}};
  34. complex_array *second_frequency = malloc((100000+10)*sizeof(complex_array));
  35. for (int i = 0; i < 100000+10; ++i)
  36. {
  37. second_frequency[i].x = 0;
  38. second_frequency[i].y = 0;
  39. second_frequency[i].count = 0;
  40. }
  41. int n,m,p,temp1,temp2,p_copy,i = 1,general_counter,ans = 0;
  42. scanf("%d",&n);
  43. scanf("%d",&m);
  44. scanf("%d",&p);
  45. p_copy = p;
  46. while(p--)
  47. {
  48. scanf("%d %d",&temp1,&temp2);
  49. hold[i].x = temp1;
  50. hold[i].y = temp2;
  51. hold[i].count++;
  52. i++;
  53.  
  54. }
  55. qsort(hold+1,p_copy,sizeof(complex_array),comparefunc);
  56. int j = 1;
  57.  
  58. for (int i = 1; i <=p_copy ;i++)//compressed array
  59. {
  60. int prev = i;
  61. while(((hold[i].x == hold[i+1].x)&&(hold[i].y == hold[i+1].y)))
  62. {
  63. hold[prev].count++;i++;
  64. }
  65. second_frequency[j].x = hold[prev].x;
  66. second_frequency[j].y = hold[prev].y;
  67. second_frequency[j].count = hold[prev].count;
  68. j++;
  69.  
  70. }
  71. //for (int i = 1; i < j;++i)printf("%d %d %d\n",second_frequency[i].x,second_frequency[i].y,second_frequency[i].count );
  72. int prev_y;
  73. int prev_max_count;
  74. ans =0;
  75. general_counter = j-1;
  76. for (int i = 1; i <=n ; ++i)//row 1 to n ans
  77. {
  78.  
  79. if((second_frequency[general_counter].x==i)&&(general_counter>=1))
  80. {
  81.  
  82. if (second_frequency[general_counter].y!=m)
  83. {
  84. if(second_frequency[general_counter].count>1)
  85. {
  86. printf("%d\n",-1 );
  87. while(second_frequency[general_counter].x==i)general_counter--;
  88. }
  89. else
  90. {
  91. prev_max_count = second_frequency[general_counter].count;
  92. prev_y = second_frequency[general_counter].y;
  93. general_counter--;
  94. while((second_frequency[general_counter].x==i)&&(general_counter>=1))
  95. {
  96. if (second_frequency[general_counter].y+1 == prev_y)
  97. {
  98. if (!(second_frequency[general_counter].count<=prev_max_count+1))
  99. {
  100. printf("%d\n",-1 );
  101. break;
  102. }
  103.  
  104. }
  105. else if(second_frequency[general_counter].count>1)
  106. {
  107.  
  108. printf("%d\n", -1);break;
  109. }
  110. prev_y = second_frequency[general_counter].y;
  111. prev_max_count = second_frequency[general_counter].count;
  112. general_counter--;
  113.  
  114. }
  115. if (second_frequency[general_counter+1].x == i)//assuring loop doesnt break
  116. {
  117. if (second_frequency[general_counter+1].y == 1)
  118. {
  119. ans-=second_frequency[general_counter+1].count;
  120. }
  121. printf("%d\n", m-1+ans);
  122. }
  123.  
  124. }
  125.  
  126. }
  127. else
  128. {
  129. ans+=second_frequency[general_counter].count;
  130. prev_max_count = second_frequency[general_counter].count;
  131. prev_y = second_frequency[general_counter].y;
  132. general_counter--;
  133. while((second_frequency[general_counter].x==i)&&(general_counter>=1))
  134. {
  135. if (second_frequency[general_counter].y+1 == prev_y)
  136. {
  137. if (!(second_frequency[general_counter].count<=prev_max_count+1))
  138. {
  139. printf("%d\n",-1 );
  140. break;
  141. }
  142.  
  143. }
  144. else if(second_frequency[general_counter].count>1)
  145. {
  146.  
  147. printf("%d\n", -1);break;
  148. }
  149. prev_y = second_frequency[general_counter].y;
  150. prev_max_count = second_frequency[general_counter].count;
  151. general_counter--;
  152.  
  153. }
  154. if (second_frequency[general_counter+1].x == i)//assuring loop doesnt break
  155. {
  156. if (second_frequency[general_counter+1].y == 1)
  157. {
  158. ans-=second_frequency[general_counter+1].count;
  159. }
  160. printf("%d\n", m-1+ans);
  161. }
  162.  
  163. }
  164.  
  165. }
  166. else
  167. {
  168. printf("%d\n",m-1 );
  169. }
  170.  
  171. ans = 0;
  172.  
  173. }
  174.  
  175.  
  176.  
  177.  
  178.  
  179. free(second_frequency);
  180. return 0;
  181. }
Success #stdin #stdout 0s 3352KB
stdin
1 4 3
1 1
1 1
1 4
stdout
-1
4