日常训练2025-1-2
日常训练2025-1-2
D. Digital string maximization
rating:1300
思路:找trick + 暴力
根据题意,一个数值为 i 的数最多向左移动 i 步。而最大的数为 9 ,也就是说对于一个位置 x ,他的值只可能来自于 \([x, x + 9]\) 这个范围,更远的那些数不可能移动过来
所以即使是暴力时间复杂度也不过是\(O(9*n)\)
评述
好烦这道题!!
代码
#include <bits/stdc++.h>
typedef std::pair<int, int> pii;
#define INF 0x3f3f3f3f
#define MOD 998244353
using i64 = long long;
const int N = 1e5+5;
void solve(){
std::string s;
std::cin >> s;
int n = s.size();
for (int i = 0; i < n; i++){
int cur = s[i] - '0';
int pos = -1;
for (int j = i; j < n && j < i + 10; j++){
if (s[j] - '0' - j + i > cur){
pos = j;
cur = s[j] - '0' - j + i;
}
}
if (pos != -1){
for (int k = pos; k >= i + 1; k--){
s[k]--;
std::swap(s[k], s[k-1]);
}
}
}
std::cout << s << '\n';
}
signed main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout<<std::setiosflags(std::ios::fixed)<<std::setprecision(2);
int t = 1, i;
std::cin >> t;
for (i = 0; i < t; i++){
solve();
}
return 0;
}
B. pspspsps
rating:1300
思路(找trick)
直接找规律吧,没啥其他方法
代码
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#if !defined(ONLINE_JUDGE) && defined(LOCAL)
#include "helper.h"
bool IS_LOCAL = true;
#else
#define dbg(...) ;
bool IS_LOCAL = false;
#endif
void go() {
int n;
string s;
cin >> n >> s;
if (n == 1) {
cout << "YES" << endl;
} else if (!s.contains('s') || !s.contains('p')) {
cout << "YES" << endl;
} else if (s[0] == 's' && !s.substr(1, s.size() - 1).contains('s')) {
cout << "YES" << endl;
} else if (s[n - 1] == 'p' && !s.substr(0, n - 1).contains('p')) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
if (IS_LOCAL)
std::cout << std::unitbuf;
for (int batch = 1;; batch++) {
int caseCnt;
cin >> caseCnt;
if (!caseCnt || cin.fail())
break;
for (int i = 1; i <= caseCnt; i++) {
dbg(batch, i);
go();
}
}
return 0;
}

浙公网安备 33010602011771号