Codeforces Round #620 (Div. 2)(ABC) 2020.02.16

A

题目

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 x, 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 a, 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≤109, 1≤a,b≤109) — 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.

翻译

由于厌倦了参加太多的Codeforces回合,Gildong决定去公园休息一下。他坐在一条长凳上,很快他发现两只兔子在周围跳来跳去。其中一只兔子比另一只高。
他注意到两只兔子正朝对方跳过来。两个兔子的位置可以用水平线上的整数坐标来表示。较高的兔子当前在位置x上,较矮的兔子当前在位置y上(x<y)。每一秒,每只兔子都跳到另一个位置。较高的兔子向a的正方向跳跃,较矮的兔子向b的负方向跳跃。
例如,假设x=0, y=10, a=2, b=3。在第1秒,每只兔子将在位置2和7。在第2秒,两只兔子都在4号位置。
Gildong现在在想:这两只兔子会在同一时刻处于同一位置吗?如果是的话,需要多长时间?让我们找一个时间点(以秒为单位),在此之后兔子将在同一点。

输入

每个测试包含一个或多个测试用例。第一行包含测试用例的数量t(1≤t≤1000)。
每个测试用例只包含一行。这条直线由4个整数x, y, a,b(0≤x<y≤109,1≤a,b≤109)组成,分别是较高的兔子当前位置,较矮的兔子当前位置,较高的兔子的跳跃距离,较矮的兔子的跳跃距离。

输出

对于每个测试用例,打印单个整数:两只兔子到达相同位置所需的秒数。
如果两只兔子永远不会同时在同一位置,印- 1。

例子

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

大意

有两个人,a、b,a可以跳x米,b能跳y米,从两边开始跳。
若在n秒后,能够站在同一位置,则输出n;
否则,输出“-1”。

思路

这道题唯一需要考虑到的就是超限问题。
若是以循环,用 d= 路程 - n*(x+y),判断 ( 路程 - d )是否为零,则会超限。
若使用除法,考虑是否有余数,则不会超限。

代码

#include <iostream>

using namespace std;

int main()
{
    int t=0;
    cin >> t;
    while(t--)
    {
        int x=0,a=0,y=0,b=0;
        cin >> x >> a >> y >> b;
        int d=a-x;

        int n=0;
        if(d%(y+b)==0)
        {
            n=d/(y+b);
            cout << n << endl;
        }
        else
        {
            cout << "-1" <<endl;
        }

    }
    return 0;
}

B

题目

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 m. 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 n and m (1≤n≤100, 1≤m≤50) — the number of strings and the length of each string.

Next n lines contain a string of length m 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.

翻译

他了解到回文是一种与其相反的字符串。例如,字符串“pop”、“noon”、“x”和“kkkkkk”是回文,而字符串“moon”、“tv”和“abab”不是回文。空字符串也是回文。

输入
第一行包含两个整数n和m(1≤n≤100,1≤m≤50)-字符串的数量和每个字符串的长度。
接下来的n行包含一个长度为m的字符串,只包含小写的拉丁字母。所有字符串都是不同的。

输出
在第一行,打印你所做的最长的回文字符串的长度。
在第二行,打印回文。如果有多个答案,请打印其中一个。如果回文是空的,打印空行,或者根本不打印这一行。

例子

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

大意

给你n个字符串,问你这n个字符串能组成的最大回文串长度是多少。
回文问题。若本身就是回文数列,则放在中间。

代码

#include <bits/stdc++.h>

using namespace std;

string s[110];

int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n, m;
    map<string, int> mp;
    cin >> n >> m;//行、列数
    for (int i = 1; i <= n; ++i)
    {
        cin >> s[i];
        ++mp[s[i]];
    }
    string ans = "";
    string aans = "";
    for (int i = 1; i <= n; ++i)
    {
        string p = s[i];
        reverse(p.begin(), p.end());//翻转
        if (mp[p])
        {
            if (p == s[i])//判断是否回文
            {
                if (p.length() > aans.length())
                    aans = p;
            }
            else
                ans =ans + s[i];
            --mp[s[i]];
        }
    }
    string p = ans;
    p += aans;
    reverse(ans.begin(), ans.end());
    p += ans;
    cout << p.length() << endl;
    cout << p << endl;
    return 0;
}

C

题目

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 i-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, −109≤m≤109), where n is the number of reserved customers and m 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≤109, −109≤li≤hi≤109), 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).

翻译

Gildong拥有一家bulgogi餐厅。这家餐馆有很多顾客,所以很多人喜欢在去之前先预订。
Gildong非常努力地想让顾客满意,他甚至记住了所有顾客喜欢的温度范围!他翻阅着订餐单,想通过控制餐厅的温度来满足所有的顾客。
这家餐厅的空调有三种状态:关、暖、冷。当它关闭时,餐厅的温度保持不变。当它加热时,温度每分钟增加1。最后,当它冷却时,温度每分钟下降1。Gildong可以在任意整数分钟内任意次数地改变状态。空调一开始是关着的。
每个顾客都有三个特征值:ti——第i个顾客光顾餐馆的时间(以分钟为单位),li——他们喜欢的温度范围的下限,hi——他们喜欢的温度范围的上限。
顾客只要一到餐厅,温度就在他们喜欢的范围内,就会感到满意。正式地说,第i个客户是满意的,当且仅当第i分钟的温度在li和hi(包括hi)之间。
给定初始温度、预订客户的访问时间列表和他们喜欢的温度范围,您将帮助他查找是否可以满足所有客户。

输入

每个测试包含一个或多个测试用例。第一行包含测试用例q的数量(1≤q≤500)。测试用例的描述如下。
每个测试用例的第一行包含两个整数n和m(1≤n≤100,−109≤m≤109),其中n为预定人数,m为餐厅的初始温度。
接下来是n行。其中第i行包含三个整数ti, li, hi(1≤ti≤109,−109≤li≤hi≤109),其中ti为第i个客户来访的时间,li为其首选温度范围的下界,hi为其首选温度范围的上界。首选的温度范围包括在内。
客户的访问时间以非递减顺序给出,当前时间为0。

输出

对于每个测试用例,如果可能满足所有客户,则打印“YES”。否则,打印“不”。
您可以打印每个字母在任何情况下(大写或小写)。

例子

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

大意

输入几个例子。每个例子都是有几个预定人数,房间初始温度是多少。
每个预定客人什么时候来,温度的上、下限。

思路

根据每个客人到来的时间差,调整温度,看是否满足客人的喜好。
第i个人温度在【ti,hi】之间,经过 t s,温度在【ti + t,hi - t】之间,
与下一位客人的舒适温度取交集,若有,则继续;若无,则没有。
取得的交集与下一位可以继续以上的方式,求得结果。

代码

借鉴了这位大佬的代码

#include <bits/stdc++.h>

#define max(a,b) (((a)>(b))?(a):(b))
#define min(a,b) (((a)<(b))?(a):(b))
#define def 110

long t[def],x[def],y[def];

int main()
{
    long N,n,m,l,r,i;
    bool ans;
    scanf("%ld",&N);
    while(N--)
    {
        scanf("%ld%ld",&n,&m);
        l=r=m;
        ans=true;
        for(i=1; i<=n; i++)
        {
            scanf("%ld%ld%ld",&t[i],&x[i],&y[i]);
            l-=t[i]-t[i-1];
            r+=t[i]-t[i-1];
            if(l<=y[i]&&r>=x[i])
            {
                l=max(l,x[i]);
                r=min(r,y[i]);
            }
            else
                ans=false;
        }
        if(ans)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值