【2020CF#620 div2-题解】A,B,C题
A. Two Rabbits
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.
He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position xx, and the shorter rabbit is currently on position y (x<y). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by aa, and the shorter rabbit hops to the negative direction by b.

For example, let's say x=0, y=10, a=2, and b=3. At the 1-st second, each rabbit will be at position 2 and 7. At the 2-nd second, both rabbits will be at position 4.
Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1≤t≤1000).
Each test case contains exactly one line. The line consists of four integers x, y, a, b (0≤x<y≤10^9, 1≤a,b≤10^9) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.
Output
For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.
If the two rabbits will never be at the same position simultaneously, print −1.
Example
input
5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1
output
2
-1
10
-1
1
Note
The first case is explained in the description.
In the second case, each rabbit will be at position 3 and 7 respectively at the 1-st second. But in the 2-nd second they will be at 6 and 4respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.
【题目大意】
给定两个兔子的坐标x,y(x<y),以及他们每秒钟能够跳的步数a,b,问两只兔子多少秒钟能在同一个地方相遇,如果不能输出-1。
【思路分析】
数学题,只要满足(y-x)是(a+b)的倍数就可以相遇。
【参考代码】
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x,y,a,b,t;
scanf("%d",&t);
while(t--){
scanf("%d %d %d %d",&x,&y,&a,&b);
if((y-x)%(a+b)!=0) printf("-1\n");
else printf("%d\n",(y-x)/(a+b));
}
return 0;
}
B. Longest Palindrome
Returning back to problem solving, Gildong is now studying about palindromes. He learned that a palindrome is a string that is the same as its reverse. For example, strings "pop", "noon", "x", and "kkkkkk" are palindromes, while strings "moon", "tv", and "abab" are not. An empty string is also a palindrome.
Gildong loves this concept so much, so he wants to play with it. He has n distinct strings of equal length mm. He wants to discard some of the strings (possibly none or all) and reorder the remaining strings so that the concatenation becomes a palindrome. He also wants the palindrome to be as long as possible. Please help him find one.
Input
The first line contains two integers nnand mm (1≤n≤100, 1≤m≤50) — the number of strings and the length of each string.
Next n lines contain a string of length mm each, consisting of lowercase Latin letters only. All strings are distinct.
Output
In the first line, print the length of the longest palindrome string you made.
In the second line, print that palindrome. If there are multiple answers, print any one of them. If the palindrome is empty, print an empty line or don't print this line at all.
Examples
input
3 3
tab
one
bat
output
6
tabbat
input
4 2
oo
ox
xo
xx
output
6
oxxxxo
input
3 5
hello
codef
orces
output
0
input
9 4
abab
baba
abcd
bcde
cdef
defg
wxyz
zyxw
ijji
output
20
ababwxyzijjizyxwbaba
Note
In the first example, "battab" is also a valid answer.
In the second example, there can be 4 different valid answers including the sample output. We are not going to provide any hints for what the others are.
In the third example, the empty string is the only valid palindrome string.
【题目大意】
给定n个长度为m的字符串序列,求问可以任意去掉字符串以及任意组合,组成的最长回文串的长度和信息
【思路分析】
这一道题曾想过思考怎么用dfs,但是感觉复杂度太大。后来突然灵光一现,仔细分析了样例和题面之后发现了一些玄机。
可以仔细看一下样例4:ababwxyzijjizyxwbaba
由于回文串的前缀和后缀是一致的,我称为是回文匹配的。
那么算法开始了:
1.那么我们可以在n个字符串里面去找,有多少对字符串是回文匹配的,个数记为cnt。
2.在输入的过程中,记录一下哪些是自我回文的,用一个数组来记录一下位置。
3.那么最终的回文串的长度S,如果所有自我回文的串都被匹配了,那么$$ S=cnt \times 2\times m $$,如果有一个自我回文的串str没有被匹配,那么$$ S=cnt \times 2\times m+str.length() $$
【参考代码】
代码写的有点杂乱,就不放了。
C. Air Conditioner
Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it.
Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant.
The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially.
Each customer is characterized by three values: ti — the time (in minutes) when the i-th customer visits the restaurant, li — the lower bound of their preferred temperature range, and hi — the upper bound of their preferred temperature range.
A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the ii-th customer is satisfied if and only if the temperature is between li and hi(inclusive) in the ti-th minute.
Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers.
Input
Each test contains one or more test cases. The first line contains the number of test cases q (1≤q≤500). Description of the test cases follows.
The first line of each test case contains two integers n and m (1≤n≤100, −10^9 ≤m≤10^9), where n is the number of reserved customers and mm is the initial temperature of the restaurant.
Next, n lines follow. The i-th line of them contains three integers ti, li, and hi (1≤ti≤10^9$$, $$−10^9 ≤li≤hi≤10^9), where ti is the time when the i-th customer visits, li is the lower bound of their preferred temperature range, and hi is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive.
The customers are given in non-decreasing order of their visit time, and the current time is 0.
Output
For each test case, print "YES" if it is possible to satisfy all customers. Otherwise, print "NO".
You can print each letter in any case (upper or lower).
Example
input
4
3 0
5 1 2
7 3 5
10 -1 0
2 12
5 7 10
10 16 20
3 -100
100 0 0
100 -50 50
200 100 100
1 100
99 -100 0
output
YES
NO
YES
NO
Note
In the first case, Gildong can control the air conditioner to satisfy all customers in the following way:
- At 0-th minute, change the state to heating (the temperature is 0).
- At 2-nd minute, change the state to off (the temperature is 2).
- At 5-th minute, change the state to heating (the temperature is 2, the 1-st customer is satisfied).
- At 6-th minute, change the state to off (the temperature is 3).
- At 7-th minute, change the state to cooling (the temperature is 3, the 2-nd customer is satisfied).
- At 10-th minute, the temperature will be 0, which satisfies the last customer.
In the third case, Gildong can change the state to heating at 0-th minute and leave it be. Then all customers will be satisfied. Note that the 1-st customer's visit time equals the 2-nd customer's visit time.
In the second and the fourth case, Gildong has to make at least one customer unsatisfied.
【题目大意】
多组数据
餐厅有一台空调,有制冷、关机、制热的功能。开启制冷,每个单位时间温度降低1;开启关机,温度不变;开启制热,每个单位时间温度增加1.
初始时间t=0
给定餐厅的到餐厅的人数n,初始化温度m(我的程序中是p)
接下来给n个客人的到达时间\(ti\),温度下限li,温度上限ri。
问是否空调能满足所有客人适宜的温度区间,能就输出“YES“,否则”NO“。
【思路分析】
贪心问题。设d为相邻两个客人直接的时间差,low为当前温度为m的情况下,d时间能降低到的温度,up为当前温度为m的情况下,d时间能上升到的温度。那么我们就有了一个餐厅能调节的温度区间:[low,up]。
接下来贪心的做,按顾客到达的时间顺序依次满足。
对于每位顾客的温度区间[ \(l[i],r[i]\) ],都与[low,up]取交集,如果有交集,更新\(low=max(low,l[i]),up=min (up,r[i])\);如果没有交集,就直接break,输出NO。
【参考代码】
#include <bits/stdc++.h>
using namespace std;
const int N =1000;
long long n,p;
long long t[N],l[N],r[N];
long long low,up;
int main()
{
int T;
cin>>T;
while(T--){
scanf("%lld %lld",&n,&p);
for(int i=1;i<=n;i++)
scanf("%lld %lld %lld",&t[i],&l[i],&r[i]);
t[0]=0;
low=up=p;
int flag=1;
for(int i=1;i<=n;i++){
long long d=t[i]-t[i-1];
low-=d,up+=d;
if(low>up){
flag=0;
cout<<"NO"<<endl;
break;
}
if(up<l[i]||low>r[i]) {
flag=0;
cout<<"NO"<<endl;
break;
}
low=max(l[i],low);
up=min(r[i],up);
}
if(flag) cout<<"YES"<<endl;
}
}

浙公网安备 33010602011771号