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()
- Code Jam to I/O for Women 2016【上】
- [Code Jam] Get To Work
- 练习题 welcome to code jam
- Hints for Women to Revive Romance
- 2016 google code jam
- Code Jam - Store Credit for Python
- Code Jam - Reverse Words for Python
- Code Jam - Minimum Scalar Product for Python
- Google Code Jam 2016 资格赛
- Google Code Jam 2016 R1B
- Google Code Jam 2016 Round2
- Playing Hard to Get Works Differently for Men and Women
- Use exceptions to watch for and handle I/O errors.
- Use exceptions to watch for and handle I/O errors.
- Scheduling for I/O
- Google Code Jam 2009预选赛第三题----Welcome to Code Jam
- Google Code Jam 2009资格赛 题目C Welcome to Code Jam
- Google Code Jam 2016 Round 1A
- 根据图片在ftp路径获取InputStream流
- [前端学习]基于Boostrap的弹出层和进度条应用
- [vijos 1448]: 校门外的树(树状数组/线段树)
- 阿里云搭建基于MatlabMPI的集群(八):NFS性能调优
- Spring的自动装配Bean的三种方式
- Code Jam to I/O for Women 2016【上】
- 1078 最小生成树
- MySQL安装和简单使用
- C#学习笔记(三)—–C#高级特性:扩展方法
- XZ_iOS之调起第三方App
- XML文件的解析
- 【转载】R语言数据处理——数据合并与追加
- AnnotationProcessor之Element和TypeElement
- 经典的使用关联数组的单词计数程序