Advertisement
a53

peri

a53
Mar 7th, 2018
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. /*
  2. Peri O(N^3) solution
  3. by Radu Berinde
  4. */
  5.  
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8.  
  9. char A[251][251];
  10. int *B[251];
  11. int N, M;
  12.  
  13. int Best = -13;
  14. long BestNr = 0;
  15.  
  16. #define Sum(l1, l2, c) (B[l2][c] - B[l1-1][c])
  17.  
  18. void bagamarfa(int l1, int l2)
  19. {
  20. int i, max = Sum(l1, l2, 1), maxnr = 1, sum;
  21. for (i = 2; i <= M; i++)
  22. {
  23. sum = Sum(l1, l2, i);
  24. if (Best < max + sum) Best = max + sum, BestNr = maxnr;
  25. else
  26. if (Best == max + sum) BestNr += maxnr;
  27.  
  28. max += A[l1][i] + A[l2][i];
  29.  
  30. if (max < sum) max = sum, maxnr = 1;
  31. else
  32. if (max == sum) maxnr++;
  33. }
  34. }
  35.  
  36. void solve()
  37. {
  38. int l1, l2;
  39. for (l1 = 1; l1 <= N; l1++)
  40. for (l2 = l1+1; l2 <= N; l2++)
  41. bagamarfa(l1, l2);
  42. }
  43.  
  44.  
  45. void read_data()
  46. {
  47. FILE *fi = fopen("peri.in", "rt");
  48. int i, j, x;
  49. fscanf(fi, "%d %d", &N, &M);
  50. B[0] = (int *) calloc(M+1, sizeof(int));
  51. for (i = 1; i <= N; i++)
  52. {
  53. B[i] = (int *) calloc(M+1, sizeof(int));
  54. for (j = 1; j <= M; j++)
  55. {
  56. fscanf(fi, "%d", &x);
  57. A[i][j] = x ? 1 : -1;
  58. B[i][j] = B[i-1][j] + A[i][j];
  59. }
  60. }
  61. fclose(fi);
  62. }
  63.  
  64. int main()
  65. {
  66. read_data();
  67. solve();
  68. fprintf(fopen("peri.out", "wt"), "%d %ld\n", Best, BestNr);
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement