USACO 2013 Mar Silver

Problem 1. Poker Hands

Clumsy Cows
Problem 1: Poker Hands [Albert Gu, 2011]

Bessie and her friends are playing a unique version of poker involving a deck with N (1 <= N <= 100,000) different ranks, conveniently numbered 1..N (a normal deck has N = 13). In this game, there is only one type of hand the cows can play: one may choose a card labeled i and a card labeled j and play one card of every value from i to j. This type of hand is called a “straight”.

Bessie’s hand currently holds a_i cards of rank i (0 <= a_i <= 100000). Help her find the minimum number of hands she must play to get rid of all her cards.

PROBLEM NAME: poker

INPUT FORMAT:

  • Line 1: The integer N.

  • Lines 2..1+N: Line i+1 contains the value of a_i.

SAMPLE INPUT (file poker.in):

5
2
4
1
2
3

OUTPUT FORMAT:

  • Line 1: The minimum number of straights Bessie must play to get rid of all her cards.

SAMPLE OUTPUT (file poker.out):

6

OUTPUT DETAILS:

Bessie can play a straight from 1 to 5, a straight from 1 to 2, a straight from 4 to 5, two straights from 2 to 2, and a straight from 5 to 5, for a total of 6 rounds necessary to get rid of all her cards.``

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int N;

int main() {
    setIO("poker");

    cin >> N;

    vector<int> cards(N);

    F0R(i, N) {
        cin >> cards[i];
    }

    ll ans = 0;

    FOR(i, 1, N) {
        if (cards[i] < cards[i - 1]) {
            ans += cards[i - 1] - cards[i];
        }
    }

    ans += cards[N - 1];

    cout << ans;
}

Problem 2. Farm Painting

Farm Painting
Problem 2: Farm Painting [Brian Dean, 2013]

After several harsh winters, Farmer John has decided it is time to re-paint his farm. The farm consists of N fenced enclosures (1 <= N <= 50,000), each of which can be described by a rectangle in the 2D plane whose sides are parallel to the x and y axes. Enclosures may be contained within other enclosures, but no two fences intersect, so if two enclosures cover the same area of the 2D plane, one must be contained within the other.

FJ figures that an enclosure contained within another enclosure will not be visible to the outside world, so he only wants to re-paint enclosures that are themselves not contained within any other enclosures. Please help FJ determine the total number of enclosures he needs to paint.

PROBLEM NAME: painting

INPUT FORMAT:

  • Line 1: The number of enclosures, N.

  • Lines 2..1+N: Each line describes an enclosure by 4 space-separated integers x1, y1, x2, and y2, where (x1,y1) is the lower-left corner of the enclosure and (x2,y2) is the upper-right corner. All coordinates are in the range 0..1,000,000.

SAMPLE INPUT (file painting.in):

3
2 0 8 9
10 2 11 3
4 2 6 5

INPUT DETAILS:

There are three enclosures. The first has corners (2,0) and (8,9), and so on.

OUTPUT FORMAT:

  • Line 1: The number of enclosures that are not contained within other enclosures

SAMPLE OUTPUT (file painting.out):

2

OUTPUT DETAILS:

Enclosure 3 is contained within enclosure 1, so there are two enclosures not contained within other enclosures.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
ll N;


int main() {
    setIO("painting");

    cin >> N;

    vector<array<ll, 5>> lines(N + N);

    F0R(i, N) {
        ll x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;

        lines[2 * i] = {x1, y2, y1, 1, i};
        lines[2 * i + 1] = {x2, y2, y1, 0, i};
    }

    sort(all(lines), [](array<ll, 5> a, array<ll, 5> b) {if (a[0] == b[0]) {return(a[1] < b[1]);} return a[0] < b[0];});

    ll ans = 0;

    set<pair<ll, ll>> prev;

    F0R(i, N + N) {
        if (lines[i][3]) {
            pair<ll, ll> val = * prev.lower_bound({lines[i][1], lines[i][2]});

            if (prev.size() > 0 && val.s < lines[i][2]) {
                continue;
            }
            else {
                ans++;
                prev.insert({lines[i][1], lines[i][2]});

            }
        }
        else {
            if (prev.count({lines[i][1], lines[i][2]})) {
                prev.erase({lines[i][1], lines[i][2]});
            }
        }
    }

    cout << ans;
}