算法练习系列——2024牛客五一集训派对day3
2024牛客五一集训派对day3
A:Permutation
题目描述
You are given a prime number p, you want to find a permutation of numbers $1,2,…,p−1$, such that for all $i(1≤i≤p−2), x_i+1≡2x_i(mod\ p)\ or\ x_i+1≡3x_i(modp)$.
输入描述:
1 |
|
输出描述:
1 |
|
示例1
输入
1 |
|
输出
1 |
|
思路
这道题目的大致题意就是:让你构造1~p-1的一种排列(p是素数),使得
a[i+1] == a[i]*2%p 或者 a[i+1] == a[i]*3%p
。这样的话其实直接暴力DFS的写法就OK了,但是我们要注意一点,就是每次肯定都是可以从 1 开始的。当然选择其他的应该也是可以的,但是可能有一些数字会需要进行一点特殊处理,所以我们就从最简单的 1 开始枚举就OK了。枚举的流程就是,从2 ~p - 1
,枚举到某个位置的时候我们就看看我们需要使用的a[i - 1] * 2 % p 或者 a[i - 1] * 3 % p
是否之前使用过,如果用过的话我们就可以判断序列无解了。
代码
1 |
|
E:Game
题目描述
There are n columns of blocks standing in a row. The i-th column has aia_iai blocks in the beginning. Each block has size 1×1×1. Define (x,y) represent the block at column x and is the y-th block from bottom to top. You can perform one operation:
- Push one block to the left, that means you choose one block which has no block at its right and make it move to the left. Because of the friction, the block above it will also move to the left, and because the blocks cannot intersect, the block at its left will move to the left either. This will cause a chain reaction. After every block moved, if some blocks hang in the air, then it will fall because of gravitation. Note that the blocks at column 1 can’t move to the left, so if a movement will cause a block at column 1 move, you can’t perform this operation.
Formally, let bib_ibi be the number of blocks in the i-th column now, then you can choose block (x,y) that satisfy$b_x\geq b_y$ and $b_{x+1}<y$(or $x=n$ ). let l be the greatest position that satisfies $1\leq l<x$ and $b_l<y$, then you can perform this operation as long as l exists. Then for all blocks $(i,j)$ that satisfy $i\leq x$ and $j\geq y$, it moves to (i-1,j). After that, for blocks (x,y) (y>1) that there are no blocks in $(x,y-1)$, it moves to (x,y-1). Repeat doing it until no blocks satisfy the condition.
This shows an operation that pushes the block at (6,4), and the value of l is 3.
The goal of the game is to minimize the height of blocks. You need to operate any times of the operation and minimize $\max\limits_{i=1}^n b_i$, where $b_i$ represents the number of blocks in the end. Output the minimize value.
输入描述:
输出描述:
示例1
输入
1 |
|
输出
1 |
|
思路
题意:
每次操作可以把右边的一行格子向左移动,由于重力格子下落,可以操作任意次,求最小的最高高度。
思路:
二分寻找最为合适的高度。看代码还是很好懂的。
代码一(前綴和 + 二分)
1 |
|
代码二(前缀和)
1 |
|
I:Tournament
题目描述
You are scheduling a tournament. There are n teams. There are $\frac{n(n-1)}{2}$competitions against every pair of teams. You can schedule a competition each day. For each team, it will arrive in the day when its first competition holds, and leave after its last competition finishes.
For example, there are 3 teams and the schedule is $(1,2), (1,3), (2,3)$. The first team will arrive in day 1, and leave in day $2$. It will stay for two days. The second team will stay for three days. The third team will stay for two days.
You want to find a schedule minimizing the sum of days they stay.
输入描述:
1 |
|
输出描述:
1 |
|
示例1
输入
1 |
|
输出
1 |
|
思路
如果每次我们让第一队先走,那么我们会发现标号越大的队伍住的天数越多,这明显是不合理的…
首先我们想到:1队越早走,后面的队伍住的总时间就越长,所以我们要尽量折中每个队伍的住的时间。
这里我们可以大概这么想一下,如果我们先让1走,就是直接先让2,3,4,…,n先和比赛,然后再是剩下的所有队伍互相pk了,按照这样的规则来比赛我们可以得到最大的时间之和。下面我们就要思考如何才可以最小了:我们可以通过枚举得知1队越早走,后面的队伍住的总时间就越长,所以我们要尽量折中每个队伍的住的时间。总之我们的最终目的就是让大家停留时间尽可能平均化。
于是我们就想到如下的构造方法:
- 首先把n个人分成两半
- 然后让前一半的人开始打比赛
- 然后让前一半的人和后一半的人打比赛
- 然后后一半的人开始打比赛
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
暴力: 构造:
1 2 1 2
1 3 1 3
1 4 2 3
1 5 1 4
1 6 2 4
2 3 3 4
2 4 1 5
2 5 2 5
2 6 3 5
3 4 4 5
3 5 1 6
3 6 2 6
4 5 3 6
4 6 4 6
5 6 5 6
代码
1 |
|