Timus #1494. Monobilliards

题目:有n个数按照1,2,3…n的顺序入栈,在每一步入栈的过程中(严格按照入栈顺序,即从先到后),可以按顺序出栈(严格按照出栈顺序,即从后到先),如果可以得到题目所input的顺序,则输出not a proof,否则为cheater。

例子讲解:
input:
4
1 3 2 4
output:
not a proof

题目为1,2,3,4入栈顺序
(1). 1入栈,1出栈;
(2). 2,3入栈,3,2出栈;
(3). 4入栈,4出栈
存在这种可能,输出not a proof

input:
3
3 1 2
output:
cheater

题目为1,2,3入栈顺序
(1). 1,2,3入栈,3 出栈
因为接下来出栈必须为2,所以不存在这种可能,输出cheater

解析: 堆栈

Using the idea of stack. Input numbers (by order 1,2,3,…,n) to stack one by one, remove it out of the stack (by input number’s order) in each insertion. See if the stack is empty in the end.

Timus #1521. War Games 2

题目:约塞夫问题。n个人排成一个圆圈开始报数,报数到k的人出列,输出该人在原队列的位置。

解析:
1. 直接for循环,利用vector和简单的数学计算
2. 线段树解约塞夫环

代码:
1. vector

2. 线段树解约塞夫环

CSDN代码

Timus #1726

题目:有n个人相互访问访问各自的坐标,所有道路正交,沿直线行走,求平均路程。

解析:首先对x,y分别进行排序。每个x[i + 1] – x[i] 会经过 (i + 1) * (n – (i + 1)) * 2次,y也一样,然后进入循环进行计算。

代码及精度问题:使用long long int,不要使用int,在测试样例中会超出int范围。计算过程中加入long long temp;避免在计算过程中强制类型转换造成计算错误。

Timus #1322

题目:Burrows-Wheeler逆变换,具体读题即可读懂

解析:Burrows-Wheeler 压缩(转换)算法(BWT)。

1. 百度百科上的例子:加列,排序循环

2.我也不知道什么原理,我用的是这种方法。vector of inverse transform.

Timus #1444

题目说明:二维数组上有一些点,以第一个点为起点,把剩下的点连起来形成一个没有边相交的多边形,类似于一笔画,无需回到起点,只需把n个点连起来。

题目解析:类似于Timus #1607,极角排序,但排序顺序难想很多。首先找到x轴最小的点作为算法中整个图形的起点。计算每个点的atan2。对atan2进行升序排序,但是其中atan2角度可能相同,你可以画画图想想一下,对相同的距离也进行进行升序排序,距离越大连的越后面。但是我们这道题起点必须为输入的第一个点,这就意味着我们取x轴最小的点作为起的时候需要收尾也要相连,所以如果有大于两个的点位于x轴最小的点的正上方,就会出现收尾不可能相连的错误,这就需要对距离进行降序排序。具体sort函数排序方法可见代码f函数

Problem analysis English version:

  • Find the point with minimum x, exchange it to the first position.
  • Cal atan2.
  • Sort atan2 exclude the point with minimum x which is in the first position.  
  • Find the index which is 1 and output the result.

Difference of sort definition between problem 1207:
Increasing order by theta. When theta is same, use increasing order of the distance from min_x point besides one situation. However if the points have same theta and is directly above the min_x point , we must use descending order of the distance from min_x point.

浙ICP备2021019730-1    浙公网安备 33010902002953号
Copyright © 2024 PanCake