A - Exam
原题面
描述
Your friend and you took a true/false exam of
Compute the maximum number of questions you could have gotten correctly.
输入
The first line of input contains a single integer
The second line contains a string of T
’ or an ‘F
’.
The third line contains a string of n characters, the answers your friend wrote down. Each letter is either a ‘T
’ or an ‘F
’.
The input will satisfy
输出
Print, on one line, the maximum number of questions you could have gotten correctly.
时空限制
- 时间限制:
- 空间限制:
样例
输入 | 输出 |
---|---|
3 |
2 |
6 |
9 |
题解
题意
已知自己的和朋友的判断题每题所选的答案,以及朋友的答案中正确的个数(具体是哪些题未知),求自己最多可能答对了多少道判断题。
思路
- 首先需要保证自己与朋友答案一致的题目(数量记为
)为朋友所答对的,因为若有一题朋友答对了而自己的答案与之相反,则自己一定答错了。 - 若
,则需要保证自己与朋友答案一致的其中有 题答对,不一致的题目全为自己对。此时问题答案为 。 - 若
,则需要保证自己与朋友答案一致的两者全答对,不一致的题目中有 道题朋友答对了(即朋友没答对的 题全为自己答对了)。此时答案为 。 - 故最终答案为
。
时间复杂度
代码
1 | k, a, b = int(input()), input(), input() |
C - Contest Setting
原题面
描述
A group of contest writers have written
Compute how many distinct valid contests the contest writers can produce. Two contests are distinct if and only if there exists some problem present in one contest but not present in the other.
Print the result modulo
输入
The first line of input contains two space-separated integers
The next line contains n space-separated integers representing the difficulty levels. The difficulty levels are between
输出
Print the number of distinct contests possible, modulo
时空限制
- 时间限制:
- 空间限制:
样例
输入 | 输出 |
---|---|
5 2 |
10 |
5 2 |
6 |
12 5 |
316 |
题解
题意
有
思路
- 将题目的难度值离散化,因为只需要考虑一共有多少种不同的难度值。设第
种难度值对应有 个题目。 - 考虑动态规划。定义
表示前 种难度值中挑选出 种难度值互不相同的题的方案数。 - 对于每一种难度值的题,有选一道或不选两种情况。
- 若从该种难度值的题目中选出一道,则需要保证前
种难度中恰好选了 种互不相同的难度值,故 。 - 若不选该种难度值的题目,则需要保证前前
种难度中恰好已经选了 种互不相同的难度值,故 。 - 故转移方程为
。
时间复杂度
代码
1 | from collections import Counter |
H - Repeating Goldbachs
原题面
描述
The Goldbach Conjecture states that any even number
Define a Goldbach step as taking
Given
输入
The input will consist of a single integer
输出
Print, on a single line, the number of Goldbach steps it takes to reach a number less than
时空限制
- 时间限制:
- 空间限制:
样例
输入 | 输出 |
---|---|
20 |
3 |
30 |
4 |
40 |
5 |
50 |
6 |
60 |
7 |
70 |
8 |
题解
题意
给定正整数
思路
- 根据题意模拟该过程即可。
- 注意要将
以内的质数预处理出来。
时间复杂度
整个过程理论上是
代码
1 |
|
I - Greedy Scheduler
原题面
描述
A store has
Find which cashier will process each customer’s shopping cart.
输入
The first line of input contains two space-separated integers
输出
Output a single line containing
时空限制
- 时间限制:
- 空间限制:
样例
输入 | 输出 |
---|---|
3 10 |
1 2 3 3 1 2 3 1 2 1 |
题解
题意
思路
- 维护每个收银员的下一个空闲的时间点。
- 对于每个顾客,寻找编号最小的空闲收银员。
- 可以利用优先队列加速寻找最小编号收银员的速度。
时间复杂度
代码
1 | from queue import PriorityQueue |
J - House Numbers
原题面
描述
Peter was walking down the street, and noticed that the street had houses numbered sequentially from
Given
输入
Input consists of a single line containing the integer
输出
On a single line, print
It is guaranteed that there will be a solution with
时空限制
- 时间限制:
- 空间限制:
样例
输入 | 输出 |
---|---|
1 |
1 6 8 |
11 |
11 49 68 |
999999 |
999999 1317141 1571535 |
999000 |
999000 1000000 1000999 |
题解
题意
给定正整数
思路
- 将求和符号展开,得
。 - 展开,整理,得
。 - 变形,得
。将右边记为 。 - 从小到大遍历
,当上述方程中 有正整数解时即为问题答案。 - 若需要保证精度,可采用二分答案法求
的解,避免浮点数运算。此时时间复杂度为 ,也可接受。
时间复杂度
代码
1 |
|
M - Liars
原题面
描述
There are n people in a circle, numbered from
Each person
Compute the maximum number of people who could be telling the truth.
输入
The first line contains a single integer
输出
Print, on a single line, the maximum number of people who could be telling the truth. If the given set of statements is inconsistent, print -1
instead.
时空限制
- 时间限制:
- 空间限制:
样例
输入 | 输出 |
---|---|
3 |
2 |
8 |
-1 |
题解
题意
思路
- 由于输入的
较小,因此可以直接枚举最多可能的说真话的人数。 - 对于每个可能的人数,检查每个人的描述,可以得出某个人是否说了真话,统计个数,与枚举的人数比较即可。
时间复杂度
代码
1 | n = int(input()) |
O - Pizza Deal
原题面
描述
There’s a pizza store which serves pizza in two sizes: either a pizza slice, with area
You want to maximize the amount of pizza you get per dollar. Should you pick the pizza slice or the whole pizza?
输入
The first line contains two space-separated integers
Similarly, the second line contains two space-separated integers
It is guaranteed that all values are positive integers at most
输出
If the better deal is the whole pizza, print ‘Whole pizza
’ on a single line.
If it is a slice of pizza, print ‘Slice of pizza
’ on a single line.
时空限制
- 时间限制:
- 空间限制:
样例
输入 | 输出 |
---|---|
8 4 |
Whole pizza |
9 2 |
Whole pizza |
841 108 |
Slice of pizza |
题解
题意
比较扇形披萨和圆形披萨的单位价格的披萨面积。
思路
直接计算即可。
时间复杂度
代码
1 | import math |
Q - Poker Hand
原题面
描述
You’re given a five-card hand drawn from a standard
The strength of your hand is defined as the maximum value
Find the strength of your hand.
输入
The input consists of a single line, with five two-character strings separated by spaces.
The first character in each string will be the rank of the card, and will be one of A23456789TJQK
. The second character in the string will be the suit of the card, and will be one of CDHS
.
It is guaranteed that all five strings are distinct.
输出
Output, on a single line, the strength of your hand.
时空限制
- 时间限制:
- 空间限制:
样例
输入 | 输出 |
---|---|
AC AD AH AS KD |
4 |
2C 4D 4H 2D 2H |
3 |
AH 2H 3H 4H 5H |
1 |
题解
题意
统计五张扑克牌中点数相同的最大张数。
思路
直接统计即可。
时间复杂度
代码
1 | from collections import Counter |
R - Random Index Vectors
原题面
描述
A Random Index Vector (RIV) is a data structure that can represent very large arrays where most elements are zero. Here, we consider a version of RIVs which can contain only
- Addition of two RIVs
and ; the resulting RIV is defined by . The values are clamped at , i.e., we define and . - Multiplication of two RIVs
and ; the resulting RIV is defined by . - Rotation of an RIV
by some integer ; this shifts all of the values in to the left by indices. The first values at the start of go to the end of the array and become the rightmost values.
An RIV is written in its condensed form. This representation is a list that starts with the number of nonzero values, followed by a sorted list of indices (
For example, consider an RIV representing an array
Given two RIVs in condensed form, add them, multiply them, and rotate them both. Output the results in condensed form.
输入
The first line of input contains two space-separated integers
Each of the next two lines contains a condensed form of an RIV, starting with an integer
输出
Output four vectors, one per line, in condensed form:
- Sum of the two input RIVs.
- Product of the two input RIVs.
- First RIV rotated by
. - Second RIV rotated by
.
时空限制
- 时间限制:
- 空间限制:
样例
输入 | 输出 |
---|---|
30 13 |
12 -1 3 6 7 -9 11 18 19 20 22 26 -27 |
20 4 |
8 -2 5 -8 -10 -12 15 18 19 |
题解
题意
计算两个RIV的和、积,以及两者分别位移
思路
- 和:筛选出所有非零位相加后保留所有非零位。
- 积:筛选出所有非零位相乘后保留所有非零位。
- 位移:所有非零位下标减
,若小于 则再加 。
时间复杂度
代码
1 | from collections import defaultdict |
S - Time Limits
原题面
描述
A contest setter wants to determine the time limits for a given problem. There are
输入
The first line of input contains two space-separated integers
The second line of input contains
输出
Print, on one line, the minimum time limit (in seconds) as a single integer.
时空限制
- 时间限制:
- 空间限制:
样例
输入 | 输出 |
---|---|
2 5 |
2 |
3 4 |
5 |
题解
题意
输出最大运行时间的
思路
直接计算即可。
时间复杂度
代码
1 | from math import ceil |
- 本文标题:2018-2019 ACM-ICPC Pacific Northwest Regional Contest - 部分水题题解
- 本文作者:myetyet
- 创建时间:2020-09-01 14:26:06
- 本文链接:https://myetyet.github.io/posts/cb8f4655/
- 版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!