Submission #47095

# Submission time Handle Problem Language Result Execution time Memory
47095 2018-04-27T12:23:21 Z SpaimaCarpatilor Cultivation (JOI17_cultivation) C++17
80 / 100
2000 ms 8184 KB
#include<bits/stdc++.h>

using namespace std;

int N, R, C, x[309], y[309], precalc[309][309][3];
const int INF = 1e9 + 100;
unsigned int ans = UINT_MAX;
set < int > sa, sb, sab;

namespace ds {
    multiset < int > xs, dx;

    void init ()
    {
        xs.clear (), dx.clear ();
    }

    void delDif (int x)
    {
        dx.erase (dx.find (x));
    }

    void add (int x)
    {
        if (xs.find (x) != xs.end ()) xs.insert (x);
        else
        {
            xs.insert (x);
            auto it1 = xs.lower_bound (x), it2 = xs.upper_bound (x);
            if (it1 != xs.begin () && it2 != xs.end ())
                it1 --,
                delDif ((*it2) - (*it1)),
                it1 ++;
            if (it1 != xs.begin ())
                it1 --,
                dx.insert (x - (*it1));
            if (it2 != xs.end ())
                dx.insert ((*it2) - x);
        }
    }

    void del (int x)
    {
        if (xs.count (x) >= 2) xs.erase (xs.find (x));
        else
        {
            auto it1 = xs.lower_bound (x), it2 = xs.upper_bound (x);
            if (it1 != xs.begin () && it2 != xs.end ())
                it1 --,
                dx.insert ((*it2) - (*it1)),
                it1 ++;
            if (it1 != xs.begin ())
                it1 --,
                delDif (x - (*it1));
            if (it2 != xs.end ())
                delDif ((*it2) - x);
            xs.erase (x);
        }
    }

    int maxDif ()
    {
        if (dx.empty ()) return 0;
        return *(dx.rbegin ());
    }

    int getA ()
    {
        if (xs.empty ()) return INF;
        return (*xs.begin ()) - 1;
    }

    int getB ()
    {
        if (xs.empty ()) return INF;
        return C - (*xs.rbegin ());
    }

    void store (int &a, int &b, int &df)
    {
        a = getA ();
        b = getB ();
        df = maxDif () - 1;
    }
};

unsigned int pos[1209];
int nr, stat[1209][3];
int dq[3][1209], fst[3], lst[3];

void clearDeque ()
{
    for (int i=0; i<3; i++)
        fst[i] = 1, lst[i] = 0;
}

void add (int pos)
{
    for (int i=0; i<3; i++)
    {
        while (fst[i] <= lst[i] && stat[dq[i][lst[i]]][i] <= stat[pos][i])
            lst[i] --;
        dq[i][++lst[i]] = pos;
    }
}

void del (int pos)
{
    for (int i=0; i<3; i++)
        while (fst[i] <= lst[i] && dq[i][fst[i]] <= pos)
            fst[i] ++;
}

int getSum ()
{
    if (fst[0] > lst[0]) return 2 * INF;
    int minA = stat[dq[0][fst[0]]][0], minB = stat[dq[1][fst[1]]][1], minAB = stat[dq[2][fst[2]]][2];
    return max (minA + minB, minAB);
}

