QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#632842#7725. Just Sum Two Numbersmemset0AC ✓8ms7488kbC++234.2kb2024-10-12 14:02:542024-10-12 14:02:54

Judging History

你现在查看的是最新测评结果

  • [2024-10-12 14:02:54]
  • 评测
  • 测评结果:AC
  • 用时:8ms
  • 内存:7488kb
  • [2024-10-12 14:02:54]
  • 提交

answer

#include <bits/stdc++.h>
#define sz(x) ((int)(x).size())
#define all(x) begin(x), end(x)
#ifdef memset0
#define log(...) fprintf(stderr, __VA_ARGS__)
#else
#define endl '\n'
#define log(...) (void(0))
#endif
using namespace std;
using ll = long long;
using lf = long double;
using pii = pair<int, int>;
using ull = unsigned long long;

template <class T> struct Point {
  using P = Point;
  T x, y;
  explicit Point(T x = 0, T y = 0) : x(x), y(y) {}
  bool operator<(P p) const { return tie(x, y) < tie(p.x, p.y); }
  bool operator==(P p) const { return tie(x, y) == tie(p.x, p.y); }
  P operator-(P p) const { return P(x - p.x, y - p.y); }
  T dot(P p) const { return x * p.x + y * p.y; }
  T cross(P p) const { return x * p.y - y * p.x; }
  T cross(P a, P b) const { return (a - *this).cross(b - *this); }
  T dist2() const { return x * x + y * y; }
  double dist() const { return sqrt(dist2()); }
};
using pt = Point<ll>;

vector<pt> convexHull(vector<pt> pts) {
  if (sz(pts) <= 1) return pts;
  sort(all(pts));
  vector<pt> h(sz(pts) + 1);
  int s = 0, t = 0;
  for (int it = 2; it--; s = --t, reverse(all(pts)))
    for (pt p : pts) {
      while (t >= s + 2 && h[t - 2].cross(h[t - 1], p) <= 0) t--;
      h[t++] = p;
    }
  return {h.begin(), h.begin() + t - (t == 2 && h[0] == h[1])};
}

pair<pt, pt> hullIiameter(vector<pt> S) {
  int n = sz(S), j = n < 2 ? 0 : 1;
  pair<ll, pair<pt, pt>> res({0, {S[0], S[0]}});
  for (int i = 0; i < j; i++)
    for (;; j = (j + 1) % n) {
      res = max(res, {(S[i] - S[j]).dist2(), {S[i], S[j]}});
      if ((S[(j + 1) % n] - S[j]).cross(S[i + 1] - S[i]) >= 0) break;
    }
  return res.second;
}

double polygonArea(const vector<pt> &v) {
  ll a = v.back().cross(v[0]);
  for (int i = 0; i + 1 < sz(v); i++) {
    a += v[i].cross(v[i + 1]);
  }
  return abs(a / 2.0);
}

double lineDist(pt a, pt b, pt p) { return (double)(b - a).cross(p - a) / (b - a).dist(); }

