牛客周赛29
小红大战小紫
https://ac.nowcoder.com/acm/contest/73422/A
C
小红拿到了一个字符串,其中一定包含连续子串xiao
,和连续子串hong
请你将字符串重排,使得该字符串包含xiaohong
的连续子串
思路
在原串中删除xiao
和hong
void solve(){
string s;
cin >> s;
s.erase(s.find("xiao"), 4);
s.erase(s.find("hong"), 4);
cout << s + "xiaohong" << endl;
}
D
小红拿到了一个数组,她定义f(i)
为删除第i
个元素后,数组的中位数。现在小红想让你求出f(1), f(2), .., f(n)
思路
分类讨论
void solve(){
int n;
cin >> n;
ve<pii> g(n + 1);
g[0] = {0, 0};
for(int i = 1; i <= n; i++){
int x;
cin >> x;
g[i] = {x, i};
}
ve<double> res(n + 1);
sort(all(g));
if (n & 1) {
double a = (1.0 * g[n / 2].x + g[n / 2 + 1].x) / 2;
double b = (1.0 * g[n / 2 + 1].x + g[n / 2 + 2].x) / 2;
double c = (1.0 * g[n / 2].x + g[n / 2 + 2].x) / 2;
for (int i = 1; i <= n; i++) {
if (i <= n / 2) res[g[i].y] = b;
else if (i == n / 2 + 1) res[g[i].y] = c;
else res[g[i].y] = a;
}
} else {
double a = 1.0 * g[n / 2 + 1].x;
double b = 1.0 * g[n / 2].x;
for (int i = 1; i <= n; i++) {
if (i <= n / 2) res[g[i].y] = a;
else res[g[i].y] = b;
}
}
for (int i = 1; i <= n; i++) printf("%.1lf\n", res[i]);
}
E
小红希望你构造一个数组,给你一个x,满足以下三个条件:
- 数组的元素都是素数
- 数组所有元素相乘恰好等于x
- 数组任意相邻两个元素不等
思路
- 对x质因数分解,获取质因数和指数,创建优先队列保存
- 优先队列交替输出出现次数最大和次大的因子
- 特判1
void solve(){
ll x;
cin >> x;
if (x == 1) {
cout << -1 << endl;
return;
}
priority_queue<pii> heap;
ve<ll> res;
for (ll i = 2; i <= x / i; i++) {
if (x % i == 0) {
int t = 0;
while (x % i == 0) {
x /= i;
t++;
}
heap.push({t, i});
}
}
if (x > 1) heap.push({1, x});
while (!heap.empty()){
auto a = heap.top();
heap.pop();
a.x--;
if (!heap.empty()) {
auto b = heap.top();
heap.pop();
b.x--;
res.ps(a.y), res.ps(b.y);
if (a.x > 0) heap.push(a);
if (b.x > 0) heap.push(b);
} else {
if (a.x) {
cout << -1 << endl;
return;
} else res.ps(a.y);
}
}
cout << res.size() << endl;
for (auto x : res) cout << x << " ";
}