{% note blue 'fas fa-bullhorn' simple %}打过的最难的div3{% endnote %}
A.Line Breaks
题目描述
{% tabs A %}
Kostya has a text s consisting of n words made up of Latin alphabet letters. He also has two strips on which he must write the text. The first strip can hold m characters, while the second can hold as many as needed.
Kostya must choose a number x and write the first x words from s on the first strip, while all the remaining words are written on the second strip. To save space, the words are written without gaps, but each word must be entirely on one strip.
Since space on the second strip is very valuable, Kostya asks you to choose the maximum possible number x such that all words s1,s2,…,sx fit on the first strip of length m.
一段文本有
问:第一页纸能写多少个单词
{% endtabs %}
解题思路
模拟
参考代码
#include<bits/stdc++.h>
using i128 = __int128;using i64 = long long;#define endl '\n'#define pii std::pair<int ,int>#define fix(x) std::fixed << std::setprecision(x)const i64 inf = 1e17 + 50, MAX_N = 1e5 + 50, mod = 1e9 + 7;std::mt19937_64 rng(std::chrono::system_clock::now().time_since_epoch().count());
void solve() { int n, m, f = 1; std::cin >> n >> m; std::vector<std::string> s(n); for(int i = 0; i < n; i++) { std::cin >> s[i]; }
int su = 0, ct = 0; for(int i = 0; i < n; i++) { if (su + s[i].size() <= m) { ct++; su += (int) s[i].size(); } else { break; } } std::cout << ct << endl;}
signed main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr), std::cout.tie(nullptr); int Lazy_boy_ = 1; std::cin >> Lazy_boy_; while (Lazy_boy_--) solve(); return 0;}B.Transfusion
题目描述
{% tabs B %}
You are given an array a of length n. In one operation, you can pick an index i from 2 to n−1 inclusive, and do one of the following actions:
- Decrease
by 1, then increase by 1. - Decrease
by 1, then increase by 1.
After each operation, all the values must be non-negative. Can you make all the elements equal after any number of operations?
给定长度为
; ;
问:是否可以通过以上操作将数组的所有元素变为相等。
{% endtabs %}
解题思路
可以发现每次修改的下标奇偶性相同,我们可以分别记录下奇数下标和偶数下标的
那么能实现的判断条件也很简单,如下:
奇 奇 偶 偶 奇 奇 , 偶 偶
参考代码
#include <bits/stdc++.h>
#define int long long#define endl '\n'#define pii std::pair<int, int>#define fix(x) std::fixed << std::setprecision(x)const int inf = 1e17 + 50, MAX_N = 1e5 + 50, mod = 1e9 + 7;
void solve() { int n; std::cin >> n; std::vector<int> a(n); int ct[2]{}, sum[2]{}; int s = 0; for (int i = 0; i < n; i++) { std::cin >> a[i]; sum[i % 2] += a[i]; ct[i % 2] ++; }
if(sum[0] % ct[0] || sum[1] % ct[1] || sum[0] / ct[0] != sum[1] / ct[1]) { std::cout << "NO" << endl; }else { std::cout << "YES" << endl; }}
signed main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr), std::cout.tie(nullptr); int Lazy_boy_ = 1; std::cin >> Lazy_boy_; while (Lazy_boy_--) solve(); return 0;}C.Uninteresting Number
题目描述
{% tabs C %}
You are given a number n with a length of no more than 105.
You can perform the following operation any number of times: choose one of its digits, square it, and replace the original digit with the result. The result must be a digit (that is, if you choose the digit x, then the value of x2 must be less than 10).
Is it possible to obtain a number that is divisible by 9 through these operations?
给你一个长度不超过
你可以多次进行下面的运算:选择其中一个数字,将其平方,然后用运算结果替换原来的数字。结果必须是一位数字(也就是说,如果您选择数字
通过这些运算,有可能得到一个能被
{% endtabs %}
解题思路
数字必须是数字这一要求对变换有如下限制:我们可以将
我们将使用
我们将计算数字中
因此,最终的解法是这样的:我们计算数字的位数之和,数出
参考代码
#include <bits/stdc++.h>
#define int long long#define endl '\n'#define pii std::pair<int, int>#define fix(x) std::fixed << std::setprecision(x)const int inf = 1e17 + 50, MAX_N = 1e5 + 50, mod = 1e9 + 7;
void solve() { std::string s; std::cin >> s; int sum = 0, ct1 = 0, ct2 = 0; for (auto i : s) { sum += i - '0'; ct1 += (i == '2' ? 1 : 0); ct2 += (i == '3' ? 1 : 0); }
for (int i = 0; i <= std::min(9ll, ct1); i++) { for (int j = 0; j <= std::min(9ll, ct2); j++) { if ((sum + i * 2 + j * 6) % 9 == 0) { std::cout << "YES" << endl; return; } } }
std::cout << "NO" << endl;}
signed main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr), std::cout.tie(nullptr); int Lazy_boy_ = 1; std::cin >> Lazy_boy_; while (Lazy_boy_--) solve(); return 0;}D.Digital string maximization
题目描述
{% tabs D %}
You are given a string s, consisting of digits from 0 to 9. In one operation, you can pick any digit in this string, except for 0 or the leftmost digit, decrease it by 1, and then swap it with the digit left to the picked.
For example, in one operation from the string 1023, you can get 1103 or 1022.
Find the lexicographically maximum string you can obtain after any number of operations.
给你一个由 0 到 9 的数字组成的字符串 s。在一次操作中,您可以选取该字符串中除 0 或最左边数字之外的任意一个数字,将其减少 1,然后将其与左边的数字对调。
例如,从字符串 1023 中进行一次运算,可以得到 1103 或 1022。
找出任意多次运算后可以得到的词性最大的字符串。
{% endtabs %}
解题思路
让我们看看数字
因此,我们可以对每个
参考代码
#include <bits/stdc++.h>
#define int long long#define endl '\n'#define pii std::pair<int, int>#define fix(x) std::fixed << std::setprecision(x)const int inf = 1e17 + 50, MAX_N = 1e5 + 50, mod = 1e9 + 7;
void solve() { std::string s; std::cin >> s; for (int i = 0; i < s.size(); i++) { int x = s[i] - '0', pos = i; for (int j = i; j < std::min(i + 10, (int)s.size()); j++) { if (s[j] - '0' - (j - i) > x) { x = s[j] - '0' - (j - i); pos = j; } } while (pos > i) { std::swap(s[pos], s[pos - 1]); pos--; } s[i] = x + '0'; } std::cout << s << endl;}
signed main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr), std::cout.tie(nullptr); int Lazy_boy_ = 1; std::cin >> Lazy_boy_; while (Lazy_boy_--) solve(); return 0;}