这次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
2
3
15 20 60 75 80 100
4
9 9 12 12 12 15 16 20Output
Case #1: 15 60 75
Case #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 * 4if 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
3
8 3 1
8 4 2
4 1 8Output
Case #1: 6 4
Case #2: 1 7
Case #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左边的数为数组第一个数,即1kright = k - 1 if k > 1 else d#K右边的数,如果K为1,则K右边的数为数组的最后一个数,即Di = 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/2pkr = ((kright + 1) / 2 - i + d/2)%(d/2)#如果把K插入到奇数数组,则K右边的数的下标(从1开始)if pkr == 0:pkr = d/2kleft = 2 *pkl- 1#该下标对应的数值kright = 2 * pkr - 1if 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/2pkr = (i + kright/2) % (d/2)if pkr == 0:pkr = d/2kleft = 2 * pklkright = 2 * pkrreturn [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【上】相关推荐

  1. Code jam 2008 practice A

    Code jam 2008 practice A A:Saving the universe B:Train timetable C:Fly swatter A: The urban legend g ...

  2. Google Code Jam程序设计大赛中国人获冠亚军

    来自 Google的官方消息,今年的Google Code Jam程序设计大赛,冠亚军都被中国人获得. 冠军是楼天城(Tiancheng Lou,来自清华大学计算机系),奖金$10,000.亚军朱泽园 ...

  3. 入职顶级互联网公司,竞争性编程是必须的吗?Google code jam King赛前采访(附有视频)

    主持人:你能告诉我一些关于你自己的事吗 受访者:就像我决定从事的竞争性编程一样,竞争性编程我不仅参加了那些比赛,高中时我在美国参加Google Code Jam,因为我进入了决赛,这是我生命中的一部分 ...

  4. Code Jam - Store Credit for Python

    这道题是Code Jam Africa 2010, Qualification Round的原题,现被用于练习 (https://code.google.com/codejam/contest/351 ...

  5. 【Google Code Jam】Millionaire

    题目描述 Google Code Jam 2008APAC local onsites C 最开始你有X元钱,要进行M轮赌博.每一轮赢的概率为P,你可以选择赌与不赌,如果赌也可以将所持的任意一部分钱作 ...

  6. Windows Server 2016上SQL Server Always On可用性组的全面指南

    In this article, we will configure a SQL Server Always On Availability Group on the Windows Server 2 ...

  7. Windows Server 2016上具有Docker容器SQL Server

    In this article let's see how to install a SQL Server Instance using a Docker container in Windows s ...

  8. 如何在Windows Server 2016上配置iSCSI启动器

    In this article, I am going to explain how we can configure the iSCSI initiator and use it to connec ...

  9. 配置iscsi服务器_在Windows Server 2016上安装和配置iSCSI目标服务器

    配置iscsi服务器 In this article, I am going to explain how we can install and configure the iSCSI Target ...

最新文章

  1. 判断直线与线段 是否相交 + 加入误差 故需要判断重点 poj 3304 Segments
  2. html 页面中的 base href 和 target
  3. 《JavaScript设计模式与开发实践》模式篇(3)—— 代理模式
  4. lisp将图元追加选择_汕尾幸运儿喜爱大乐透 两次买彩票就擒获8注追加二等奖254万元...
  5. C语言,你真的弄懂了么?
  6. AGG第三十一课 pattern_perspective样式透明
  7. Navicate ---error 2003: can‘t connect to mysql server on ‘localhost‘(10061)“
  8. unity可以直接转h5吗_瞎折腾:用Unity撸纯HTML5移动游戏/应用
  9. 【zhasite】托业英语阅读技巧有哪些
  10. 微信打开h5链接,缓存未清除解决办法
  11. 央行企业征信异议、声明业务办理流程?
  12. 在 Airbnb 使用机器学习预测房源的价格
  13. 计算机论文研究思路怎么写,论文研究思路要写什么
  14. HTTP中Get、Post、Put与Delete的区别
  15. 唐桥医学云会议助力第二届世界中医药互联网产业大会
  16. 运放放大倍数计算公式_低噪声前置高增益放大器设计
  17. 碳足迹-ISO14064认证
  18. 机器学习-第2关:信息熵与信息增益
  19. 【uniapp】小程序中修改Vant组件navbar左箭头的颜色及图标与背景
  20. 第一次使用微信开发者工具,登录不显示二维码

热门文章

  1. 提供免费点菜系统,欢迎使用
  2. matlab/simulink 风储调频,风电调频,一次调频,四机两区系统,采用频域模型法使得风电渗透率25%,附加虚拟惯性控制
  3. 面向对象实验unit2-题目1(综合性题目):面向对象实验之实现复试系统
  4. 金沙数据-《大话搜索引擎》之开篇
  5. 手机QQ AndroidManifest 用于跳转
  6. 广东工业大学2016校赛决赛Problem B Sward Art Online(分组背包)
  7. Cubietruck---25.android蓝牙分析3_search分析 2
  8. 本科二批计算机类学校有哪些,二本大学有哪些学校比较好?本科二批公办大学有哪些?...
  9. 不用安装软件也能下载网页视频的方法
  10. ❤️数据可视化❤️:基于Echarts + GeoJson实现的地图视觉映射散点(气泡)组件【25】 - 新疆省