Code Jam to I/O for Women 2016【上】

来源:互联网 发布:md5摘要算法 编辑:程序博客网 时间:2024/05/17 22:41

这次code jam一共四大题。
下面分别详细展示A+B两道题的解题思路及Python代码。

Problem A. Cody’s Jams
题目:
Problem

Cody, the owner of the legendary Cody’s Jams store, is planning a huge jam sale. To make things simple, he has decided to sell every item in his store at a 25% discount — that is, each item’s sale price is exactly 75% of its regular price. Since all of his regular prices happened to be integers divisible by four, his sale prices are conveniently also all integers.

To prepare for the sale, he placed an order to print new labels for all of his items at their sale prices. He also placed an order to print new labels for all of his items at their regular prices, to use once the sale is over.

Cody just came back from picking up his order. Unfortunately, the printer gave him both orders in one combined stack, sorted by price. Both the sale price and the regular price label for each item are present somewhere in the stack. However, both types of labels look the same, and since he does not remember the price of every item, he is not sure which labels are the sale price labels. Can you figure that out?

For instance, if the regular prices were 20, 80, and 100, the sale prices would be 15, 60, and 75, and the printer’s stack would consist of the labels 15, 20, 60, 75, 80, and 100.

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of two lines. The first line contains a single integer N, the number of items in Cody’s store. The second line contains 2N integers P1, P2, …, P2N in non-decreasing order by the price printed on each label given by the printer.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is a list of N integers: the labels containing sale prices, in non-decreasing order.

Limits

1 ≤ T ≤ 100.
1 ≤ Pi ≤ 109, for all i.
Pi ≤ Pi+1, for all i. (The prices are in non-decreasing order.)
It is guaranteed that a unique solution exists.
Small dataset

1 ≤ N ≤ 4.
Large dataset

1 ≤ N ≤ 100.
Sample

Input  2315 20 60 75 80 10049 9 12 12 12 15 16 20Output Case #1: 15 60 75Case #2: 9 9 12 15

Case #1 is the one described in the problem statement.

Notice in Case #2 that it is possible for multiple items to have the same price, and for an item to have a regular price that equals the sale price of another item.
这道题题意大概如下:
Cody给商店里的商品分别打印了原价,以及打75折之后的价格标签。商品数目为N,则标签数目为2N。现在标签数列已经为升序。
给出标签数列及商品数目,要求输出打折后的标签,仍旧以升序排列。
分为大小两个数据集。
需要下载数据,在本地得到结果再上传

这道题的思路:
如果数i / 3 * 4 in listOfItems,证明i是一个打折后的价格,输出这个标签价格,并在原数组中删除原价标签。时间复杂度0(n),空间复杂度O(n)

#!/usr/bin/python# -*- coding: utf-8 -*-def salePrice(intList):    res = []#新标签    while intList:#原标签        i = intList[0]        t = i / 3 * 4        if t in intList:#t为原价,在原标签里            res.append(i)#保存打折后的标签            intList.pop(0)#在原数组中删去原价标签和这个打折标签            del intList[intList.index(t)]        else:            del intList[intList.index(i)]#如果t不在原标签,证明i就是原价标签    return res#f = open('A-small-practice.in','r')f = open('A-large-practice.in','r')#打开小测试数据t = int(f.readline())#读入测试例子个数#f1 = open('A-small-out1.txt','a')f1 = open('A-large-out0.txt','a')#新建txt文件保存输出for i in range(1,t + 1):    n = int(f.readline())#读入商品个数    nl = [ int(s) for s in f.readline().split()]#读入商品标签数组    f1.write('Case #{}:'.format(i))    out = salePrice(nl)#调用函数,得到输出结果    for j in range(n):        f1.write(' '+str(out[j]))    f1.write('\n')#记得换行

Problem B. Dance Around The Clock
Problem

The owner of a prestigious ballroom has painted a beautiful circular clock on the dance floor, and a group of D dancers numbered 1 through D are about to literally “dance around the clock”. They are standing in a circle, with dancer 1 at the 12:00 position of the circle and the other dancers going clockwise around the circle in increasing numerical order. The number of dancers is even.

The dance will go on for N turns. On the i-th turn (counting starting from 1), the following will happen:

  • If i is odd, then the dancer currently at the 12:00 position will
    swap positions with the next dancer in clockwise order. Then, going
    past those two, the next pair of dancers in clockwise order will swap
    positions, and so on, all the way around the ring clockwise, until
    all dancers have participated in exactly one swap.
  • If i is even, then the dancer currently at the 12:00 position will
    swap positions with the next dancer in counterclockwise order. Then,
    going past those two, the next pair of dancers in counterclockwise
    order will swap positions, and so on, all the way around the ring
    counterclockwise, until all dancers have participated in a swap.

For example, this diagram shows the initial state and two turns of a dance with eight people.
这里写图片描述

Which two dancers will be next to dancer number K when the dance is over?

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each consists of one line with three integers D, K, and N: the total number of dancers, the number of one of the dancers, and the number of turns the dance will go on for.

Output

For each test case, output one line containing Case #x: y z, where:

  • x is the test case number (starting from 1).
  • y is the number of the dancer who will be standing to dancer number
    K’s left (that is, one step away in clockwise order) when the dance is over.
  • z is the number of the dancer who will be standing to dancer number
    K’s right (that is, one step away in counterclockwise order) when the dance is over.

Limits

1 ≤ T ≤ 100.
D is even.
1 ≤ K ≤ D.
Small dataset

4 ≤ D ≤ 10.
1 ≤ N ≤ 10.
Large dataset

4 ≤ D ≤ 108.
1 ≤ N ≤ 108.
Sample

