峰值
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, res = 0;
cin >> n;
vector<int> v(n), l0(n), l1(n);
for (int i = 0; i < n; ++i) cin >> v[i];
for (int i = 1; i < n; ++i) {
if (v[i] > v[i-1]) l0[i] = max(l0[i], l0[i-1] + 1);
}
for (int i = n-2; i >= 0; --i) {
if (v[i] > v[i+1]) l1[i] = max(l1[i], l1[i+1] + 1);
}
for (int i = 0; i < n; ++i) {
if (l0[i] >= 1 && l1[i] >= 1) {
res = max(res, l0[i] + l1[i] + 1);
}
}
cout << res << "\n";
}
数字
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll N, M = 0;
cin >> N;
while (1) {
if (M > LLONG_MAX - N) {
cout << "-1\n";
return 0;
}
M += N;
set<char> s;
for (auto c : to_string(M)) {
if (c < '2') continue;
s.insert(c);
}
if (s.size() <= 1) break;
}
cout << M << "\n";
}
字符串
#include <bits/stdc++.h>
using namespace std;
vector<int> v(26);
int t = 0, k;
bool dfs(int i, int n, string &s, bool flag) {
if (i == n) return t == 0;
char old_c = s[i];
for (char c = flag ? 'z' : s[i]; c >= 'a'; --c) {
int cnt = v[c - 'a'];
int need = cnt == 0 ? k - 1 : -1;
if (t + need > n - i - 1) continue;
v[c - 'a'] = cnt == 0 ? k - 1 : cnt - 1;
t += need;
s[i] = c;
if (dfs(i + 1, n, s, flag | (c < old_c))) return true;
v[c - 'a'] = cnt;
t -= need;
}
s[i] = old_c;
return false;
}
int main() {
string s;
cin >> k >> s;
if (k == 1) { cout << s << "\n"; return 0; }
else if (s.size() % k != 0) cout << "-1\n";
else if (!dfs(0, s.size(), s, false)) cout << "-1\n";
else cout << s << "\n";
}
猜想
#include <bits/stdc++.h>
using namespace std;
int main() {
ll N, M;
cin >> N >> M;
ll i = 2, Sqrt = sqrt(M);
map<int, int> mp;
while (M != 1) {
if (M < i) {
mp[M]++;
break;
}
if (M % i == 0) {
++mp[i];
M /= i;
}
else if (i == 2) ++i;
else i += 2;
}
ll res = LLONG_MAX;
for (auto [i, j] : mp) {
ll cnt = 0, n = N;
while (n > 1) {
cnt += n / i;
n /= i;
}
res = min(res, cnt / j);
}
cout << res << "\n";
}
全部评论
(9) 回帖