Hướng dẫn giải của Sân golf


Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của flashmt

const fi='';
      fo='';
      maxn=1010;
var n,re,m,last:longint;
    a,h,hh:array[0..1,0..maxn] of longint;
    st,d:array[0..maxn] of longint;

procedure rf;
var i,j:longint;
begin
     read(m,n);
     st[0]:=-maxlongint;
     for i:=0 to 1 do
     begin
          a[i,n+1]:=maxlongint;
          a[i,0]:=maxlongint;
     end;
     for j:=1 to n do a[0,j]:=maxlongint;
end;

procedure pr;
var i,j,z,t:longint;
begin
     for i:=1 to m do
     begin
          last:=0; z:=i and 1;
          for j:=1 to n do
          begin
               read(a[z,j]);
               if a[z,j]>=a[1-z,j] then
               begin
                    h[z,j]:=h[1-z,j]+1;
                    hh[z,j]:=hh[1-z,j]+1;
               end
               else
               begin
                    h[z,j]:=1;
                    hh[z,j]:=1;
               end;
               if a[1-z,j]>a[1-z,j+1] then hh[z,j]:=1;
               if h[z,j]>re then re:=h[z,j];
               if a[z,j]<a[z,j-1] then
               begin
                    last:=1; st[1]:=hh[z,j]; d[1]:=j;
                    continue;
               end;
               if hh[z,j]>st[last] then
               begin
                    inc(last); st[last]:=hh[z,j]; d[last]:=j;
                    continue;
               end
               else
               begin
                    while hh[z,j]<=st[last] do
                    begin
                         if st[last]<=h[z,j] then t:=1 else t:=0;
                         if re<st[last]*(j-d[last]+t) then
                            re:=st[last]*(j-d[last]+t);
                         dec(last);
                    end;
                    inc(last);
                    if hh[z,j]<>st[last] then st[last]:=hh[z,j];
               end;
          end;
          for j:=1 to last do
              if re<st[j]*(n-d[j]+1) then
                 re:=st[j]*(n-d[j]+1);
     end;
     writeln(re);
end;

begin
     assign(input,fi); reset(input);
     assign(output,fo); rewrite(output);
     rf;
     pr;
     close(input); close(output);
end.

Code mẫu của ladpro98

#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>
#include <climits>
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define REP(i, a, b) for(int i = (a); i <=(b); i++)
#define FORD(i, a, b) for(int i = (a); i > (b); i--)
#define REPD(i, a, b) for(int i = (a); i >=(b); i--)
#define TR(it, a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define RESET(a, v) memset((a), (v), sizeof((a)))
#define SZ(a) (int((a).size()))
#define ALL(a) (a).begin(), (a).end()
#define PB push_back
#define MP make_pair
#define LL long long
#define LD long double
#define II pair<int, int>
#define X first
#define Y second
#define VI vector<int>

const int N = 1010;

using namespace std;

int a[N][N], L[N], R[N], h[N], lcp[N];
int n, m, ans;

int main() {
    ios :: sync_with_stdio(0); cin.tie(0);
    cin >> m >> n;
    REP(i, 1, m) REP(j, 1, n) cin >> a[i][j];
    REP(i, 1, m) {
        REP(j, 1, n)
        if (a[i][j] >= a[i - 1][j]) h[j]++;
            else h[j] = 1;
        REP(j, 2, n)
        if (a[i][j] >= a[i][j - 1]) lcp[j]++;
            else lcp[j] = 0;
        REP(j, 2, n)
            lcp[j] = min(lcp[j], min(h[j - 1], h[j]));
        REP(j, 2, n)
        for(L[j] = j - 1; L[j] > 1 && lcp[L[j]] >= lcp[j]; L[j] = L[L[j]]);
        REPD(j, n, 2)
        for(R[j] = j + 1; R[j] <= n && lcp[R[j]] >= lcp[j]; R[j] = R[R[j]]);
        REP(j, 1, n) ans = max(ans, h[j]);
        REP(j, 2, n) ans = max(ans, lcp[j] * (R[j] - L[j]));
    }
    cout << ans;
    return 0;
}

Code mẫu của RR

{$R+,Q+}
PROGRAM NKGOLF;
CONST
  FINP='';
  FOUT='';
  maxn=1005;
  oo=1000000;
VAR
  m,n,kq:longint;
  a:array[0..maxn,0..maxn] of longint;
  d2,d1:array[0..maxn,0..maxn] of longint;
  stack,pre:array[1..maxn*maxn] of longint;
  top:longint;
