「解题报告」Codeforces Round 905 (Div. 3)
A. Morning#
You are given a four-digit pin code consisting of digits from to that needs to be entered. Initially, the cursor points to the digit . In one second, you can perform exactly one of the following two actions:
- Press the cursor to display the current digit,
- Move the cursor to any adjacent digit.
The image above shows the device you are using to enter the pin code. For example, for the digit , the adjacent digits are and , and for the digit , there is only one adjacent digit, .
Determine the minimum number of seconds required to enter the given four-digit pin code.
Input
Each test consists of multiple test cases. The first line contains a single integer () - the number of the test cases. This is followed by their description.
The single line of each test case describes the pin code as a string of length , consisting of digits from to .
Output
For each test case, output the minimum number of seconds required to enter the given pin code.
简单模拟题。
// The code was written by yifan, and yifan is neutral!!!
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define bug puts("NOIP rp ++!");
#define rep(i, a, b, c) for (int i = (a); i <= (b); i += (c))
#define per(i, a, b, c) for (int i = (a); i >= (b); i -= (c))
template<typename T>
inline T read() {
T x = 0;
bool fg = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
fg |= (ch == '-');
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return fg ? ~x + 1 : x;
}
template<typename T>
void write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
template<typename T>
void print(T x, char c) {
write(x);
putchar(c);
}
int T;
int s[10];
void solve() {
rep (i, 1, 4, 1) {
scanf("%1d", &s[i]);
if (s[i] == 0) {
s[i] = 10;
}
}
int cur = 1, ans = 4;
rep (i, 1, 4, 1) {
if (s[i] != cur) {
ans += abs(s[i] - cur);
cur = s[i];
}
}
print(ans, '\n');
}
int main() {
T = read<int>();
while (T --) {
solve();
}
return 0;
}
B. Chemistry#
You are given a string of length , consisting of lowercase Latin letters, and an integer .
You need to check if it is possible to remove exactly characters from the string in such a way that the remaining characters can be rearranged to form a palindrome. Note that you can reorder the remaining characters in any way.
A palindrome is a string that reads the same forwards and backwards. For example, the strings "z", "aaa", "aba", "abccba" are palindromes, while the strings "codeforces", "reality", "ab" are not.
Input
Each test consists of multiple test cases. The first line contains a single integer () — the number of the test cases. This is followed by their description.
The first line of each test case contains two integers and () — the length of the string and the number of characters to be deleted.
The second line of each test case contains a string of length , consisting of lowercase Latin letters.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output "YES" if it is possible to remove exactly characters from the string in such a way that the remaining characters can be rearranged to form a palindrome, and "NO" otherwise.
You can output the answer in any case (uppercase or lowercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive answers.
开 个桶,然后先取偶数对元素,最后判断偶数对元素的总个数是否大于等于 即可。
// The code was written by yifan, and yifan is neutral!!!
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define bug puts("NOIP rp ++!");
#define rep(i, a, b, c) for (int i = (a); i <= (b); i += (c))
#define per(i, a, b, c) for (int i = (a); i >= (b); i -= (c))
template<typename T>
inline T read() {
T x = 0;
bool fg = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
fg |= (ch == '-');
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return fg ? ~x + 1 : x;
}
template<typename T>
void write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
template<typename T>
void print(T x, char c) {
write(x);
putchar(c);
}
const int N = 1e5 + 5;
int T, n, k;
int cnt[26];
char s[N];
void solve() {
rep (i, 0, 25, 1) {
cnt[i] = 0;
}
n = read<int>(), k = read<int>();
scanf("%s", s);
rep (i, 0, n - 1, 1) {
++ cnt[s[i] - 'a'];
}
ll res = 0;
rep (i, 0, 25, 1) {
res += 2 * (cnt[i] / 2);
}
if (res >= n - k - 1) {
puts("YES");
} else {
puts("NO");
}
}
int main() {
T = read<int>();
while (T --) {
solve();
}
return 0;
}
C. Raspberries#
You are given an array of integers and a number (). In one operation, you can do the following:
- Choose an index ,
- Set .
Find the minimum number of operations needed to make the product of all the numbers in the array divisible by .
Input
Each test consists of multiple test cases. The first line contains a single integer () — the number of test cases. Then follows the description of the test cases.
The first line of each test case contains two integers and (, ) — the size of the array and the number .
The second line of each test case contains integers ().
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output the minimum number of operations needed to make the product of all the numbers in the array divisible by .
分两类处理,一类是 的时候,另一类是 的时候。
// The code was written by yifan, and yifan is neutral!!!
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define bug puts("NOIP rp ++!");
#define rep(i, a, b, c) for (int i = (a); i <= (b); i += (c))
#define per(i, a, b, c) for (int i = (a); i >= (b); i -= (c))
template<typename T>
inline T read() {
T x = 0;
bool fg = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
fg |= (ch == '-');
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return fg ? ~x + 1 : x;
}
template<typename T>
void write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
template<typename T>
void print(T x, char c) {
write(x);
putchar(c);
}
const int N = 1e5 + 5;
int T, n, k;
int a[N];
int gcd(int a, int b) {
if (!b) {
return a;
}
return gcd(b, a % b);
}
void solve() {
n = read<int>(), k = read<int>();
int g, ans = 1e9;
ll res = 1;
rep (i, 1, n, 1) {
a[i] = read<int>();
res = res * a[i] % k;
}
if (!res) {
ans = 0;
print(ans, '\n');
return ;
}
if (k != 4) {
rep (i, 1, n, 1) {
rep (j, 1, 10, 1) {
if (k * j < a[i]) continue ;
ans = min(ans, k * j - a[i]);
}
}
print(ans, '\n');
} else {
int fg1 = 0, fg2 = 0;
rep (i, 1, n, 1) {
if (a[i] % 2 == 0) {
++ fg2;
} else {
++ fg1;
}
}
if (fg1 && fg2) {
puts("1");
return ;
}
if (!fg1 && fg2) {
puts("2");
return ;
}
if (!fg2) {
ans = 2;
rep (i, 1, n, 1) {
rep (j, 1, 10, 1) {
if (k * j < a[i]) continue ;
ans = min(ans, k * j - a[i]);
}
}
print(ans, '\n');
}
}
}
int main() {
T = read<int>();
while (T --) {
solve();
}
return 0;
}
D. In Love#
Initially, you have an empty multiset of segments. You need to process $$$$ operations of two types:
- — Add the segment to the multiset,
- — Remove exactly one segment from the multiset. It is guaranteed that this segment exists in the multiset.
After each operation, you need to determine if there exists a pair of segments in the multiset that do not intersect. A pair of segments and do not intersect if there does not exist a point such that and .
Input
The first line of each test case contains an integer () — the number of operations.
The next lines describe two types of operations. If it is an addition operation, it is given in the format . If it is a deletion operation, it is given in the format ().
Output
After each operation, print "YES" if there exists a pair of segments in the multiset that do not intersect, and "NO" otherwise.
You can print the answer in any case (uppercase or lowercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive answers.
考察 multiset
的使用。
// The code was written by yifan, and yifan is neutral!!!
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define bug puts("NOIP rp ++!");
#define rep(i, a, b, c) for (int i = (a); i <= (b); i += (c))
#define per(i, a, b, c) for (int i = (a); i >= (b); i -= (c))
template<typename T>
inline T read() {
T x = 0;
bool fg = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
fg |= (ch == '-');
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return fg ? ~x + 1 : x;
}
template<typename T>
void write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
template<typename T>
void print(T x, char c) {
write(x);
putchar(c);
}
using tii = tuple<ll, ll>;
int n;
multiset<ll> l, r;
int main() {
n = read<int>();
string op;
ll x, y;
rep (i, 1, n, 1) {
cin >> op >> x >> y;
if (op == "+") {
l.emplace(x);
r.emplace(y);
} else {
l.erase(l.lower_bound(x));
r.erase(r.lower_bound(y));
}
if (l.size() < 2) {
puts("NO");
continue ;
}
ll it1 = *r.begin(), it2 = *l.rbegin();
if (it1 < it2) {
puts("YES");
} else {
puts("NO");
}
}
return 0;
}
E. Look Back#
You are given an array of integers . You need to make it non-decreasing with the minimum number of operations. In one operation, you do the following:
- Choose an index ,
- Set .
An array is non-decreasing if for all .
Input
Each test consists of multiple test cases. The first line contains a single integer () — the number of test cases. This is followed by their description.
The first line of each test case contains an integer () — the size of the array .
The second line of each test case contains integers ().
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output the minimum number of operations needed to make the array non-decreasing.
log2
函数的使用。
// The code was written by yifan, and yifan is neutral!!!
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define bug puts("NOIP rp ++!");
#define rep(i, a, b, c) for (int i = (a); i <= (b); i += (c))
#define per(i, a, b, c) for (int i = (a); i >= (b); i -= (c))
template<typename T>
inline T read() {
T x = 0;
bool fg = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
fg |= (ch == '-');
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return fg ? ~x + 1 : x;
}
template<typename T>
void write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
template<typename T>
void print(T x, char c) {
write(x);
putchar(c);
}
const int N = 1e5 + 5;
using ull = unsigned long long;
using db = double;
const db eps = 1e-11;
int T, n;
db _2[N];
ull a[N];
void solve() {
n = read<int>();
ll cnt = 0;
_2[1] = 0;
rep (i, 1, n, 1) {
a[i] = read<ll>();
_2[i] = log2(1.0 * a[i]);
}
rep (i, 2, n, 1) {
if (_2[i - 1] - _2[i] > eps) {
db cha = _2[i - 1] - _2[i];
ll chatmp = cha;
if (cha - chatmp <= eps) {
cnt += chatmp;
_2[i] += chatmp;
} else {
cnt += chatmp + 1;
_2[i] += chatmp + 1;
}
}
}
print(cnt, '\n');
}
int main() {
T = read<int>();
while (T --) {
solve();
}
return 0;
}
作者:yifan0305
出处:https://www.cnblogs.com/yifan0305/p/17781386.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
转载时还请标明出处哟!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】