CF994A - Fingerprints
题目描述
You are locked in a room with a door that has a keypad with 10 keys corresponding to digits from 0 to 9. To escape from the room, you need to enter a correct code. You also have a sequence of digits.
Some keys on the keypad have fingerprints. You believe the correct code is the longest not necessarily contiguous subsequence of the sequence you have that only contains digits with fingerprints on the corresponding keys. Find such code.
输入
The first line contains two integers n and m (1≤n,m≤10) representing the number of digits in the sequence you have and the number of keys on the keypad that have fingerprints.
The next line contains n distinct space-separated integers x1,x2,…,xn (0≤xi≤9) representing the sequence.
The next line contains mm distinct space-separated integers y1,y2,…,ym (0≤yi≤9) — the keys with fingerprints.
输出
In a single line print a space-separated sequence of integers representing the code. If the resulting sequence is empty, both printing nothing and printing a single line break is acceptable.
样例
Input
1 | 7 3 |
Output
1 | 7 1 2 |
Input
1 | 4 4 |
Output
1 | 1 0 |
注意
In the first example, the only digits with fingerprints are 1, 2 and 7. All three of them appear in the sequence you know, 7 first, then 1 and then 2. Therefore the output is 7 1 2. Note that the order is important, and shall be the same as the order in the original sequence.
In the second example digits 0, 1, 7 and 9 have fingerprints, however only 0 and 1 appear in the original sequence. 1 appears earlier, so the output is 1 0. Again, the order is important.
解题思路
送分水题,根据题意可以知道,已知一个原序列和带指纹的数字序列,密码是原序列的一个最长子序列(子序列的数都带指纹)。
这道题目给出三行数据,第一行m和n。第二行m个数字为已知序列,第三行n个数字为带指纹的数字。
我们只需要依次判断m中的数是否在n中出现,若出现则输出。(顺序需要按照原序列的顺序)
AC代码
1 |
|
English单词积累
keypad—键盘
subsequence—子序列
CF994B - Knights of a Polygonal Table
题目描述
Unlike Knights of a Round Table, Knights of a Polygonal Table deprived of nobility and happy to kill each other. But each knight has some power and a knight can kill another knight if and only if his power is greater than the power of victim. However, even such a knight will torment his conscience, so he can kill no more than k other knights. Also, each knight has some number of coins. After a kill, a knight can pick up all victim’s coins.
Now each knight ponders: how many coins he can have if only he kills other knights?
You should answer this question for each knight.
输入
The first line contains two integers n and k (1 ≤ n ≤10^5^, 0 ≤ k ≤ min(n−1, 10) ) — the number of knights and the number k from the statement.
The second line contains n integers p1,p2,…,p (1≤pi≤10^9^) — powers of the knights. All pi are distinct.
The third line contains n integers c1,c2,…,cn (0≤ci≤10^9^) — the number of coins each knight has.
输出
Print n integers — the maximum number of coins each knight can have it only he kills other knights.
样例
Input
1 | 4 2 |
Output
1 | 1 3 46 36 |
Input
1 | 5 1 |
Output
1 | 1 3 5 7 9 |
Input
1 | 1 0 |
Output
1 | 3 |
注意
Consider the first example.
- The first knight is the weakest, so he can’t kill anyone. That leaves him with the only coin he initially has.
- The second knight can kill the first knight and add his coin to his own two.
- The third knight is the strongest, but he can’t kill more than k=2 other knights. It is optimal to kill the second and the fourth knights: 2+11+33=46.
- The fourth knight should kill the first and the second knights: 33+1+2=36.
In the second example the first knight can’t kill anyone, while all the others should kill the one with the index less by one than their own.
In the third example there is only one knight, so he can’t kill anyone.
解题思路
题目说有n个骑士,它们杀人不超过k个,每个人都有power和金币。
只能击杀power比自己小的人,杀掉其他人可以捡别人的金币。求击杀后拥有的金币的最大数量。
这题也不算很难,模拟也能过。起初的思路在第九个测试点超时了,优化了一下sort使用的次数后一直在后面的测试点WA。
反复检查没发现问题,最后才发现没开long long,超出int的范围了。
int在32和64位机器人中占4个字节,表示的范围 -231— 2^31^-1 数量级大概在10^9^。
模拟的思路:给每个骑士一个id,这个id用于最后按照输入的顺序输出答案。
首先给骑士按power由小到大排序。
遍历骑士的数组(关键,一定分情况,否则超时):
如果前面的人的数量小于k,把前面人的金币数量加入coin数组,然后全部杀。
如果前面的人的数量大于k。把coin数组排序,数组中最小的金币数量和最新一个骑士的金币数量比较。取最大的。然后杀k个人。
AC代码
1 |
|
English单词积累
Knights—骑士
Polygonal—多边形
deprived of—剥夺
nobility—贵族
torment—折磨
conscience—良心
ponders—思考
optimal—最优的
CF994C - Two Squares
题目描述
You are given two squares, one with sides parallel to the coordinate axes, and another one with sides at 45 degrees to the coordinate axes. Find whether the two squares intersect.
The interior of the square is considered to be part of the square, i.e. if one square is completely inside another, they intersect. If the two squares only share one common point, they are also considered to intersect.
输入
The input data consists of two lines, one for each square, both containing 4 pairs of integers. Each pair represents coordinates of one vertex of the square.
The first line contains the coordinates of the square with sides parallel to the coordinate axes, the second line contains the coordinates of the square at 45 degrees.
All the values are integer and between −100 and 100.
输出
Print “Yes” if squares intersect, otherwise print “No”.
You can print each letter in any case (upper or lower).
样例
Input
1 | 0 0 6 0 6 6 0 6 |
Output
1 | YES |
Input
1 | 0 0 6 0 6 6 0 6 |
Output
1 | NO |
Input
1 | 6 0 6 6 0 6 0 0 |
Output
1 | YES |
注意
In the first example the second square lies entirely within the first square, so they do intersect.
In the second sample squares do not have any points in common.
Here are images corresponding to the samples:
解题思路
题目给的数据范围很小,因此可以之间暴力枚举图中所有的点。
如果点即在s1又在s2,则证明这两个正方形相交。
AC代码
1 |
|
English单词积累
sides—边
parallel—平行
coordinate axes—坐标轴
intersect—相交
interior—内部
vertex—顶点