unsigned int relevantXs[609], relevantX1[309], relevantX2[309];
unsigned int relevantShifts[1209], relevantS1[609], relevantS2[609];
void solve (int updown)
{
    ds::init ();
    clearDeque ();
    for (int i=1; i<=N; i++)
        relevantX2[i] = x[i] + updown + 1;
    merge (relevantX1 + 1, relevantX1 + N + 1, relevantX2 + 1, relevantX2 + N + 1, relevantXs + 1);

    nr = 1, pos[1] = -0, stat[1][0] = INF, stat[1][1] = INF, stat[1][2] = -1;
    int l = 1, r = 0;
    for (int currPos = 1; currPos <= 2 * N; currPos ++)
    {
        unsigned int t = relevantXs[currPos];
        while (r < N && x[r + 1] <= t)
            r ++;
        while (x[l] + updown < t && l <= N)
            l ++;
        nr ++, pos[nr] = t;
        stat[nr][0] = precalc[l][r][0];
        stat[nr][1] = precalc[l][r][1];
        stat[nr][2] = precalc[l][r][2];
    }
    pos[nr + 1] = UINT_MAX;
    int n = 0, m = 0, p = 0;
    for (int i=1; i<=nr; i++)
    {
        if (i > 1 && pos[i] - 1 <= updown)
            relevantS1[++n] = pos[i] - 1;
        if (pos[i] >= R && pos[i] - R <= updown)
            relevantS2[++m] = pos[i] - R;
    }
    merge (relevantS1 + 1, relevantS1 + n + 1, relevantS2 + 1, relevantS2 + m + 1, relevantShifts + 1);
    p = n + m, relevantShifts[++p] = updown;
    int j = 1, i = 1;
    for (int currPos = 1; currPos <= p; currPos ++)
    if (currPos == 1 || relevantShifts[currPos] != relevantShifts[currPos - 1])
    {
        unsigned int shift = relevantShifts[currPos];
        while (j <= nr && pos[j] <= R + shift)
            add (j), j++;
        while (pos[i + 1] - 1 < 1 + shift)
            del (i), i ++;
        unsigned int curr = updown + getSum ();
        if (curr < ans)
            ans = curr;
    }
}

void readAndSort ()
{
    pair < int, int > point[309];
    scanf ("%d %d\n", &R, &C);
    scanf ("%d", &N);
    for (int i=1; i<=N; i++)
        scanf ("%d %d", &point[i].first, &point[i].second);
    sort (point + 1, point + N + 1);
    for (int i=1; i<=N; i++)
        x[i] = point[i].first, y[i] = point[i].second;
}

void doPrecalc ()
{
    for (int i=1; i<=N + 1; i++)
    {
        ds::init ();
        ds::store (precalc[i][i - 1][0], precalc[i][i - 1][1], precalc[i][i - 1][2]);
        for (int j=i; j<=N; j++)
            ds::add (y[j]),
            ds::store (precalc[i][j][0], precalc[i][j][1], precalc[i][j][2]);
    }
}

int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);

readAndSort ();
doPrecalc ();
for (int i=1; i<=N; i++)
    sa.insert (x[i] - 1), sb.insert (R - x[i]), relevantX1[i] = x[i];
for (int i=1; i<=N; i++)
    for (int j=i + 1; j<=N; j++)
    if (x[i] != x[j])
    {
        int val = x[i] - x[j];
        if (val < 0) val = -val;
        sab.insert (val - 1);
    }
for (auto a : sa)
    for (auto b : sb)
        sab.insert (a + b);
for (auto ab : sab)
{
    if (ans >= ab)
        solve (ab);
}
printf ("%d\n", ans);
return 0;
}

Compilation message

cultivation.cpp: In function 'void solve(int)':
cultivation.cpp:136:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while (r < N && x[r + 1] <= t)
                         ~~~~~~~~~^~~~
cultivation.cpp:138:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while (x[l] + updown < t && l <= N)
                ~~~~~~~~~~~~~~^~~
cultivation.cpp:149:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (i > 1 && pos[i] - 1 <= updown)
                      ~~~~~~~~~~~^~~~~~~~~
cultivation.cpp:151:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (pos[i] >= R && pos[i] - R <= updown)
             ~~~~~~~^~~~
cultivation.cpp:151:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (pos[i] >= R && pos[i] - R <= updown)
                            ~~~~~~~~~~~^~~~~~~~~
cultivation.cpp: In function 'int main()':
cultivation.cpp:217:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if (ans >= ab)
         ~~~~^~~~~
cultivation.cpp: In function 'void readAndSort()':
cultivation.cpp:174:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d %d\n", &R, &C);
     ~~~~~~^~~~~~~~~~~~~~~~~~~
cultivation.cpp:175:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d", &N);
     ~~~~~~^~~~~~~~~~
cultivation.cpp:177:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf ("%d %d", &point[i].first, &point[i].second);
         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 428 KB Output is correct
