Codeforces Round #617 (Div. 3)(题解)

在这里插入图片描述
写在前面: 啊啊啊!又是思路对了写不出来。。。掉分场。。。

A. Array with Odd Sum

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array a consisting of n integers.

In one move, you can choose two indices 1≤i,j≤n such that i≠j and set ai:=aj. You can perform such moves any number of times (possibly, zero). You can choose different indices in different operations. The operation := is the operation of assignment (i.e. you choose i and j and replace ai with aj).

Your task is to say if it is possible to obtain an array with an odd (not divisible by 2) sum of elements.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤2000) — the number of test cases.

The next 2t lines describe test cases. The first line of the test case contains one integer n (1≤n≤2000) — the number of elements in a. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤2000), where ai is the i-th element of a.

It is guaranteed that the sum of n over all test cases does not exceed 2000 (∑n≤2000).

Output
For each test case, print the answer on it — “YES” (without quotes) if it is possible to obtain the array with an odd sum of elements, and “NO” otherwise.

Example
inputCopy
5
2
2 3
4
2 2 8 8
3
3 3 3
4
5 5 5 5
4
1 1 1 1
outputCopy
YES
NO
YES
NO
NO
思路: 如果总数是奇数,则需要里面要有奇数,如果是偶数,则里面需要有奇数又有偶数。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>
 
using namespace std;
 
#define endl '\n'
 
typedef long long ll;
 
int a[20004];
 
int main()
{
    int t;
 
    cin >> t;
 
    while (t--)
    {
        int n;
        cin >> n;
 
        int f1 = -1, f2 = -1;
 
        for (int i = 0; i < n; ++i)
        {
            cin >> a[i];
            if (a[i] % 2 == 1)
                f1 = 1;
            if (a[i] % 2 == 0)
                f2 = 1;
        }
 
        if (n % 2 == 1 && f1 == 1)
        {
            cout << "Yes" << endl;
        }
        else if (n % 2 == 0 && f2 == 1 && f1 == 1)
        {
            cout << "Yes" << endl;
        }
        else
            cout << "No" << endl;
    }
    return 0;
}

B. Food Buying

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Mishka wants to buy some food in the nearby shop. Initially, he has s burles on his card.

Mishka can perform the following operation any number of times (possibly, zero): choose some positive integer number 1≤x≤s, buy food that costs exactly x burles and obtain ⌊x10⌋ burles as a cashback (in other words, Mishka spends x burles and obtains ⌊x10⌋ back). The operation ⌊ab⌋ means a divided by b rounded down.

It is guaranteed that you can always buy some food that costs x for any possible value of x.

Your task is to say the maximum number of burles Mishka can spend if he buys food optimally.

For example, if Mishka has s=19 burles then the maximum number of burles he can spend is 21. Firstly, he can spend x=10 burles, obtain 1 burle as a cashback. Now he has s=10 burles, so can spend x=10 burles, obtain 1 burle as a cashback and spend it too.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases.

The next t lines describe test cases. Each test case is given on a separate line and consists of one integer s (1≤s≤109) — the number of burles Mishka initially has.

Output
For each test case print the answer on it — the maximum number of burles Mishka can spend if he buys food optimally.

Example
inputCopy
6
1
10
19
9876
12345
1000000000
outputCopy
1
11
21
10973
13716
1111111111
思路: 不断用出整十的数字,然后将剩余的零头加到后一次的获得的数上,一直到0.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>

using namespace std;

#define endl '\n'

typedef long long ll;


int main()
{
    int t;

    cin >> t;

    while (t--)
    {
        ll s;
        cin >> s;

        ll sum = s;

        while (s)
        {
            sum += s / 10;
            int x = s % 10;
            s /= 10;
            if (s == 0)
                break;
            s += x;
        }

        cout << sum << endl;
    }
    return 0;
}

C.Yet Another Walking Robot

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
There is a robot on a coordinate plane. Initially, the robot is located at the point (0,0). Its path is described as a string s of length n consisting of characters ‘L’, ‘R’, ‘U’, ‘D’.

Each of these characters corresponds to some move:

‘L’ (left): means that the robot moves from the point (x,y) to the point (x−1,y);
‘R’ (right): means that the robot moves from the point (x,y) to the point (x+1,y);
‘U’ (up): means that the robot moves from the point (x,y) to the point (x,y+1);
‘D’ (down): means that the robot moves from the point (x,y) to the point (x,y−1).
The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn’t want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point (xe,ye), then after optimization (i.e. removing some single substring from s) the robot also ends its path at the point (xe,ye).

This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot’s path such that the endpoint of his path doesn’t change. It is possible that you can’t optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string s).

Recall that the substring of s is such string that can be obtained from s by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of “LURLLR” are “LU”, “LR”, “LURLLR”, “URL”, but not “RR” and “UL”.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤1000) — the number of test cases.

The next 2t lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer n (1≤n≤2⋅105) — the length of the robot’s path. The second line of the test case contains one string s consisting of n characters ‘L’, ‘R’, ‘U’, ‘D’ — the robot’s path.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 (∑n≤2⋅105).

Output
For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot’s path doesn’t change, print -1. Otherwise, print two integers l and r such that 1≤l≤r≤n — endpoints of the substring you remove. The value r−l+1 should be minimum possible. If there are several answers, print any of them.

Example
input
4
4
LRUD
4
LURD
5
RRUDU
5
LLDDR
output
1 2
1 4
3 4
-1
思路: 记录每一个重复,重复的区间就是可以去掉的区间,然后找最短的即可。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>

using namespace std;

#define endl '\n'

typedef long long ll;

int main()
{
    int t;

    cin >> t;

    while (t--)
    {
        int n;
        cin >> n;
        string s;
        cin >> s;

        map<pair<int, int>, int>mp;

        int x = 0, y = 0;
        int l = 0, r = 9999999;

        mp[{0, 0}] = 1;

        for (int i = 0; i < n; ++i)
        {
            if (s[i] == 'L')
                x--;
            if (s[i] == 'R')
                x++;
            if (s[i] == 'U')
                y++;
            if (s[i] == 'D')
                y--;

            if (mp[{x, y}])
                if (i - mp[{x, y}] < r - l)
                    l = mp[{x, y}], r = i;

            mp[{x, y}] = i + 2;
        }

        if (l == 0)
            cout << "-1" << endl;
        else
            cout << l << " " << r + 1 << endl;
    }
    return 0;
}

发布了161 篇原创文章 · 获赞 7 · 访问量 7077

猜你喜欢

转载自blog.csdn.net/weixin_43778744/article/details/104189783