AtCoder Beginner Contest 338 A B C D

A - Capitalized?

time limit per test: 2 second
memory limit per test: 1024 megabytes

Problem Statement

You are given a non-empty string S S S consisting of uppercase and lowercase English letters. Determine whether the following condition is satisfied: - The first character of S S S is uppercase, and all other characters are lowercase.

Constraints

  • 1 ≤ ∣ S ∣ ≤ 100 1 \leq |S| \leq 100 1S100 ( ∣ S ∣ |S| S is the length of the string S S S.)
  • Each character of S S S is an uppercase or lowercase English letter.

Input

The input is given from Standard Input in the following format:

S

Output

If the condition is satisfied, print Yes; otherwise, print No.

Sample Input 1

Capitalized

Sample Output 1

Yes

The first character C of Capitalized is uppercase, and all other characters apitalized are lowercase, so you should print Yes.

Sample Input 2

AtCoder

Sample Output 2

No

AtCoder contains an uppercase letter C that is not at the beginning, so you should print No.

Sample Input 3

yes

Sample Output 3

No

The first character y of yes is not uppercase, so you should print No.

Sample Input 4

A

Sample Output 4

Yes

Tutorial

先判断首字母是不是大写,再判断后面的字母是不是小写

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long

void solve() {
    string s;
    cin >> s;
    if (not(s[0] >= 'A' and s[0] <= 'Z')) {
        cout << "No";
        return;
    }
    for (int i = 1; i < s.size(); ++i) {
        if (s[i] >= 'A' and s[i] <= 'Z') {
            cout << "No";
            return;
        }
    }
    cout << "Yes";
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    return 0;
}

B - Frequency

time limit per test: 2 second
memory limit per test: 1024 megabytes

Problem Statement

You are given a string S S S consisting of lowercase English letters. Find the character that appears most frequently in S S S. If multiple such characters exist, report the one that comes earliest in alphabetical order.

Constraints

  • 1 ≤ ∣ S ∣ ≤ 1000 1 \leq |S| \leq 1000 1S1000 ( ∣ S ∣ |S| S is the length of the string S S S.)
  • Each character in S S S is a lowercase English letter.

Input

The input is given from Standard Input in the following format:

S

Output

Among the characters that appear most frequently in S S S, print the one that comes earliest in alphabetical order.

Sample Input 1

frequency

Sample Output 1

e

In frequency, the letter e appears twice, which is more than any other character, so you should print e.

Sample Input 2

atcoder

Sample Output 2

a

In atcoder, each of the letters a, t, c, o, d, e, and r appears once, so you should print the earliest in alphabetical order, which is a.

Sample Input 3

pseudopseudohypoparathyroidism

Sample Output 3

o

Tutorial

先计数,然后从 a 遍历到 z 寻找最大值

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long

void solve() {
    string s;
    cin >> s;
    char ans = 'a';
    int cnt = 0;
    map<char, int> mp;
    for (char c : s) {
        mp[c]++;
    }
    for (auto [c, y] : mp) {
        if (y > cnt) {
            ans = c;
            cnt = y;
        }
    }
    cout << ans << endl;
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    return 0;
}

C - Leftover Recipes

time limit per test: 2 second
memory limit per test: 1024 megabytes

Problem Statement

Your refrigerator has N N N kinds of ingredients. Let us call them ingredient 1 1 1, … \dots , ingredient N N N. You have Q i Q_i Qi grams of ingredient i i i.

You can make two types of dishes. To make one serving of dish A, you need A i A_i Ai grams of each ingredient i i i ( 1 ≤ i ≤ N ) (1 \leq i \leq N) (1iN). To make one serving of dish B, you need B i B_i Bi grams of each ingredient i i i. You can only make an integer number of servings of each type of dish.

Using only the ingredients in the refrigerator, what is the maximum total number of servings of dishes you can make?

Constraints

  • 1 ≤ N ≤ 10 1 \leq N \leq 10 1N10
  • 1 ≤ Q i ≤ 1 0 6 1 \leq Q_i \leq 10^6 1Qi106
  • 0 ≤ A i ≤ 1 0 6 0 \leq A_i \leq 10^6 0Ai106
  • There is an i i i such that A i ≥ 1 A_i \geq 1 Ai1.
  • 0 ≤ B i ≤ 1 0 6 0 \leq B_i \leq 10^6 0Bi106
  • There is an i i i such that B i ≥ 1 B_i \geq 1 Bi1.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