procedure readInput;
var
  f:text;
  i,j:longint;
begin
  assign(f,FINP); reset(f);
  read(f,m,n);
  for i:=1 to m do
    for j:=1 to n do
      read(f,a[i,j]);
  close(f);
end;
procedure solve1;
var
  i,j:longint;
begin
  for j:=1 to n do
    d1[1,j]:=1;
  kq:=0;
  for i:=2 to m do
    for j:=1 to n do
      begin
        if a[i,j]>=a[i-1,j] then d1[i,j]:=d1[i-1,j]+1
        else d1[i,j]:=1;
        if d1[i,j]>kq then kq:=d1[i,j];
      end;
end;
procedure writeOutput;
var
  f:text;
begin
  assign(f,FOUT); rewrite(f);
  writeln(f,kq);
  close(f);
end;
procedure pop(var u,v:longint);
begin
  u:=stack[top]; v:=pre[top]; dec(top);
end;
procedure push(u,v:longint);
begin
  inc(top); stack[top]:=u; pre[top]:=v;
end;
function min(a,b:longint):longint;
begin
  if a<b then min:=a else min:=b;
end;
procedure findMax(i:longint);
var
  j,v,u,luu:longint;
begin
  top:=0;
  for j:=1 to n do
    begin
      v:=j;
      if top>0 then luu:=stack[top];
      while (top>0) and (d2[i,stack[top]]>=d2[i,j]) do
        begin
          pop(u,v);
          if v>1 then
          begin
            if (luu-v+2)*min(d2[i,u],d1[i,v-1])>kq then
              kq:=(luu-v+2)*min(d2[i,u],d1[i,v-1]);
          end
          else
            if (luu-v+1)*d2[i,u]>kq then kq:=(luu-v+1)*d2[i,u];
        end;
      push(j,v);
    end;
  if top>0 then luu:=stack[top];
  while top>0 do
    begin
      pop(u,v);
      if v>1 then
      begin
        if kq<(luu-v+2)*min(d2[i,u],d1[i,v-1])
          then kq:=(luu-v+2)*min(d2[i,u],d1[i,v-1]);
      end
      else if (luu-v+1)*d2[i,u]>kq then kq:=(luu-v+1)*d2[i,u];
    end;
end;
procedure solve2;
var
  i,j:longint;
begin
  for i:=1 to m do a[i,0]:=-oo;
  for j:=1 to n do a[0,j]:=-oo;
  for i:=1 to m do
    for j:=1 to n do
      if a[i,j]<a[i,j-1] then d2[i,j]:=0
      else
        if a[i,j]<a[i-1,j] then d2[i,j]:=1
        else d2[i,j]:=d2[i-1,j]+1;
  for i:=1 to m do
    findMax(i);
end;
BEGIN
  readInput;
  solve1;
  solve2;
  writeOutput;
END.

Code mẫu của hieult

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

#define Rep(i,n) for(typeof(n) i = 0; i < (n); ++i)
#define Repd(i,n) for(typeof(n) i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(typeof(b) i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(typeof(a) i = (a); i >= (b); --i)
#define Fit(i,v) for(typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))
#define nl puts("")
#define sp printf(" ")
#define ok puts("ok")
//#include <conio.h>

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> void db(T a, int p = -1) { if (p >= 0) cout << fixed << setprecision(p); cout << a << " "; }
template<class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> struct Triple { T x, y, z; Triple() {} Triple(T _x, T _y, T _z) : x(_x), y(_y), z(_z) {} };
template<class T> Triple<T> euclid(T a, T b) { if (b == 0) return Triple<T>(1, 0, a); Triple<T> r = euclid(b, a % b); return Triple<T>(r.y, r.x - a / b * r.y, r.z); }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }
const int bfsz = 1 << 16; char bf[bfsz + 5]; int rsz = 0;int ptr = 0;
char gc() { if (rsz <= 0) { ptr = 0; rsz = fread(bf, 1, bfsz, stdin); if (rsz <= 0) return EOF; } --rsz; return bf[ptr++]; }
void ga(char &c) { c = EOF; while (!isalpha(c)) c = gc(); }
int gs(char s[]) { int l = 0; char c = gc(); while (isspace(c)) c = gc(); while (c != EOF && !isspace(c)) { s[l++] = c; c = gc(); } s[l] = '\0'; return l; }
template<class T> bool gi(T &v) {
    v = 0; char c = gc(); while (c != EOF && c != '-' && !isdigit(c)) c = gc(); if (c == EOF) return false; bool neg = c == '-'; if (neg) c = gc();
    while (isdigit(c)) { v = v * 10 + c - '0'; c = gc(); } if (neg) v = -v; return true;
}

