A - Odd Palindrome
原题面
描述
We say that a string is odd if and only if all palindromic substrings of the string have odd length.
Given a string s
, determine if it is odd or not.
A substring of a string s
is a nonempty sequence of consecutive characters from s
. A palindromic substring is a substring that reads the same forwards and backwards.
输入
The input consists of a single line containing the string s
(
It is guaranteed that s consists of lowercase ASCII letters (‘a
’–‘z
’) only.
输出
If s
is odd, then print “Odd.
” on a single line (without quotation marks). Otherwise, print “Or not.
” on a single line (without quotation marks).
时空限制
- 时间限制:
- 空间限制:
样例
输入 | 输出 |
---|---|
amanaplanacanalpanama |
Odd. |
madamimadam |
Odd. |
annamyfriend |
Or not. |
nopalindromes |
Odd. |
题解
题意
给定字符串s
,判断s
中所有回文子串长度是否都为奇数。
思路
暴力枚举s
的所有子串,并判断其是否为回文字符串以及其长度是否为奇数。
时间复杂度
代码
1 | def palindromic(s: str) -> bool: |
E. Straight Shot
原题面
描述
You have a toy robot that walks straight at a constant speed
The direction that robot is facing is not changed by the sidewalks; the robot will face in the same orientation for the entire duration of its walk. These sidewalks are aligned with the
One final caveat: You don’t want the toy robot to walk for too long. If the robot cannot reach the destination in at most twice the time it would take in the absence of all moving sidewalks (i.e.,
输入
The first line consists of three space-separated numbers
Each of the next
输出
If the robot cannot reach the destination in at most twice the time it would take in the absence of all moving sidewalks, output “Too hard
” on a single line (without quotation marks).
Otherwise, output, on a single line, the travel time of the robot from the start to the destination, rounded and displayed to exactly three decimal places.
时空限制
- 时间限制:
- 空间限制:
样例
输入 | 输出 |
---|---|
1 806873 66 |
15055.988 |
2 422193 100 |
5043.896 |
1 670764 22.4 |
Too hard |
题解
题意
- 求一质点从
恰好运动到 的时间。 - 在起点
处可设定其速度 的方位角(从 轴正方向起顺时针转过的角度 ,其中 ),之后不可再改变该角度。 - 在质点向
运动的过程中,若质点当前坐标 满足 ,则会叠加上平行于 轴的速度矢量 (以 轴正方向为 的正方向)。 - 质点全程分段做匀速直线运动。
思路
- 将初速度
正交分解到平行于坐标轴方向,以坐标轴的正方向为分速度的正方向,分别为 方向上的速度 与 方向上的速度 。 - 当质点位于传送带(sidewalk)上时,
方向上的速度改变为 。 - 由于要求恰好运动到
,则需要保证 方向上的位移为 。 - 质点位于第
个传送带上时,通过该传送带(横坐标不在该传送带区间内)的时间只与 方向上的速度有关,为 。 - 在上述
的时间内 方向上的位移为 。 - 该部分位移累计
。 - 令
、 ,则 。 - 注意到常数
实际表示所有传送带的总长度,故不在传送带上的总长度为 ,通过时间为 。 - 在上述
的时间内 方向上的位移为 。 - 故
方向上的总位移为 。 - 令
,由于 ,则 , 。 - 若
,则可计算时间 。
时间复杂度
代码
1 | from math import sqrt |
L - Delayed Work
原题面
描述
You own a company that hires painters to paint houses. You can hire as many painters as you want, but for every painter you hire, you have to pay
All painters paint at the same rate. One painter can finish painting the house in
What is the minimum amount of money you will have to pay, including what you pay to the painters and the cost penalty, to get a house painted?
输入
The input consists of a single line containing three space-separated integers
输出
Print, on a single line, the minimum cost to have the house painted, rounded and displayed to exactly three decimal places.
时空限制
- 时间限制:
- 空间限制:
样例
输入 | 输出 |
---|---|
31 41 59 |
549.200 |
3 4 5 |
16.000 |
题解
题意
雇
思路
- 目标函数为
,其中 为正整数。 - 根据基本不等式,得
(当且仅当 ,即 时取等号)。 - 最小花费则为
与中的较小者。
时间复杂度
代码
1 | from math import sqrt |
O - Halfway
原题面
描述
A friend of yours has written a program that compares every pair of a list of items. With n items, it works as follows. First, it prints a 1
, and it compares item 2
, and compares item
Your friend wants to know when his program is halfway done. For a program that makes an odd number of total comparisons, this is when it is doing the middle comparison. For a program that makes an even number of total comparisons, this is when it is doing the first of the two middle comparisons.
What will the last number printed be when the program is halfway done?
Note that since the earlier items have more comparisons than the later items, the answer is not
simply
输入
The input consists of a single line containing the integer
输出
Print, on a single line, the last number your friend’s program prints when it is halfway done.
时空限制
- 时间限制:
- 空间限制:
样例
输入 | 输出 |
---|---|
4 |
1 |
题解
题意
输出下列C++程序的最后一个输出的值。
1 |
|
思路
- 比较总次数
;中间次比较次数 。 - 当外层循环执行第
次时,内层循环最多会将 cnt
的值加上。 - 问题可转化为求满足
的最小正整数 。 - 变形得
。 - 二次函数
的对称轴为直线 ,故答案在区间 上单调递减。因此可以使用二分答案法进行求解。 - 由于当
取 ,即外层循环执行到第 次时,内层循环并不会改变 cnt
的值,因此答案不会为。
时间复杂度
代码
1 | n = int(input()) |
Q - Unloaded Die
原题面
描述
Consider a six-sided die, with sides labeled
die.
For any die, define the expected result of rolling the die to be equal to the average of the values of the sides, weighted by the probability of those sides coming up. For example, all six sides of a fair die are equally likely to come up, and thus the expected result of rolling it is
You are given a loaded die, and you would like to unload it to make it more closely resemble a fair die. To do so, you can erase the number on one of the sides, and replace it with a new number which does not need to be an integer or even positive. You want to do so in such a way that
- The expected result of rolling the die is
, just like a fair die. - The difference between the old label and the new label on the side you change is as small as possible.
输入
The input consists of a single line containing six space-separated nonnegative real numbers
It is guaranteed that the given numbers will sum to
输出
Print, on a single line, the absolute value of the difference between the new label and old label, rounded and displayed to exactly three decimal places.
时空限制
- 时间限制:
- 空间限制:
样例
输入 | 输出 |
---|---|
0.16666 0.16667 0.16667 0.16666 0.16667 0.16667 |
0.000 |
0.2 0.2 0.1 0.2 0.2 0.1 |
1.000 |
题解
题意
给出一个六面骰子(点数为
思路
- 未修改前的期望为
。 - 设被修改的面原点数为
,修改后点数为 。 - 修改后的期望为
。 - 修改差值为
。 - 为使差值最小,取最大的
即可。
时间复杂度
代码
1 | v = [float(x) for x in input().split()] |
R - Star Arrangements
原题面
描述
The recent vote in Puerto Rico favoring United States statehood has made flag makers very excited. An updated flag with 1
2
3
4
5
6
7
8
9* * * * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
This pattern has the property that adjacent rows differ by no more than one star. We represent this star arrangement compactly by the number of stars in the first two rows: 6,5
.
A 9,8
). Conversely, if a state were to leave the union, one appealing representation would be seven rows of seven stars (7,7
).
A flag pattern is visually appealing if it satisfies the following conditions:
- Every other row has the same number of stars.
- Adjacent rows differ by no more than one star.
- The first row cannot have fewer stars than the second row.
Your team sees beyond the short-term change to
输入
The input consists of a single line containing the integer
输出
On the first line, print
This list of compact representations should be printed in increasing order of the number of stars in the first row; if there are ties, print them in order of the number of stars in the second row. The cases
The compact representations must be printed in the form “x,y
”, with exactly one comma between x
and y
and no other characters.
时空限制
- 时间限制:
- 空间限制:
样例
输入 | 输出 |
---|---|
3 |
3: |
50 |
50: |
51 |
51: |
题解
题意
将总共
思路
- 分以下三种情况讨论:
- 一行
个、一行 个为一组,总共 组。 - 一行
个、一行 个为一组,总共 组,最后多出一行 个。 - 一行
个为一组,总共 组。 - 注意
且 。
时间复杂度
代码
1 | s = int(input()) |
S - Forbidden Zero
原题面
描述
You’re writing the positive integers in increasing order starting from one. But you’ve never learned the digit zero, and thus omit any number that contains a zero in any position. The first ten integers you write are:
You have just written down the integer
输入
The input consists of a single line containing the integer
输出
Print, on a single line, the next integer you will be writing down.
时空限制
- 时间限制:
- 空间限制:
样例
输入 | 输出 |
---|---|
99 |
111 |
1234 |
1235 |
题解
题意
求大于
思路
枚举大于
时间复杂度
代码
1 | def hasZero(x: int) -> bool: |
- 本文标题:2017-2018 ACM-ICPC Pacific Northwest Regional Contest - 部分水题题解
- 本文作者:myetyet
- 创建时间:2020-08-08 11:03:25
- 本文链接:https://myetyet.github.io/posts/1fa445b6/
- 版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!