Q_1 Q_2 ... Q_N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

Output

Assuming that you can make a maximum total of S S S servings of dishes, print the integer S S S.

Sample Input 1

2
800 300
100 100
200 10

Sample Output 1

5

This refrigerator has 800 800 800 grams of ingredient 1 1 1 and 300 300 300 grams of ingredient 2 2 2.

You can make one serving of dish A with 100 100 100 grams of ingredient 1 1 1 and 100 100 100 grams of ingredient 2 2 2, and one serving of dish B with 200 200 200 grams of ingredient 1 1 1 and 10 10 10 grams of ingredient 2 2 2.

To make two servings of dish A and three servings of dish B, you need 100 × 2 + 200 × 3 = 800 100 \times 2 + 200 \times 3 = 800 100×2+200×3=800 grams of ingredient 1 1 1, and 100 × 2 + 10 × 3 = 230 100 \times 2 + 10 \times 3 = 230 100×2+10×3=230 grams of ingredient 2 2 2, neither of which exceeds the amount available in the refrigerator. In this way, you can make a total of five servings of dishes, but there is no way to make six, so the answer is 5 5 5.

Sample Input 1

2
800 300
100 0
0 10

Sample Output 1

5

This refrigerator has 800 800 800 grams of ingredient 1 1 1 and 300 300 300 grams of ingredient 2 2 2.

You can make one serving of dish A with 100 100 100 grams of ingredient 1 1 1 and 100 100 100 grams of ingredient 2 2 2, and one serving of dish B with 200 200 200 grams of ingredient 1 1 1 and 10 10 10 grams of ingredient 2 2 2.

To make two servings of dish A and three servings of dish B, you need 100 × 2 + 200 × 3 = 800 100 \times 2 + 200 \times 3 = 800 100×2+200×3=800 grams of ingredient 1 1 1, and 100 × 2 + 10 × 3 = 230 100 \times 2 + 10 \times 3 = 230 100×2+10×3=230 grams of ingredient 2 2 2, neither of which exceeds the amount available in the refrigerator. In this way, you can make a total of five servings of dishes, but there is no way to make six, so the answer is 5 5 5.

Sample Input 2

2
800 300
100 0
0 10

Sample Output 2

38

You can make 8 8 8 servings of dish A with 800 800 800 grams of ingredient 1 1 1, and 30 30 30 servings of dish B with 300 300 300 grams of ingredient 2 2 2, for a total of 38 38 38 servings.

Sample Input 3

2
800 300
801 300
800 301

Sample Output 3

0

You cannot make any dishes.

Sample Input 4

10
1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0

Sample Output 4

222222

Tutorial

根据数据范围可知,本题可以直接通过枚举 A A A 的数量,获取 B B B 的数量,然后对两者的数量 c n t A cnt_A cntA c n t B cnt_B cntB 的和取最大值即可

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long

void solve() {
    int n, ans = 0, mn = INF;
    cin >> n;
    vector<int> q(n), a(n), b(n);
    for (int &qi : q) {
        cin >> qi;
    }
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        if (a[i]) {
            mn = min(mn, q[i] / a[i]);
        }
    }
    for (int &bi : b) {
        cin >> bi;
    }
    for (int x = 0; x <= mn; ++x) {
        int cnt = INF;
        for (int i = 0; i < n; ++i) {
            if (b[i]) {
                cnt = min(cnt, (q[i] - a[i] * x) / b[i]);
            }
        }
        ans = max(ans, cnt + x);
    }
    cout << ans << endl;
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    return 0;
}

D - Island Tour

time limit per test: 2 second
memory limit per test: 1024 megabytes

Problem Statement

The AtCoder Archipelago consists of N N N islands connected by N N N bridges. The islands are numbered from 1 1 1 to N N N, and the i i i-th bridge ( 1 ≤ i ≤ N − 1 1\leq i\leq N-1 1iN1) connects islands i i i and i + 1 i+1 i+1 bidirectionally, while the N N N-th bridge connects islands N N N and 1 1 1 bidirectionally. There is no way to travel between islands other than crossing the bridges.

On the islands, a tour that starts from island X 1 X_1 X1 and visits islands X 2 , X 3 , … , X M X_2, X_3, \dots, X_M X2,X3,,XM in order is regularly conducted. The tour may pass through islands other than those being visited, and the total number of times bridges are crossed during the tour is defined as the length of the tour.

