鱼C论坛

 找回密码
 立即注册
查看: 620|回复: 0

[技术交流] 1.2Lucky Charms

[复制链接]
发表于 2023-8-16 13:03:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 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):

Droop  Bracelet         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;
}

本帖被以下淘专辑推荐:

  • · USACO|主题: 2, 订阅: 0
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-6 08:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表