python学习

本文记录了作者使用 Python 完成 CodeForces 平台上的竞赛题目过程,包括了 A、B、C 和 D 四个级别的题目解析及代码实现。文中详细介绍了 Python 的各种用法,如列表推导式、函数定义等,并分享了解题思路。
部署运行你感兴趣的模型镜像

        这两天开始学习python,不知道从何学起。上w3cschool看了下,果断用来试着敲CF Round 298。

A

n = int(input())
if n<=2:
	print "1\n1"
elif n==3:
	print "2\n1 3"
else:
	print n
	for i in range(2,n+1,2):
		print i,
	for i in range(1,n+1,2):
		print i,

B

这题表示不知道怎么一行输入多个数了。。然后看了一下别人的代码,竟然可以这么短。

v1, v2 = map(int, raw_input().split())
t, d = map(int, raw_input().split())
ans = 0
for i in range(0,t):
	ans=ans+min(v1+d*i,v2+d*(t-i-1))
print ans

v1, v2 = map(int, raw_input().split())
t, d = map(int, raw_input().split())
print sum(min(v1 + d * i, v2 + d*(t-i-1)) for i in xrange(t))

C

果然python大法好。。参考一下B的输入,就能写这么短的代码

n, A = map(int, raw_input().split())
d = map(int, raw_input().split())
sum=0
for i in range(0,n):
	sum+=d[i]
for i in range(0,n):
	sum-=d[i]
	print max(0,A-sum-1)+max(0,d[i]-(A-(n-1)) ),
	sum+=d[i]

D

还是有好些语法不会。注意下面代码中二维数组的定义。同时我发现了一个现象,循环中改变循环变量的值好像是无效的。另外可以pop()数组,还会有返回值。

n = int(input())
a = map(int,raw_input().split())
lis=[[] for i in range(n)]

for i in range(0,n):
	lis[a[i]].append(i+1)

ans = []
cur=0
for i in range(0,n):
	while (len(lis[cur])==0):
		cur-=3
		if(cur<0):break
	if(cur<0):break
	if(len(lis[cur])!=0):
		ans.append(lis[cur].pop())
		cur+=1

if(len(ans)==n):
	print "Possible"
	for i in range(0,n):
		print ans[i],
else:
	print "Impossible"

然后又试敲了下CF Round 299 div.1。

A

TLE了,可能是语言本身效率不高的原因,也可能是没实现好。注意函数是怎么使用的。

A, B, n = map(int,raw_input().split())

def calc(l, r):
	re = (A+(l-1)*B + A+(r-1)*B) * (r-l+1) / 2;
	return re

for i in range(0,n):
	l, t, m = map(int,raw_input().split())
	
	maxR = (t-A)/B+1
	tot = t*m
	if(maxR<l):
		print -1
	else:
		le=l
		ri=ans=mid=maxR
		while(le<=ri):
			mid=(le+ri)>>1
			if(calc(l,mid)<=tot):
				le=mid+1
				ans=mid
			else:
				ri=mid-1
		print ans

B

实现了一下KMP算法,数组下标从0开始有点蛋疼(不知道怎么方便地弄成从1开始)。另外就是似乎在运行过程中,中间结果溢出了32位整型,但是并没出现爆32位整型的问题。

n, m = map(int,raw_input().split())
p = raw_input()
lenp=len(p)
next = [-1 for i in range(lenp)]

# calc next array
k=-1
for i in range(1,lenp):
	while(p[i]!=p[k+1]):
		k=next[k]
		if(k==-1):break;
	if(p[i]==p[k+1]):k+=1
	next[i]=k
#calc end	

if(m): y = map(int,raw_input().split())
else: y=[]
for i in range(m):y[i]-=1

s = [0 for i in range(n)]
if(m>0):
	for i in range(y[0],y[0]+lenp):
		s[i]=1

for i in range(1,m):
	if(y[i]-y[i-1]<lenp):
		target = lenp-(y[i]-y[i-1])-1
		ok=0
		cur=lenp-1
		while(True):
			cur = next[cur]
			if(cur==-1):break
			if(cur==target):
				ok=1
				break
		if(ok==1):
			for j in range(y[i-1]+lenp,y[i]+lenp):s[j]=1
		else:
			print 0
			exit()
	else:
		for j in range(y[i],y[i]+lenp):s[j]=1

ans=1
for i in range(0,n):
	if(not s[i]):
		ans*=26
		ans%=1000000007
print ans




您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值