const double PI = 2 * acos(0);
const string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int dr[] = {0, +1, 0, -1};
const int dc[] = {+1, 0, -1, 0};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const double eps = 1e-9;
const ll mod = 100000007;
typedef pair<int, int> II;

#define maxn 1005

int l[maxn], r[maxn], h[maxn], a[maxn][maxn], x[maxn][maxn];
int m,n,kq = 0;

int doc(){
    int res = 1, run;
    For(j, 1, n){
        run = 1;
        For(i, 2, m){
            if(x[i][j] >= x[i - 1][j]) run++;
            else run = 1;
            res = max(res, run);
        }
    }
    return res;
}

int ngang(){
    int res = 1, run;
    For(i, 1, m){
        run = 1;
        For(j, 2, n){
            if(x[i][j] >= x[i][j - 1]) run++;
            else run = 1;
            res = max(res, run);
        }
    }
    return res;
}

int cal(int r, int c){
    return (x[r][c] <= x[r + 1][c] && x[r][c] <= x[r][c + 1] && x[r + 1][c] <= x[r + 1][c + 1] && x[r][c + 1] <= x[r + 1][c + 1]);
}

int main(){
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);

     gi(m); gi(n);
     For(i, 1, m) For(j, 1, n) gi(x[i][j]);
     kq = max(doc(), ngang());
     m--; n--;
     For(i, 1, m) For(j, 1, n) a[i][j] = cal(i, j);
     memset(h,0,sizeof(h));
     For(i, 1, m) {
          For(j, 1, n) {
               if(a[i][j])
                     h[j]++;
               else h[j] = 0;
          }
          h[0] = -1; h[n+1] = -1;
          For(j, 1, n){
               l[j] = j;
               while(h[l[j]-1]>=h[j]) l[j] = l[l[j]-1];
          }
          Ford(j, n, 1){
               r[j] = j;
               while(h[r[j]+1]>=h[j]) r[j] = r[r[j]+1];
          }
          For(j, 1, n){
               if(h[j] > 0)
               kq = max(kq,(h[j] + 1) *(r[j]-l[j]+2));
              // cout << kq << endl;
          }
     }
     printf("%d",kq);
     //getch();
}

Code mẫu của khuc_tuan

uses math;
type integer = longint;
var
   tt, res, i, j, m, n : integer;
   a : array[1..1000,1..1000] of longint;
   b : array[1..1000,1..1000] of boolean;
   h, left, right : array[1..1000] of integer;
begin
     read( m, n);
     for i:=1 to m do for j:=1 to n do read(a[i,j]);
     for i:=1 to m-1 do for j:=1 to n-1 do b[i,j] :=  (a[i,j]<=a[i+1,j]) and (a[i,j]<=a[i,j+1]) and (a[i+1,j]<=a[i+1,j+1]) and (a[i][j+1]<=a[i+1][j+1]);
     res := 1;
     for i:=1 to m do begin
         tt := 1;
         for j:=2 to n do if a[i,j]>=a[i,j-1] then begin inc(tt); res := max( res, tt) end else tt:=1;

     end;
     for j:=1 to n do begin
         tt := 1;
         for i:=2 to m do if(a[i,j]>=a[i-1,j]) then begin inc(tt); res := max( res, tt) end else tt:=1;
     end;

     {for i:=1 to m-1 do begin for j:=1 to n-1 do write(b[i,j]:7); writeln end;}

     for i:=1 to m-1 do begin
         for j:=1 to n-1 do if b[i,j] then inc(h[j]) else h[j]:=0;
         for j:=1 to n-1 do begin
             tt := j-1;
             while(tt>0) and (h[tt]>=h[j]) do tt := left[tt];
             left[j] := tt;
         end;
         for j:=n-1 downto 1 do begin
             tt := j+1;
             while(tt<n) and (h[tt]>=h[j]) do tt := right[tt];
             right[j] := tt;
         end;
         for j:=1 to n-1 do if h[j]>0 then begin
              res := max( res, (h[j]+1) * (right[j] - left[j]));
         end;
     end;
     writeln(res);
end.

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.