const int N = 1e6 + 9, M = 1e3 + 9;
const int nxt[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int n, m, ql, qr;
bool a[M][M], vis[M][M];
string buffer;
pair<int, int> q[N];

struct Pattern {
  vector<pii> a;
  vector<pt> b, cov;
  double x, y;
  pair<pt, pt> s;
  int num;

  bool operator<(const Pattern &rhs) const { return y < rhs.y; }
};
vector<Pattern> vec;

int main() {
#ifdef memset0
  freopen("J.in", "r", stdin);
#endif
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> buffer;
    for (int j = 1; j <= m; j++) {
      a[i][j] = buffer[j - 1] == '#';
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i][j] && !vis[i][j]) {
        q[ql = qr = 1] = {i, j}, vis[i][j] = 1;
        while (ql <= qr) {
          auto u = q[ql++];
          for (int k = 0; k < 4; k++) {
            int vx = u.first + nxt[k][0];
            int vy = u.second + nxt[k][1];
            if (a[vx][vy] && !vis[vx][vy]) {
              q[++qr] = {vx, vy}, vis[vx][vy] = 1;
            }
          }
        }

        vec.resize(vec.size() + 1);
        auto &cur = vec.back();
        cur.a = vector<pii>(q + 1, q + qr + 1);
        for (const auto &[x, y] : cur.a) {
          cur.b.emplace_back(x - 1, y - 1);
          cur.b.emplace_back(x - 1, y);
          cur.b.emplace_back(x, y - 1);
          cur.b.emplace_back(x, y);
        }
        sort(all(cur.b));
        cur.b.erase(unique(all(cur.b)), cur.b.end());

        cur.cov = convexHull(cur.b);
        cur.s = hullIiameter(cur.cov);
        cur.x = ((double)cur.s.first.x + cur.s.second.x) / 2;
        cur.y = ((double)cur.s.first.y + cur.s.second.y) / 2;
      }
  sort(all(vec));

  for (auto &cur : vec) {
    double area = polygonArea(cur.cov);
    log("cells %d area = %.2lf\n", sz(cur.a), area);
    if (cur.a.size() > 0.6 * area) {
      cur.num = 1;
    } else {
      double w = (cur.s.first - cur.s.second).dist();
      double h = 0;
      for (const auto &p : cur.b) {
        h = max(h, lineDist(cur.s.first, cur.s.second, p));
      }
      h *= 2;
      if (h / w > 0.05) {
        cur.num = 5;
      } else {
        cur.num = 1;
      }
    }
    log(">> num = %d\n", cur.num);
  }

  int ans = 0;
  for (int i = 0; i < sz(vec); i++) {
    ans += vec[i].num;
  }
  ans -= 5;
  cout << ans << endl;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 5832kb

input:

100 354
.......................................................................................................................................................................................................................................................................................................

output:

17

result:

ok 1 number(s): "17"

Test #2:

score: 0
Accepted
time: 2ms
memory: 5940kb

input:

135 269
.............................................................................................................................................................................................................................................................................
.........................

output:

14

result:

ok 1 number(s): "14"

Test #3:

score: 0
Accepted
time: 0ms
memory: 5960kb

input:

294 451
.......................................................................................................................................................................................................................................................................................................

output:

18

result:

ok 1 number(s): "18"

Test #4:

score: 0
Accepted
time: 3ms
memory: 4480kb

input:

234 701
.......................................................................................................................................................................................................................................................................................................

output:

32

result:

ok 1 number(s): "32"

Test #5:

score: 0
Accepted
time: 6ms
memory: 6084kb

input:

888 585
.......................................................................................................................................................................................................................................................................................................

output:

49

result:

ok 1 number(s): "49"

Test #6:

score: 0
Accepted
time: 3ms
memory: 4916kb

input:

579 350
.......................................................................................................................................................................................................................................................................................................

output:

26

result:

ok 1 number(s): "26"

Test #7:

score: 0
Accepted
time: 7ms
memory: 6224kb

input:

884 910
.......................................................................................................................................................................................................................................................................................................

output:

59

result:

ok 1 number(s): "59"

Test #8:

score: 0
Accepted
time: 5ms
memory: 5516kb

input:

652 717
.......................................................................................................................................................................................................................................................................................................

output:

51

result:

ok 1 number(s): "51"

Test #9:

score: 0
Accepted
time: 4ms
memory: 5076kb

input:

568 468
.......................................................................................................................................................................................................................................................................................................

output:

29

result:

ok 1 number(s): "29"

Test #10:

score: 0
Accepted
time: 4ms
memory: 6492kb

input:

923 959
.......................................................................................................................................................................................................................................................................................................

output:

61

result:

ok 1 number(s): "61"

Test #11:

score: 0
Accepted
time: 3ms
memory: 5920kb

input:

361 295
.......................................................................................................................................................................................................................................................................................................

output:

21

result:

ok 1 number(s): "21"

Test #12:

score: 0
Accepted
time: 3ms
memory: 5840kb

input:

712 929
.......................................................................................................................................................................................................................................................................................................

output:

50

result:

ok 1 number(s): "50"

Test #13:

score: 0
Accepted
time: 8ms
memory: 7392kb

input:

929 970
.......................................................................................................................................................................................................................................................................................................

output:

64

result:

ok 1 number(s): "64"

Test #14:

score: 0
Accepted
time: 0ms
memory: 4736kb

input:

394 346
.......................................................................................................................................................................................................................................................................................................

output:

24

result:

ok 1 number(s): "24"

Test #15:

score: 0
Accepted
time: 4ms
memory: 6556kb

input:

418 578
.......................................................................................................................................................................................................................................................................................................

output:

28

result:

ok 1 number(s): "28"

Test #16:

score: 0
Accepted
time: 3ms
memory: 6096kb

input:

457 499
.......................................................................................................................................................................................................................................................................................................

output:

27

result:

ok 1 number(s): "27"

Test #17:

score: 0
Accepted
time: 8ms
memory: 6312kb

input:

985 955
.......................................................................................................................................................................................................................................................................................................

output:

63

result:

ok 1 number(s): "63"

Test #18:

score: 0
Accepted
time: 3ms
memory: 4580kb

input:

209 704
.......................................................................................................................................................................................................................................................................................................

output:

30

result:

ok 1 number(s): "30"

Test #19:

score: 0
Accepted
time: 4ms
memory: 6424kb

input:

837 156
............................................................................................................................................................
..........................................................................................................................................

output:

37

result:

ok 1 number(s): "37"

Test #20:

score: 0
Accepted
time: 1ms
memory: 4284kb

input:

198 126
..............................................................................................................................
..............................................................................................................................
.........................................

output:

3

result:

ok 1 number(s): "3"

Test #21:

score: 0
Accepted
time: 7ms
memory: 6448kb

input:

996 684
.......................................................................................................................................................................................................................................................................................................

output:

57

result:

ok 1 number(s): "57"

Test #22:

score: 0
Accepted
time: 4ms
memory: 6204kb

input:

803 985
.......................................................................................................................................................................................................................................................................................................

output:

65

result:

ok 1 number(s): "65"

Test #23:

score: 0
Accepted
time: 3ms
memory: 6932kb

input:

948 820
.......................................................................................................................................................................................................................................................................................................

output:

62

result:

ok 1 number(s): "62"

Test #24:

score: 0
Accepted
time: 2ms
memory: 4848kb

input:

470 242
..................................................................................................................................................................................................................................................
....................................................

output:

19

result:

ok 1 number(s): "19"

Test #25:

score: 0
Accepted
time: 5ms
memory: 6044kb

input:

967 248
........................................................................................................................................................................................................................................................
..............................................

output:

49

result:

ok 1 number(s): "49"

Test #26:

score: 0
Accepted
time: 2ms
memory: 5996kb

input:

323 198
......................................................................................................................................................................................................
................................................................................................

output:

17

result:

ok 1 number(s): "17"

Test #27:

score: 0
Accepted
time: 4ms
memory: 5324kb

input:

623 385
.......................................................................................................................................................................................................................................................................................................

output:

31

result:

ok 1 number(s): "31"

Test #28:

score: 0
Accepted
time: 3ms
memory: 7292kb

input:

955 959
.......................................................................................................................................................................................................................................................................................................

output:

65

result:

ok 1 number(s): "65"

Test #29:

score: 0
Accepted
time: 7ms
memory: 6196kb

input:

987 745
.......................................................................................................................................................................................................................................................................................................

output:

56

result:

ok 1 number(s): "56"

Test #30:

score: 0
Accepted
time: 0ms
memory: 4460kb

input:

328 309
.......................................................................................................................................................................................................................................................................................................

output:

14

result:

ok 1 number(s): "14"

Test #31:

score: 0
Accepted
time: 5ms
memory: 6660kb

input:

574 923
.......................................................................................................................................................................................................................................................................................................

output:

47

result:

ok 1 number(s): "47"

Test #32:

score: 0
Accepted
time: 2ms
memory: 4188kb

input:

180 259
...................................................................................................................................................................................................................................................................
...................................

output:

13

result:

ok 1 number(s): "13"

Test #33:

score: 0
Accepted
time: 4ms
memory: 4772kb

input:

169 947
.......................................................................................................................................................................................................................................................................................................

output:

44

result:

ok 1 number(s): "44"

Test #34:

score: 0
Accepted
time: 4ms
memory: 5440kb

input:

892 244
....................................................................................................................................................................................................................................................
..................................................

output:

36

result:

ok 1 number(s): "36"

Test #35:

score: 0
Accepted
time: 3ms
memory: 7488kb

input:

1000 598
......................................................................................................................................................................................................................................................................................................

output:

57

result:

ok 1 number(s): "57"

Test #36:

score: 0
Accepted
time: 5ms
memory: 6576kb

input:

821 608
.......................................................................................................................................................................................................................................................................................................

output:

47

result:

ok 1 number(s): "47"

Test #37:

score: 0
Accepted
time: 5ms
memory: 6684kb

input:

223 988
.......................................................................................................................................................................................................................................................................................................

output:

51

result:

ok 1 number(s): "51"

Test #38:

score: 0
Accepted
time: 1ms
memory: 3888kb

input:

141 139
...........................................................................................................................................
...........................................................................................................................................
...............

output:

2

result:

ok 1 number(s): "2"

Test #39:

score: 0
Accepted
time: 7ms
memory: 6292kb

input:

921 940
.......................................................................................................................................................................................................................................................................................................

output:

51

result:

ok 1 number(s): "51"

Test #40:

score: 0
Accepted
time: 2ms
memory: 5804kb

input:

317 124
............................................................................................................................
............................................................................................................................
.............................................

output:

14

result:

ok 1 number(s): "14"

Test #41:

score: 0
Accepted
time: 5ms
memory: 5464kb

input:

692 482
.......................................................................................................................................................................................................................................................................................................

output:

38

result:

ok 1 number(s): "38"

Test #42:

score: 0
Accepted
time: 0ms
memory: 5796kb

input:

464 313
.......................................................................................................................................................................................................................................................................................................

output:

25

result:

ok 1 number(s): "25"

Test #43:

score: 0
Accepted
time: 0ms
memory: 6516kb

input:

697 545
.......................................................................................................................................................................................................................................................................................................

output:

35

result:

ok 1 number(s): "35"

Test #44:

score: 0
Accepted
time: 3ms
memory: 6268kb

input:

265 445
.......................................................................................................................................................................................................................................................................................................

output:

19

result:

ok 1 number(s): "19"

Test #45:

score: 0
Accepted
time: 4ms
memory: 5336kb

input:

701 197
.....................................................................................................................................................................................................
.................................................................................................

output:

35

result:

ok 1 number(s): "35"

Test #46:

score: 0
Accepted
time: 5ms
memory: 6700kb

input:

572 761
.......................................................................................................................................................................................................................................................................................................

output:

41

result:

ok 1 number(s): "41"

Test #47:

score: 0
Accepted
time: 1ms
memory: 5904kb

input:

254 236
............................................................................................................................................................................................................................................
..........................................................

output:

9

result:

ok 1 number(s): "9"

Test #48:

score: 0
Accepted
time: 5ms
memory: 6148kb

input:

644 866
.......................................................................................................................................................................................................................................................................................................

output:

46

result:

ok 1 number(s): "46"

Test #49:

score: 0
Accepted
time: 6ms
memory: 5148kb

input:

528 898
.......................................................................................................................................................................................................................................................................................................

output:

44

result:

ok 1 number(s): "44"

Test #50:

score: 0
Accepted
time: 0ms
memory: 5700kb

input:

796 387
.......................................................................................................................................................................................................................................................................................................

output:

34

result:

ok 1 number(s): "34"

Test #51:

score: 0
Accepted
time: 3ms
memory: 6052kb

input:

862 785
.......................................................................................................................................................................................................................................................................................................

output:

51

result:

ok 1 number(s): "51"

Test #52:

score: 0
Accepted
time: 5ms
memory: 5612kb

input:

688 663
.......................................................................................................................................................................................................................................................................................................

output:

46

result:

ok 1 number(s): "46"

Test #53:

score: 0
Accepted
time: 3ms
memory: 4512kb

input:

372 589
.......................................................................................................................................................................................................................................................................................................

output:

25

result:

ok 1 number(s): "25"

Test #54:

score: 0
Accepted
time: 0ms
memory: 6080kb

input:

100 1000
......................................................................................................................................................................................................................................................................................................

output:

94

result:

ok 1 number(s): "94"

Test #55:

score: 0
Accepted
time: 3ms
memory: 5800kb

input:

999 100
....................................................................................................
....................................................................................................
.............................................................................................

output:

94

result:

ok 1 number(s): "94"

Test #56:

score: 0
Accepted
time: 5ms
memory: 6772kb

input:

998 1000
......................................................................................................................................................................................................................................................................................................

output:

134

result:

ok 1 number(s): "134"