Codeforces development d

Source: Internet
Author: User

Given a matrix of n, m <= 300, and then finding a rectangle with a length and width greater than 2, it takes the least time to circle the border counterclockwise from the upper left corner. The calculation of time is as follows: given 3 numbers: Tu, TP, and TD, the number in the path is increased to Tu, equal to TP, otherwise TD.

Idea: directly pre-process four arrays and then directly launch brute force attacks. N4 method. However, a small constant can be 4000 + MS.

There is also an n3logn method ..

Specifically, the lower right corner is fixed, and then the upper right corner is enumerated, and a prefix and binary search are maintained through a push ..

But the constant must be much faster than the brute force ..

Code:

 1 #include <bits/stdc++.h> 2 #define M0(x) memset(x, 0, sizeof(x)) 3 #define repf(i, a, b) for(int i = (a); i <= (b); ++i) 4 #define repd(i, a, b) for(int i = (a); i >= (b); --i) 5 using namespace std; 6 int U[303][303], D[303][303], L[303][303], R[303][303], a[303][303]; 7 int n, m; 8 int tp, tu, td, t; 9 10 inline int f(const int& a, const int& b){11      return a == b ? tp : (a > b ? tu : td); 12 }13 14 void init(){15      scanf("%d%d%d", &tp, &tu, &td);16      M0(U), M0(D), M0(L), M0(R), M0(a);17      repf(i, 1, n) repf(j, 1, m) scanf("%d", &a[i][j]);18      repf(i, 1, n) repf(j, 1, m){19           D[i][j] = D[i-1][j] + f(a[i][j], a[i-1][j]);20           R[i][j] = R[i][j-1] + f(a[i][j], a[i][j-1]);21      }22      repd(i, n, 1) repd(j, m, 1){23           U[i][j] = U[i+1][j] + f(a[i][j], a[i+1][j]);24           L[i][j] = L[i][j+1] + f(a[i][j], a[i][j+1]);25      }26 }27 28 void solve(){29      int ansd = 0x3fffffff, ans = 0;30      int lx, ly, rx, ry, d;31      int tmp;32      for (int x = 1; x <= n; ++x)33          for (int y = 1; y <= m; ++y)34              for (int x1 = x + 2; x1 <= n; ++x1)35                  for (int y1 = y + 2; y1 <= m; ++y1){36                       tmp = D[x1][y1] - D[x][y1] + U[x][y] - U[x1][y];37                       tmp += (R[x][y1] - R[x][y] + L[x1][y] - L[x1][y1]);38                       d = abs(tmp - t);39                       if (d < ansd || (d == ansd && tmp < ans)){40                            lx = x, ly = y;41                            rx = x1, ry = y1;42                            ansd = d, ans = tmp;43                       }           44                  }45 //    cout <<ans << endl; 46      printf("%d %d %d %d\n", lx, ly, rx, ry);47 }48 49 int main(){50 //    freopen("a.in", "r", stdin);51     while (scanf("%d%d%d", &n, &m, &t) != EOF){52           init();53           solve();54     }55     return 0;56 }
View code

 

Codeforces development d

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.