#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>usingnamespace std;
int a[15];
intmain(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
bool is_right = true, is_wrong = false;
for (int i = 1 ; i <= n; i++)
{
cin >> a[i];
if (a[i] < a[i - 1])
{
is_right = false;
}
if (a[i] == a[i - 1])
{
is_wrong = true;
}
}
if (is_right)
{
cout << "YES" << endl;
continue;
}
if (is_wrong)
{
cout << "NO" << endl;
continue;
}
bool toDo = false;
bool is_solve = true;
do {
is_solve = true;
toDo = false;
for (int i = 2; i <= n - 1; i++)
{
if (a[i] > a[i + 1])
{
if (a[i] > a[i - 1])
{
toDo = true;
swap(a[i], a[i + 1]);
}
}
}
for (int i = 2; i <= n; i++)
{
if (a[i] < a[i - 1] || a[i] == a[i - 1])
{
is_solve = false;
}
}
}
while (toDo);
if (!is_solve)
{
cout << "NO" << endl;
}
else {
cout << "YES" << endl;
}
}
return0;
}
2|2更优解法
Observe that since we are only allowed to choose to swap and , it means that cannot be modified by the operation. Hence, must hold. In fact, we can prove that as long as , we will be able to sort the array.
Consider the largest element of the array. Let its index be . Our objective is to move to the end of the array. If , it means that the largest element is already at the end. Otherwise, since is the largest element, this means that and . Hence, we can do an operation on index and move the largest element one step closer to the end.
We repeatedly do the operation until we finally move the largest element to the end of the array. Then, we can pretend that the largest element does not exist and do the same algorithm for the prefix of size . Hence, we will able to sort the array by doing this repeatedly.
#include<bits/stdc++.h>usingnamespace std;
typedeflonglong ll;
typedef vector <ll> vi;
intmain(){
int t;
cin >> t;
while (t --> 0) {
int n;
cin >> n;
vi arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
if (arr[0] == 1) {
cout << "YES";
} else {
cout << "NO";
}
cout << '\n';
}
}
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define INF 0x7fffffffffffffff#define re registerusingnamespace std;
using ll = longlong;
inlinevoidSwap(int &a, int &b){
a = a ^ b;
b = a ^ b;
a = a ^ b;
return;
}
constint N = 2e5 + 10;
int n;
int a[N];
bool vis[N];
ll ans = -INF;
inlineboolcheck(int a, int b){
return a == 65 && b == 66;
}
inlinevoiddfs(ll now){
if (now < ans)
{
return;
}
ans = now;
for (re int i = 1; i <= n - 1; i++)
{
if (check(a[i], a[i + 1]) && !vis[i])
{
Swap(a[i], a[i + 1]);
vis[i] = true;
dfs(now + 1);
Swap(a[i], a[i + 1]);
vis[i] = false;
}
}
}
intmain(){
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
ans = -INF;
char ch;
getchar();
for (re int i = 1; i <= n; i++)
{
ch = getchar();
a[i] = ch;
}
dfs(0);
ans = (ans == -INF) ? 0 : ans;
printf("%lld\n", ans);
}
return0;
}
3|1正确解法
1|0CF标程
If the string consists of only or only , no operations can be done and hence the answer is .
Otherwise, let be the smallest index where and be the largest index where
If , this means that the string is of the form Since all the are before the , no operation can be done and hence the answer is also .
Now, we are left with the case where . Note that and by definition. Since the operation moves to the right and to the left, this means that will always consist of all and will always consist of all . Hence, no operation can be done from index to as well as from index to .
The remaining indices where an operation could possibly be done are from to . In fact, it can be proven that all operations can be done if their order is chosen optimally.
Let array store the indices of between and that contain in increasing order. In other words, and , where is the number of occurences of between and . For convenience, we let . Then, we do the operations in the following order:
It can be seen that the above ordering does operation on all indices between and . To see why all of the operations are valid, we look at each row separately. Each row starts with , which is valid as and (assuming that it is not the last operation of the row). Then, the following operations in the same row move to the left until position . To see why the last operation of the row is valid as well, even though might be equal to initially by definition, either which means that , or an operation was done on index in the previous row which moved to index . Hence, all operations are valid.
#include<bits/stdc++.h>usingnamespace std;
char s[200005];
signedmain(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tc, n; cin >> tc;
while (tc--) {
cin >> n; s[n + 1] = 'C';
for (int i = 1; i <= n; ++i) cin >> s[i];
int pt1 = 1, pt2 = 1, ans = 0;
while (s[pt1] == 'B') ++pt1, ++pt2;
while (pt1 <= n) {
int cntA = 0, cntB = 0;
while (s[pt2] == 'A') ++pt2, ++cntA;
while (s[pt2] == 'B') ++pt2, ++cntB;
if (s[pt2 - 1] == 'B') ans += pt2 - pt1 - 1;
if (cntB) pt1 = pt2 - 1;
elsebreak;
}
cout << ans << '\n';
}
}
#include<bits/stdc++.h>usingnamespace std;
using LL = longlong;
constint INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
constint N = 300005;
char s[N];
intmain(){
int _;
scanf("%d", &_);
while (_--) {
int n;
scanf("%d%s", &n, s + 1);
int ans = 0, x = 0, y = 0;
for (int i = 1; i <= n; i++) {
if (s[i] == 'B')
y = i;
else {
if (x == 0) x = i;
}
}
if (x != 0 && y != 0) {
if (y >= x) ans = y - x;
}
printf("%d\n", ans);
}
return0;
}
int a[N], b[N], c[N];
int ans[N];
int n, m;
voidsolve(){
std::cin >> n >> m;
for (int i = 1; i <= n; i++) std::cin >> a[i];
for (int i = 1; i <= n; i++) std::cin >> b[i];
std::iota(c + 1, c + n + 1, 1);
std::sort(c + 1, c + n + 1, [](int p1, int p2){
return a[p1] < a[p2];
});
std::sort(b + 1, b + n + 1);
for (int i = 1; i <= m; i++)
{
int x = c[n - m + i];
if (a[x] <= b[i]) {std::cout << "NO\n"; return ;}
ans[x] = b[i];
}
for (int i = 1; i <= n - m; i++)
{
int x = c[i];
if (a[x] > b[i + m]) {std::cout << "NO\n"; return ;}
ans[x] = b[i + m];
}
std::cout << "YES\n";
for (int i = 1; i <= n; i++) std::cout << ans[i] << " ";
std::cout << std::endl;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下