题目链接
Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any i (1≤i≤n−1), this condition must be satisfied:
max(a[i],a[i+1])/min(a[i],a[i+1])≤2
For example, the arrays [1,2,3,4,3], [1,1,1] and [5,10] are dense. And the arrays [5,11], [1,4,2], [6,6,1] are not dense.
You are given an array a of n integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added.
For example, if a=[4,2,10,1], then the answer is 5, and the array itself after inserting elements into it may look like this: a=[4,2,3,5,10,6,4,2,1] (there are other ways to build such a).
题目大意
在列表中插入数字,使得相邻数字满足最大值除以最小值小于等于2,求插入最小的数字
思路
当相邻数的商大于2就不断除以2直到符合要求就行了
for _ in range(int(input())):
n=int(input())
ls=list(map(int,input().split()))
ls1=[]
a=0
ans=0
for i in range(1,len(ls)):
a=max(ls[i],ls[i-1])/min(ls[i],ls[i-1])
if a>2:
ls1.append(a)
for i in ls1:
while i >2:
i=i/2
ans+=1
print(ans)
You are given a number n (divisible by 3) and an array a[1…n]. In one move, you can increase any of the array elements by one. Formally, you choose the index i (1≤i≤n) and replace ai with ai+1. You can choose the same index i multiple times for different moves.
Let’s denote by c0, c1 and c2 the number of numbers from the array a that have remainders 0, 1 and 2 when divided by the number 3, respectively. Let’s say that the array a has balanced remainders if c0, c1 and c2 are equal.
For example, if n=6 and a=[0,2,5,5,4,8], then the following sequence of moves is possible:
initially c0=1, c1=1 and c2=4, these values are not equal to each other. Let’s increase a3, now the array a=[0,2,6,5,4,8];
c0=2, c1=1 and c2=3, these values are not equal. Let’s increase a6, now the array a=[0,2,6,5,4,9];
c0=3, c1=1 and c2=2, these values are not equal. Let’s increase a1, now the array a=[1,2,6,5,4,9];
c0=2, c1=2 and c2=2, these values are equal to each other, which means that the array a has balanced remainders.
Find the minimum number of moves needed to make the array a have balanced remainders.
题目大意
要求数组元素模3之后余数为0,1,2的个数相同,若不同则可以对每个元素进行加1的操作,求使数组变成目标数组的最小操作次数,如[0,2,5,5,4,8],模3后余数为[0,2,2,2,1,2],则c0=1, c1=1 and c2=4
思路
求出平均数并对c0,c1和c2进行枚举(原本余2的数加1后会余0)直到三个数都等于平均数。
for _ in range(int(input())):
n=int(input())
ls=list(map(int,input().split()))
ls1=[]
res=[0]*3
cnt=0
for i in ls:
ls1.append(i%3)
for i in ls1:
res[i]+=1
ave=sum(res)//3
while res[0]!=res[1] or res[0]!=res[2] or res[1]!=res[2]:
if res[0]<ave:
res[0]+=1
res[2]-=1
elif res[1]<ave:
res[1]+=1
res[0]-=1
elif res[2]<ave:
res[2]+=1
res[1]-=1
cnt+=1
print(cnt)
You are given a positive integer x. Check whether the number x is representable as the sum of the cubes of two positive integers.
Formally, you need to check if there are two integers a and b (1≤a,b) such that a3+b3=x.
For example, if x=35, then the numbers a=2 and b=3 are suitable (23+33=8+27=35). If x=4, then no pair of numbers a and b is suitable.
题目大意
判断一个数是否能拆成两个数的立方和
思路
最容易想到的应该就是打表,在python中这里需要用集合set,因为对于 in 方法,set()的平均时间复杂度是 O(1),远好于 list() 的O(n)。用列表应该会超时
cubSet = set()
for i in range(1, 10001):
cubSet.add(i**3)
for k in range(int(input())):
x = int(input())
res = False
for c1 in cubSet:
if (x - c1) in cubSet:
res = True
print("YES")
break
if not res:
print("NO")
A permutation — is a sequence of length n integers from 1 to n, in which all the numbers occur exactly once. For example, [1], [3,5,2,1,4], [1,3,2] — permutations, and [2,3,2], [4,3,1], [0] — no.
Polycarp was recently gifted a permutation a[1…n] of length n. Polycarp likes trees more than permutations, so he wants to transform permutation a into a rooted binary tree. He transforms an array of different integers into a tree as follows:
the maximum element of the array becomes the root of the tree;
all elements to the left of the maximum — form a left subtree (which is built according to the same rules but applied to the left part of the array), but if there are no elements to the left of the maximum, then the root has no left child;
all elements to the right of the maximum — form a right subtree (which is built according to the same rules but applied to the right side of the array), but if there are no elements to the right of the maximum, then the root has no right child.
For example, if he builds a tree by permutation a=[3,5,2,1,4], then the root will be the element a2=5, and the left subtree will be the tree that will be built for the subarray a[1…1]=[3], and the right one — for the subarray a[3…5]=[2,1,4]. As a result, the following tree will be built
思路
从[l,r]区间扫到最大值下标x,然后递归构造[l,x-1]和[x+1,r]
def build(l, r, depth):
mxid = max(range(l, r + 1), key=lambda a: ls[a])
if mxid > l:
build(l, mxid - 1, depth + 1)
if mxid < r:
build(mxid + 1, r, depth + 1)
d[mxid] = depth
for _ in range(int(input())):
n=int(input())
ls=list(map(int,input().split()))
d=[0]*n
build(0,n-1,0)
print(" ".join(map(str,d)))
A championship is held in Berland, in which n players participate. The player with the number i has ai (ai≥1) tokens.
The championship consists of n−1 games, which are played according to the following rules:
in each game, two random players with non-zero tokens are selected;
the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly);
the winning player takes all of the loser’s tokens;
The last player with non-zero tokens is the winner of the championship.
All random decisions that are made during the championship are made equally probable and independently.
For example, if n=4, a=[1,2,4,3], then one of the options for the game (there could be other options) is:
during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player’s tokens. Now a=[0,2,4,4];
during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now a=[0,2,8,0];
during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player’s tokens. Now a=[0,0,10,0];
the third player is declared the winner of the championship.
Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players.
题目大意
每个玩家都有一个点数,两个人对战,点数大的获胜,相同点数随机一人获胜,获胜后获得败者的点数,问哪些人会有获胜的可能性。
思路
假设数组已经从小到大排好序了。那么容易发现:
1.如果ai打完了所有比他小的后仍然比a(i+1)小,那显然ai是不可能赢的。
2.如果ai不可能赢,那所有比他小的都不可能赢。
故做前缀和,找到最大的i使得ai不可能赢即可。
本题最后输出的是原数组的位置,所以排序前最好就加上索引。
for _ in range(int(input())):
n=int(input())
arr=list(enumerate(list(map(int,input().split())),1))
arr.sort(key=lambda x:x[1])
#求前缀和
pre=[]
sum = 0
start=0
for i in range(len(arr)):
sum+=arr[i][1]
pre.append(sum)
for i in range(0,len(pre)-1):
if pre[i]<arr[i+1][1]:
start=i+1
ans=[]
for j in range(start,len(arr)):
ans.append(arr[j][0])
ans.sort()
print(len(ans))
print(*ans)
Polycarp was gifted an array a of length n. Polycarp considers an array beautiful if there exists a number C, such that each number in the array occurs either zero or C times. Polycarp wants to remove some elements from the array a to make it beautiful.
For example, if n=6 and a=[1,3,2,1,4,2], then the following options are possible to make the array a array beautiful:
Polycarp removes elements at positions 2 and 5, array a becomes equal to [1,2,1,2];
Polycarp removes elements at positions 1 and 6, array a becomes equal to [3,2,1,4];
Polycarp removes elements at positions 1,2 and 6, array a becomes equal to [2,1,4];
Help Polycarp determine the minimum number of elements to remove from the array a to make it beautiful.
题目大意
最小删除多少个数字使得剩下的数字出现次数都一样。
for _ in range(int(input())):
n=int(input())
ls=list(map(int,input().split()))
d={
}
for word in ls:
d[word]=d.get(word,0)+1
#将出现次数排序
ls= sorted(d.values())
cost,ans = 0,0
for j in range(len(ls)):
cost = (len(ls)-j)*ls[j]
if cost > ans:
ans = cost
print(n - ans)