题目链接

文章目录

  • A. Dense Array
  • B. Balanced Remainders
  • C. Sum of Cubes
  • D. Permutation Transformation
  • E. Accidental Victory
  • F - Equalize the Array

A. Dense Array

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=0ans=0for 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/2ans+=1print(ans)

B. Balanced Remainders

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]*3cnt=0for i in ls:ls1.append(i%3)for i in ls1:res[i]+=1ave=sum(res)//3while res[0]!=res[1] or res[0]!=res[2] or res[1]!=res[2]:if res[0]<ave:res[0]+=1res[2]-=1elif res[1]<ave:res[1]+=1res[0]-=1elif res[2]<ave:res[2]+=1res[1]-=1cnt+=1print(cnt)

C. Sum of Cubes

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 = Falsefor c1 in cubSet:if (x - c1) in cubSet:res = Trueprint("YES")breakif not res:print("NO")

D. Permutation Transformation

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]*nbuild(0,n-1,0)print(" ".join(map(str,d)))

E. Accidental Victory

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 = 0start=0for 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+1ans=[]for j in range(start,len(arr)):ans.append(arr[j][0])ans.sort()print(len(ans))print(*ans)

F - Equalize the Array

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,0for j in range(len(ls)):cost = (len(ls)-j)*ls[j]if cost > ans:ans = costprint(n - ans)

Codeforces Round #702 (Div. 3)补题相关推荐

  1. Codeforces Round #807 (Div. 2)补题

    C. Mark and His Unfinished Essay https://codeforces.com/contest/1705/problem/C 会卡long long,下面解法62ms过 ...

  2. Codeforces Round #723 (Div. 2)补题

    水题,只需要将序列分成两部分即可,一部分是大的,一部分是小的. #include <cstdio> #include <iostream> #include <algor ...

  3. Codeforces Round #787 (Div. 3)补题

    目录: 官网链接 E. Replace With the Previous, Minimize F. Vlad and Unfinished Business G. Sorting Pancakes ...

  4. Codeforces Round #702 (Div. 3)A-G题解

    Codeforces Round #702 (Div. 3)A-G题解 比赛链接:https://codeforces.ml/contest/1490 这场F读错题意白给一发,G二分的if(dp[mi ...

  5. Codeforces Round #702 (Div. 3)解题报告

    Codeforces Round #702 (Div. 3) 全部题解 读错题意,写了半天真是心态爆炸,总的来看这次题目不难的. A. Dense Array http://codeforces.co ...

  6. Codeforces Round 700 (Div. 2) B题 英雄杀怪兽

    Codeforces Round 700 (Div. 2) B题 链接: https://codeforces.com/contest/1480/problem/B 大致意思: n组数据,每组数据的第 ...

  7. codeforces round div2,3周赛补题计划(从开学到期末)

    1. 本学期场次 从2020.09.19-2021.01.18,一共18周. 题号 场次 日期 备注 1475 Codeforces Round #697 (Div. 3) 1.25 1474 Cod ...

  8. Codeforces Round #774 (Div. 2)E题题解

    Codeforces Round #774 (Div. 2) E. Power Board 题目陈述 有一个n×m(1≤n,m≤106)n\times m(1\le n,m\le10^6)n×m(1≤ ...

  9. Codeforces Round #702 (Div. 3)——B

    链接:https://codeforces.com/contest/1490 解析:此题的思路很简单,分别算出C0.C1.C2的值,最终使C0=C1=C2=n/3即可 注意:C0只能+1,即C0转化为 ...

最新文章

  1. Nginx从安装到高可用,一篇搞定!
  2. ROS kinetic安装、Kinect2驱动安装和配置
  3. 决策树算法(二)——构建数据集
  4. 《Nature》上给青年科研工作者的几条忠告 (转载)
  5. 进程间通信之分别用共享内存和信号量实现卖票
  6. 51与PC通信协议设计及实现(三):51部分模块化分工及设计
  7. html鼠标各种坐标,各种MOUSE鼠标形状的表示方法
  8. matlab中英文文献,matlab外文文献
  9. 游戏加速外挂的原理是什么 ?
  10. 金钏跳井,凸显贾府主子冷血,下人们离心离德是必然。
  11. 匿名方法和Lambda表达式
  12. 易语言调用大漠Ocr文字识别游戏角色坐标
  13. 中文名字和英文名字正则匹配
  14. 自控原理学习笔记-反馈控制系统的动态模型(4)-频率特性函数Nyquist图及Bode图
  15. AMBER免费申请流程
  16. Linux进程创建fork、进程退出exit()、进程等待waitpid()
  17. css 设置背景图一半_css怎么背景图片显示不全?
  18. OLAP和数据立方体
  19. 网规第二版:第8章 网络规划与设计论文学习笔记(含历年真题)(完结)
  20. 怎么分割视频,一个视频如何剪切成多个

热门文章

  1. 为什么https比http更安全?_货车拉钢卷为什么都是立式运输,平放不是更安全吗?...
  2. 计算机网络-15 网络测量
  3. 有些框架自动重写html标签,充分利用HTML标签元素 – 简单的xtyle前端框架
  4. 基于yolov5的目标检测和模型训练(Miniconda3+PyTorch+Pycharm+实战项目——装甲板识别)
  5. 哈尔滨工业大学考研 网络与空间安全 837 资料库
  6. CSS系列之美化网页/span标签和div标签/字体样式/文本样式
  7. 贪心算法1——找零钱问题
  8. PLC应用中单按键(自复位)控制启动与停止
  9. 如何使家里wifi信号增强一倍
  10. 惠普136系列打印机:拆封启动、无线驱动安装、电脑手机打印