More precisely, a tour is a sequence of l + 1 l+1 l+1 islands a 0 , a 1 , … , a l a_0, a_1, \dots, a_l a0,a1,,al that satisfies all the following conditions, and its length is defined as l l l:

  • For all j   ( 0 ≤ j ≤ l − 1 ) j\ (0\leq j\leq l-1) j (0jl1), islands a j a_j aj and a j + 1 a_{j+1} aj+1 are directly connected by a bridge.
  • There are some 0 = y 1 < y 2 < ⋯ < y M = l 0 = y_1 < y_2 < \dots < y_M = l 0=y1<y2<<yM=l such that for all k   ( 1 ≤ k ≤ M ) k\ (1\leq k\leq M) k (1kM), a y k = X k a_{y_k} = X_k ayk=Xk.

Due to financial difficulties, the islands will close one bridge to reduce maintenance costs. Determine the minimum possible length of the tour when the bridge to be closed is chosen optimally.

Constraints

  • 3 ≤ N ≤ 2 × 1 0 5 3\leq N \leq 2\times 10^5 3N2×105
  • 2 ≤ M ≤ 2 × 1 0 5 2\leq M \leq 2\times 10^5 2M2×105
  • 1 ≤ X k ≤ N 1\leq X_k\leq N 1XkN
  • X k ≠ X k + 1   ( 1 ≤ k ≤ M − 1 ) X_k\neq X_{k+1}\ (1\leq k\leq M-1) Xk=Xk+1 (1kM1)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M
X_1 X_2 ... X_M

Output

Print the answer as an integer.

Sample Input 1

3 3
1 3 2

Sample Output 1

2
  • If the first bridge is closed: By taking the sequence of islands ( a 0 , a 1 , a 2 ) = ( 1 , 3 , 2 ) (a_0, a_1, a_2) = (1, 3, 2) (a0,a1,a2)=(1,3,2), it is possible to visit islands 1 , 3 , 2 1, 3, 2 1,3,2 in order, and a tour of length 2 2 2 can be conducted. There is no shorter tour.
  • If the second bridge is closed: By taking the sequence of islands ( a 0 , a 1 , a 2 , a 3 ) = ( 1 , 3 , 1 , 2 ) (a_0, a_1, a_2, a_3) = (1, 3, 1, 2) (a0,a1,a2,a3)=(1,3,1,2), it is possible to visit islands 1 , 3 , 2 1, 3, 2 1,3,2 in order, and a tour of length 3 3 3 can be conducted. There is no shorter tour.
  • If the third bridge is closed: By taking the sequence of islands ( a 0 , a 1 , a 2 , a 3 ) = ( 1 , 2 , 3 , 2 ) (a_0, a_1, a_2, a_3) = (1, 2, 3, 2) (a0,a1,a2,a3)=(1,2,3,2), it is possible to visit islands 1 , 3 , 2 1, 3, 2 1,3,2 in order, and a tour of length 3 3 3 can be conducted. There is no shorter tour.

Therefore, the minimum possible length of the tour when the bridge to be closed is chosen optimally is 2 2 2.

The following figure shows, from left to right, the cases when bridges 1 , 2 , 3 1, 2, 3 1,2,3 are closed, respectively. The circles with numbers represent islands, the lines connecting the circles represent bridges, and the blue arrows represent the shortest tour routes.

Sample Input 2

4 5
2 4 2 4 2

Sample Output 2

8

Sample Input 3

163054 10
62874 19143 77750 111403 29327 56303 6659 18896 64175 26369

Sample Output 3

390009

Tutorial

利用差分,从左边界开始,先将逆时针的距离减去顺时针的距离累加到左边界,先将顺时针的距离减去逆时针的距离累加到右边界,再将顺时针的距离累加到第一个岛上,最后进行累加即可

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long

const int INF = 0x3f3f3f3f3f3f3f3f;

void solve() {
    int n, m, ans = INF;
    cin >> n >> m;
    vector<int> x(m), diff(n + 1);
    for (int &xi : x) {
        cin >> xi;
    }
    for (int i = 0; i < m - 1; ++i) {
        int a = x[i], b = x[i + 1];
        if (a > b) {
            swap(a, b);
        }
        diff[1] += b - a;
        diff[a] += n - (b - a) - (b - a);
        diff[b] += b - a - (n - (b - a));
    }
    for (int i = 1; i <= n; ++i) {
        diff[i] += diff[i - 1];
        ans = min(ans, diff[i]);
    }
    cout << ans << endl;
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    return 0;
}

  • 22
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值