Codeforces Round 892 (Div.2)
A. United We Stand
题解
- 赛时想复杂了
- 题目要求我们保证数组\(c\)中的数不是数组\(b\)中任意一个数的因子
- 我们考虑将最小值置于数组\(b\)即可
const int N = 2e5 + 10, M = 4e5 + 10;
int n;
int a[N];
void solve()
{
cin >> n;
int mi = INF;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
mi = min(a[i], mi);
}
vector<int> b, c;
for (int i = 1; i <= n; ++i)
{
if (a[i] == mi)
b.push_back(a[i]);
else
c.push_back(a[i]);
}
if (b.size() == 0 || c.size() == 0)
cout << -1 << endl;
else
{
cout << b.size() << " " << c.size() << endl;
for (auto x : b)
cout << x << " ";
cout << endl;
for (auto x : c)
cout << x << " ";
cout << endl;
}
}
B. Olya and Game with Arrays
题解
- 我们考虑贪心
- 对于所有数组中的最小值来说,不管我们怎么移动,他对答案的贡献不变
- 所以我们考虑将每个数组中的最小值移动到某一个数组中,使得次小值对答案产生贡献
- 我们如何确定被移动到的是哪一个数组呢
- 只要对次小值排序即可,对于存在最小的次小值的数组就是被移动到的数组
const int N = 2e5 + 10, M = 4e5 + 10;
int n;
vector<int> vec[N];
void solve()
{
cin >> n;
int mi = INF;
vector<int> b;
for (int i = 1; i <= n; ++i)
{
int k;
cin >> k;
vec[i].clear();
for (int j = 1; j <= k; ++j)
{
int x;
cin >> x;
vec[i].push_back(x);
}
sort(all(vec[i]));
b.push_back(vec[i][1]);
mi = min(mi, vec[i][0]);
}
sort(all(b));
int ans = 0;
ans += mi;
for (int i = 1; i < b.size(); ++i)
ans += b[i];
cout << ans << endl;
}
C. Another Permutation Problem
题解
- 打表找规律
- 发现除了\(n=2\)时都是先升序后降序的形式,例如\([1, 2,3,4,8,7,6,5]\)
- \(O(n^2)\)枚举在哪个位置开始降序即可
const int N = 2e5 + 10, M = 4e5 + 10;
int n;
int a[N], b[N], c[N];
void solve()
{
cin >> n;
if (n == 2)
{
cout << 2 << endl;
return;
}
int ans = 0;
for (int i = 1; i <= n; ++i)
{
int mx = 0, sum = 0;
for (int j = 1; j <= i; ++j)
{
sum += j * j;
mx = max(mx, j * j);
}
int t = n;
for (int j = i + 1; j <= n; ++j)
{
sum += t * j;
mx = max(mx, t * j);
--t;
}
ans = max(ans, sum - mx);
}
cout << ans << endl;
}
D. Andrey and Escape from Capygrad
\(1 \leq n, q\leq 2e5\)
题解
- 显然越向右移动越好,所以我们肯定选择\(l_i \leq x \leq b_i\)中较大的\(b_i\)传送,因为只有\(b_j > l_i\)时才能传送,所以实际上的区间为\([l_i, b_i]\)
- 所以问题转化为给定\(n\)个区间\([l_i,b_i]\),对于数轴上一点\(x > 0\),对于左端点\(l_i \leq x\)的区间,我们找到其最大的右端点
- 所以我们先考虑将区间合并\(O(nlogn)\)
- 然后离散化区间左端点和询问点
- 然后线段树在左端点处单点修改加上右端点的值即可,维护区间最大值
const int N = 4e5 + 10, M = 4e5 + 10;
int n, q;
struct node
{
int l, r, a, b;
bool operator<(const node &t) const
{
return l < t.l;
}
} line[N];
int l[N], r[N], tot, qry[N];
struct info
{
int mx;
friend info operator+(const info &a, const info &b)
{
info c;
c.mx = max(a.mx, b.mx);
return c;
}
};
struct SEG
{
info val;
} seg[N << 2];
void up(int id)
{
seg[id].val = seg[lson].val + seg[rson].val;
}
void build(int id, int l, int r)
{
if (l == r)
{
seg[id].val.mx = 0;
return;
}
int mid = l + r >> 1;
build(lson, l, mid);
build(rson, mid + 1, r);
up(id);
}
void change(int id, int l, int r, int x, int val)
{
if (l == r)
{
seg[id].val.mx = val;
return;
}
int mid = l + r >> 1;
if (x <= mid)
change(lson, l, mid, x, val);
else
change(rson, mid + 1, r, x, val);
up(id);
}
info query(int id, int l, int r, int ql, int qr)
{
if (ql <= l && r <= qr)
{
return seg[id].val;
}
int mid = l + r >> 1;
if (qr <= mid)
return query(lson, l, mid, ql, qr);
else if (ql > mid)
return query(rson, mid + 1, r, ql, qr);
else
return query(lson, l, mid, ql, qr) + query(rson, mid + 1, r, ql, qr);
}
void solve()
{
cin >> n;
for (int i = 1; i <= n; ++i)
{
int l, r, a, b;
cin >> l >> r >> a >> b;
line[i] = {l, r, a, b};
}
// 区间合并
sort(line + 1, line + n + 1);
tot = 0;
int st = line[1].l, ed = line[1].b;
line[n + 1] = {INF, INF, INF, INF};
for (int i = 2; i <= n + 1; ++i)
{
auto [L, R, a, b] = line[i];
if (L > ed)
{
tot++;
l[tot] = st, r[tot] = ed;
st = L, ed = b;
}
else
ed = max(ed, b);
}
vector<int> vec;
auto find = [&](int x)
{
return lower_bound(all(vec), x) - vec.begin() + 1;
};
for (int i = 1; i <= tot; ++i)
vec.push_back(l[i]);
cin >> q;
for (int i = 1; i <= q; ++i)
{
cin >> qry[i];
vec.push_back(qry[i]);
}
sort(all(vec));
vec.erase(unique(all(vec)), vec.end());
int m = vec.size() + 10;
build(1, 1, m);
for (int i = 1; i <= tot; ++i)
change(1, 1, m, find(l[i]), r[i]);
for (int i = 1; i <= q; ++i)
{
int x = find(qry[i]);
int mx = query(1, 1, m, 1, x).mx;
cout << max(mx, qry[i]) << " ";
}
cout << endl;
}