Seawolf 发表于 2023-8-16 13:03:24

1.2Lucky Charms

本帖最后由 Seawolf 于 2023-8-17 08:58 编辑

Lucky Charms
============

Bessie has a lovely charm bracelet whose length is L (4 <= L <=
32,768) mm.Hanging from this bracelet are C (1 <= C <= 512) charms,
each at a unique integer distance from the bracelet's left side.
Charm i dangles on the end of a string whose length is S_i mm (1
<= S_i <= 25) and which is located P_i (0 <= P_i <= L) mm from the
bracelet's left side.

Margaret snatches the bracelet from Bessie and nails it (with a
zero-width nail) to a fencepost. The nail is located N mm (1 <= N
<= L-1) from the left side of the bracelet, and the bracelet itself
thus hangs left and right of the nail, with gravity pulling the
bracelet and charms straight down.

Bessie is curious: How far is each charm from the nail in the fencepost?

By way of example, consider a bracelet of length 16 mm with three
charms. The schematic diagram below shows + signs which are each
separated by 1 mm and vertical bars which each represent 1mm of an
attached string. The charms are defined to be 4, 7, and 3 mm from
the bracelet.
   
                        1 1 1 1 1 1 1
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
            |         |             |
            |         |             |
            |         |             *
            *         |
                      |
                      |
                      *

When the charm is nailed to the fencepost with the nail at location
5, it droops like this (please ignore the left-right spread which
is shown for clarity):

DroopBracelet         Bracelet
dist.location         location
0      5         +      5    <---- nail is here
1      4      + +       6
2      3      | + +       7
3      2      | + +       8            D
4      1      | + +       9            O
5      0      * + + |    10            W
6               + |    11            N
7               + |    12            |
8               + |    13            |
9               + |    14            V
10               + |    15
11               + *    16
12                     |
13                     |
14                     *

As you can see, the first charm droops down 5 mm from the nail; the
second charm droops to 11 mm and the third charm all the way down
to 14 mm from the nail.

Calculate the charm droop distance for each charm given.

PROBLEM NAME: charms

INPUT FORMAT:

* Line 1: Three space-separated integers: L, C, and N

* Lines 2..C+1: Line i+1 describes charm i with two space-separated
      integers: S_i and P_i

SAMPLE INPUT:

16 3 5
4 4
7 9
3 16

OUTPUT FORMAT:

* Lines 1..C: Line i contains the distance from charm i to the nail

SAMPLE OUTPUT:

5
11
14


USACO limits
Time Complexity: O(5 * 10^8)
Space Complexity: O(4 * 10^6)


#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    int len, N, nail;
    cin >> len >> N >> nail;
   
    for(int i = 0; i < N; i++)
    {
      int l, pos;
      cin >> l >> pos;
      
      cout << abs(pos - nail) + l << endl;
    }
    return 0;
}
页: [1]
查看完整版本: 1.2Lucky Charms