4 Correct 2 ms 428 KB Output is correct
5 Correct 2 ms 616 KB Output is correct
6 Correct 2 ms 616 KB Output is correct
7 Correct 2 ms 616 KB Output is correct
8 Correct 2 ms 616 KB Output is correct
9 Correct 2 ms 616 KB Output is correct
10 Correct 2 ms 616 KB Output is correct
11 Correct 2 ms 616 KB Output is correct
12 Correct 2 ms 616 KB Output is correct
13 Correct 2 ms 616 KB Output is correct
14 Correct 2 ms 616 KB Output is correct
15 Correct 2 ms 616 KB Output is correct
16 Correct 2 ms 616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 428 KB Output is correct
4 Correct 2 ms 428 KB Output is correct
5 Correct 2 ms 616 KB Output is correct
6 Correct 2 ms 616 KB Output is correct
7 Correct 2 ms 616 KB Output is correct
8 Correct 2 ms 616 KB Output is correct
9 Correct 2 ms 616 KB Output is correct
10 Correct 2 ms 616 KB Output is correct
11 Correct 2 ms 616 KB Output is correct
12 Correct 2 ms 616 KB Output is correct
13 Correct 2 ms 616 KB Output is correct
14 Correct 2 ms 616 KB Output is correct
15 Correct 2 ms 616 KB Output is correct
16 Correct 2 ms 616 KB Output is correct
17 Correct 2 ms 616 KB Output is correct
18 Correct 3 ms 836 KB Output is correct
19 Correct 2 ms 836 KB Output is correct
20 Correct 2 ms 836 KB Output is correct
21 Correct 3 ms 836 KB Output is correct
22 Correct 5 ms 996 KB Output is correct
23 Correct 2 ms 996 KB Output is correct
24 Correct 8 ms 1380 KB Output is correct
25 Correct 6 ms 1380 KB Output is correct
26 Correct 14 ms 1636 KB Output is correct
27 Correct 14 ms 1636 KB Output is correct
28 Correct 8 ms 1636 KB Output is correct
29 Correct 14 ms 1744 KB Output is correct
30 Correct 14 ms 1872 KB Output is correct
31 Correct 14 ms 1872 KB Output is correct
32 Correct 14 ms 1872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 428 KB Output is correct
4 Correct 2 ms 428 KB Output is correct
5 Correct 2 ms 616 KB Output is correct
6 Correct 2 ms 616 KB Output is correct
7 Correct 2 ms 616 KB Output is correct
8 Correct 2 ms 616 KB Output is correct
9 Correct 2 ms 616 KB Output is correct
10 Correct 2 ms 616 KB Output is correct
11 Correct 2 ms 616 KB Output is correct
12 Correct 2 ms 616 KB Output is correct
13 Correct 2 ms 616 KB Output is correct
14 Correct 2 ms 616 KB Output is correct
15 Correct 2 ms 616 KB Output is correct
16 Correct 2 ms 616 KB Output is correct
17 Correct 2 ms 616 KB Output is correct
18 Correct 3 ms 836 KB Output is correct
19 Correct 2 ms 836 KB Output is correct
20 Correct 2 ms 836 KB Output is correct
21 Correct 3 ms 836 KB Output is correct
22 Correct 5 ms 996 KB Output is correct
23 Correct 2 ms 996 KB Output is correct
24 Correct 8 ms 1380 KB Output is correct
25 Correct 6 ms 1380 KB Output is correct
26 Correct 14 ms 1636 KB Output is correct
27 Correct 14 ms 1636 KB Output is correct
28 Correct 8 ms 1636 KB Output is correct
29 Correct 14 ms 1744 KB Output is correct
30 Correct 14 ms 1872 KB Output is correct
31 Correct 14 ms 1872 KB Output is correct
32 Correct 14 ms 1872 KB Output is correct
33 Correct 23 ms 1872 KB Output is correct
34 Correct 17 ms 1872 KB Output is correct
35 Correct 28 ms 1872 KB Output is correct
36 Correct 46 ms 1872 KB Output is correct
37 Correct 36 ms 1872 KB Output is correct
38 Correct 34 ms 1872 KB Output is correct
39 Correct 33 ms 1872 KB Output is correct
40 Correct 38 ms 1872 KB Output is correct
41 Correct 24 ms 1872 KB Output is correct
42 Correct 25 ms 1872 KB Output is correct
43 Correct 32 ms 1872 KB Output is correct
44 Correct 25 ms 1872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1872 KB Output is correct
2 Correct 4 ms 1872 KB Output is correct
3 Correct 3 ms 1872 KB Output is correct
4 Correct 4 ms 1872 KB Output is correct
5 Correct 3 ms 1872 KB Output is correct
6 Correct 2 ms 1872 KB Output is correct
7 Correct 2 ms 1872 KB Output is correct
8 Correct 4 ms 1872 KB Output is correct
9 Correct 3 ms 1872 KB Output is correct
10 Correct 4 ms 1872 KB Output is correct
11 Correct 4 ms 1872 KB Output is correct
12 Correct 3 ms 1872 KB Output is correct
13 Correct 2 ms 1872 KB Output is correct
14 Correct 3 ms 1872 KB Output is correct
15 Correct 2 ms 1872 KB Output is correct
16 Correct 5 ms 1872 KB Output is correct
17 Correct 3 ms 1872 KB Output is correct
18 Correct 3 ms 1872 KB Output is correct
19 Correct 3 ms 1872 KB Output is correct
20 Correct 4 ms 1872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1872 KB Output is correct
2 Correct 4 ms 1872 KB Output is correct
3 Correct 3 ms 1872 KB Output is correct
4 Correct 4 ms 1872 KB Output is correct
5 Correct 3 ms 1872 KB Output is correct
6 Correct 2 ms 1872 KB Output is correct
7 Correct 2 ms 1872 KB Output is correct
8 Correct 4 ms 1872 KB Output is correct
9 Correct 3 ms 1872 KB Output is correct
10 Correct 4 ms 1872 KB Output is correct
11 Correct 4 ms 1872 KB Output is correct
12 Correct 3 ms 1872 KB Output is correct
13 Correct 2 ms 1872 KB Output is correct
14 Correct 3 ms 1872 KB Output is correct
15 Correct 2 ms 1872 KB Output is correct
16 Correct 5 ms 1872 KB Output is correct
17 Correct 3 ms 1872 KB Output is correct
18 Correct 3 ms 1872 KB Output is correct
19 Correct 3 ms 1872 KB Output is correct
20 Correct 4 ms 1872 KB Output is correct
21 Correct 18 ms 1872 KB Output is correct
22 Correct 38 ms 1872 KB Output is correct
23 Correct 118 ms 1872 KB Output is correct
24 Correct 188 ms 1872 KB Output is correct
25 Correct 41 ms 1872 KB Output is correct
26 Correct 27 ms 1872 KB Output is correct
27 Correct 120 ms 1872 KB Output is correct
28 Correct 40 ms 1872 KB Output is correct
29 Correct 31 ms 1872 KB Output is correct
30 Correct 47 ms 1872 KB Output is correct
31 Correct 36 ms 1872 KB Output is correct
32 Correct 41 ms 1872 KB Output is correct
33 Correct 119 ms 1872 KB Output is correct
34 Correct 93 ms 1872 KB Output is correct
35 Correct 89 ms 1872 KB Output is correct
36 Correct 45 ms 1872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 428 KB Output is correct
4 Correct 2 ms 428 KB Output is correct
5 Correct 2 ms 616 KB Output is correct
6 Correct 2 ms 616 KB Output is correct
7 Correct 2 ms 616 KB Output is correct
8 Correct 2 ms 616 KB Output is correct
9 Correct 2 ms 616 KB Output is correct
10 Correct 2 ms 616 KB Output is correct
11 Correct 2 ms 616 KB Output is correct
12 Correct 2 ms 616 KB Output is correct
13 Correct 2 ms 616 KB Output is correct
14 Correct 2 ms 616 KB Output is correct
15 Correct 2 ms 616 KB Output is correct
16 Correct 2 ms 616 KB Output is correct
17 Correct 2 ms 616 KB Output is correct
18 Correct 3 ms 836 KB Output is correct
19 Correct 2 ms 836 KB Output is correct
20 Correct 2 ms 836 KB Output is correct
21 Correct 3 ms 836 KB Output is correct
22 Correct 5 ms 996 KB Output is correct
23 Correct 2 ms 996 KB Output is correct
24 Correct 8 ms 1380 KB Output is correct
25 Correct 6 ms 1380 KB Output is correct
26 Correct 14 ms 1636 KB Output is correct
27 Correct 14 ms 1636 KB Output is correct
28 Correct 8 ms 1636 KB Output is correct
29 Correct 14 ms 1744 KB Output is correct
30 Correct 14 ms 1872 KB Output is correct
31 Correct 14 ms 1872 KB Output is correct
32 Correct 14 ms 1872 KB Output is correct
33 Correct 23 ms 1872 KB Output is correct
34 Correct 17 ms 1872 KB Output is correct
35 Correct 28 ms 1872 KB Output is correct
36 Correct 46 ms 1872 KB Output is correct
37 Correct 36 ms 1872 KB Output is correct
38 Correct 34 ms 1872 KB Output is correct
39 Correct 33 ms 1872 KB Output is correct
40 Correct 38 ms 1872 KB Output is correct
41 Correct 24 ms 1872 KB Output is correct
42 Correct 25 ms 1872 KB Output is correct
43 Correct 32 ms 1872 KB Output is correct
44 Correct 25 ms 1872 KB Output is correct
45 Correct 2 ms 1872 KB Output is correct
46 Correct 4 ms 1872 KB Output is correct
47 Correct 3 ms 1872 KB Output is correct
48 Correct 4 ms 1872 KB Output is correct
49 Correct 3 ms 1872 KB Output is correct
50 Correct 2 ms 1872 KB Output is correct
51 Correct 2 ms 1872 KB Output is correct
52 Correct 4 ms 1872 KB Output is correct
53 Correct 3 ms 1872 KB Output is correct
54 Correct 4 ms 1872 KB Output is correct
55 Correct 4 ms 1872 KB Output is correct
56 Correct 3 ms 1872 KB Output is correct
57 Correct 2 ms 1872 KB Output is correct
58 Correct 3 ms 1872 KB Output is correct
59 Correct 2 ms 1872 KB Output is correct
60 Correct 5 ms 1872 KB Output is correct
61 Correct 3 ms 1872 KB Output is correct
62 Correct 3 ms 1872 KB Output is correct
63 Correct 3 ms 1872 KB Output is correct
64 Correct 4 ms 1872 KB Output is correct
65 Correct 18 ms 1872 KB Output is correct
66 Correct 38 ms 1872 KB Output is correct
67 Correct 118 ms 1872 KB Output is correct
68 Correct 188 ms 1872 KB Output is correct
69 Correct 41 ms 1872 KB Output is correct
70 Correct 27 ms 1872 KB Output is correct
71 Correct 120 ms 1872 KB Output is correct
72 Correct 40 ms 1872 KB Output is correct
73 Correct 31 ms 1872 KB Output is correct
74 Correct 47 ms 1872 KB Output is correct
75 Correct 36 ms 1872 KB Output is correct
76 Correct 41 ms 1872 KB Output is correct
77 Correct 119 ms 1872 KB Output is correct
78 Correct 93 ms 1872 KB Output is correct
79 Correct 89 ms 1872 KB Output is correct
80 Correct 45 ms 1872 KB Output is correct
81 Correct 289 ms 4860 KB Output is correct
82 Correct 295 ms 4988 KB Output is correct
83 Correct 218 ms 4988 KB Output is correct
84 Correct 1795 ms 5992 KB Output is correct
85 Correct 502 ms 8108 KB Output is correct
86 Correct 375 ms 8108 KB Output is correct
87 Correct 510 ms 8108 KB Output is correct
88 Execution timed out 2057 ms 8184 KB Time limit exceeded
89 Halted 0 ms 0 KB -