Input  38 3 18 4 24 1 8Output Case #1: 6 4Case #2: 1 7Case #3: 2 4

For Cases #1 and #2, refer to the illustration above. In Case #1, after 1 turn, dancer 6 is to dancer 3’s left, and dancer 4 is to dancer 3’s right. In Case #2, after 2 turns, dancer 1 is to dancer 4’s left, and dancer 7 is to dancer 4’s right. Remember that you’re looking from the dancer’s perspective; it may help to think in terms of clockwise and counterclockwise instead of left and right.

In Case #3, after eight turns, the arrangement looks the same as the initial arrangement, with dancer 2 to dancer 1’s left, and dancer 4 to dancer 1’s right.

有大小两个测试数据。
对于小测试数据,我第一次用暴力解法,空间复杂度为O(n),时间复杂度也为O(n).因为我新建了三个列表来存储原数组,奇数数组和偶数数组,并使用了for循环。这个办法对于大测试数据根本没用。
于是,再次研究,得到以下变化规律:

  • 对于K为奇数,我们要求的是,经过N轮变化后,K在偶数数组中的插入位置。如果知道插入位置就知道左右数了。(我们知道有这么个偶数数组,但我们不需要建立它,奇数数组同理)
  • 每经过D/2轮变化,数K的左右两个数恢复到变化前的两个数
  • 如果K为奇数,则每经过一轮变化,它就在偶数数组中向右移动一步
  • 如果K为偶数,则每经过一轮变化,它就在奇数数组中向左移动一步
  • 注意边界情况

代码

#!/usr/bin/python# -*- coding: utf-8 -*-def swapPositions(d, k, n):    #pl = [i for i in range(1, d + 1)]这是第一次解题是建立的三个数组,空间浪费超多    #oddl = [2*i+1 for i in range(d/2)]    #evenl = [2*i for i in range(1, d/2 + 1)]    kleft = k + 1 if k < d else 1#K左边的数,如果K为D,则K左边的数为数组第一个数,即1    kright = k - 1 if k > 1 else d#K右边的数,如果K为1,则K右边的数为数组的最后一个数,即D    i = n % (d/2)#n是变化轮数,每次经过D/2次变化,K左右的数恢复到变化前。i为有效变化轮数    if k % 2 == 0: #if k is even, insert k into the oddl这里是K为偶数的情况        #pleft = oddl.index(kleft)        #pright = oddl.index(kright)        #kleft = oddl[(pleft - i)%(d/2)]        #kright = oddl[(pright - i)%(d/2)]        pkl = ((kleft + 1) / 2 - i + d/2)%(d/2)#如果把K插入到奇数数组,则K左边的数的下标(从1开始)        if pkl == 0:            pkl = d/2        pkr = ((kright + 1) / 2 - i + d/2)%(d/2)#如果把K插入到奇数数组,则K右边的数的下标(从1开始)        if pkr == 0:            pkr = d/2        kleft = 2 *pkl- 1#该下标对应的数值        kright = 2 * pkr - 1    if k % 2 == 1: # if k is odd, insert k into the evenl同理        #pleft = evenl.index(kleft)        ##pright = evenl.index(kright)        #kleft = evenl[(i + pleft) % (d / 2)]        #kright = evenl[(i + pright) % (d / 2)]        pkl = (i + kleft/2) % (d/2)        if pkl == 0:            pkl = d/2        pkr = (i + kright/2) % (d/2)        if pkr == 0:            pkr = d/2        kleft = 2 * pkl        kright = 2 * pkr    return [kleft, kright]#输出K左右两边的数#f = open('B-small-practice.in','r')同题A,这里不再多做解释f = open('B-large-practice.in','r')t = int(f.readline())#f1 = open('B-small-out27.txt','a')f1 = open('B-large-out4.txt','a')for i in range(1,t + 1):    nl = [ int(s) for s in f.readline().split()]    d = nl[0]    k = nl[1]    n = nl[2]    out = swapPositions(d, k, n)    f1.write('Case #{}: {} {}'.format(i, out[0], out[1]))    f1.write('\n')f1.close()
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 儿子欠老子钱不还怎么办 傲风电竞椅扶手坏了怎么办 桑蚕丝面料发黄了怎么办? 天然植物纤维面料发黄怎么办 阴阳师吸血姬血太厚了怎么办 电话被骗了钱怎么办 镇魔曲元宝被扣怎么办 镇魔曲以前的角色怎么办 镇魔曲手游转职后装备怎么办 镇魔曲个性标签任务怎么办 教师资格证申请表打不开怎么办 怎么办appstore换到日本 电脑内存太小怎么办 win10声音卡顿怎么办 录音播放卡顿怎么办 笔记本电脑玩dnf卡怎么办 龙之谷约惠码被删了怎么办 天涯明月刀马没有了怎么办 icloud照片无法同步怎么办 ipad不能下载app怎么办 ipad屏幕孔进水怎么办 ipad无法验证登录怎么办 dnf电脑配置低怎么办 淘宝直播粉丝不够怎么办 电脑页面显示不全怎么办 脸上发痒长痘怎么办 扣扣魔性表情泡泡消失怎么办 脚起小泡泡很痒怎么办 孕妇脚痒起水泡怎么办 孕妇手脚起湿疹怎么办 嘴巴破皮了怎么办 小便刺痛阴唇红肿怎么办 集成墙面挂照片怎么办 苹果电脑网页游戏打不开怎么办 苹果6plus发热怎么办 玩手游手机太卡怎么办 苹果七发烫厉害怎么办 苹果手机延迟高怎么办 王者荣耀总是卡怎么办 王者荣耀卡屏怎么办 王者荣耀网络延迟怎么办