Codeforces Round 887 (Div. 1) 题解
https://codeforces.com/contest/1852/problems
A. Ntarsis' Set
https://codeforces.com/contest/1852/problem/A
感觉不是很一眼。
不能暴力,设当前集合为
扩展一下,集合
观察样例及其解释:
5 3
1 3 5 6 7
Day | ||
---|---|---|
设操作一次后的集合为
具体实现见代码。
/**
* author : OMG_78
* created: 2023-07-24-09.19.39
**/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
typedef long long ll;
typedef pair < int, int > PII;
typedef int itn;
mt19937 RND_MAKER (chrono :: steady_clock :: now ().time_since_epoch ().count ());
inline ll randomly (const ll l, const ll r) {return (RND_MAKER () ^ (1ull << 63)) % (r - l + 1) + l;}
bool Memory_Begins;
const double pi = acos (-1);
//__gnu_pbds :: tree < Key, Mapped, Cmp_Fn = std :: less < Key >, Tag = rb_tree_tag, Node_Upadte = null_tree_node_update, Allocator = std :: allocator < char > > ;
//__gnu_pbds :: tree < PPS, __gnu_pbds :: null_type, less < PPS >, __gnu_pbds :: rb_tree_tag, __gnu_pbds :: tree_order_statistics_node_update > tr;
inline bool ok (char c) {
if (c < '0') return true;
if (c > '9') return true;
return false;
}
template < class Z >
inline void read (Z &tmp) {
Z x = 0, f = 0;
char c = getchar ();
for ( ; ok (c) ; c = getchar ()) f = (c == '-') ? 1 : f;
for ( ; c >= '0' && c <= '9' ; c = getchar ()) x = (x << 1) + (x << 3) + (c & 15);
tmp = !f ? x : -x;
}
int n, k, cnt;
ll a[200005], b[210005][2], sm[210005];
inline ll qu (ll x) {
int L = 1, R = cnt;
while (R - L > 1) {
int mid = L + R >> 1;
if (x >= sm[mid - 1] + 1) L = mid;
else R = mid;
}
if (x >= sm[R - 1] + 1) {
x -= sm[R - 1], x --;
return b[R][0] + x;
}
else {
x -= sm[L - 1], x --;
return b[L][0] + x;
}
}
bool Memory_Ends;
signed main () {
fprintf (stderr, "%.3lf MB\n", (&Memory_Begins - &Memory_Ends) / 1048576.0);
int _; read (_);
while (_ --) {
read (n), read (k);
for (int i = 1;i <= n; ++ i) read (a[i]);
if (a[1] != 1) {
printf ("1\n");
continue;
}
cnt = 0;
for (int i = 1;i <= n; ++ i) {
if (i != n) {
if (a[i] + 1 <= a[i + 1] - 1) cnt ++, b[cnt][0] = a[i] + 1, b[cnt][1] = a[i + 1] - 1;
}
else {
cnt ++;
b[cnt][0] = a[n] + 1, b[cnt][1] = 9e18;
}
}
for (int i = 1;i <= cnt; ++ i) sm[i] = sm[i - 1] + b[i][1] - b[i][0] + 1;
ll u = b[1][0];
-- k;
while (k --) {
u = qu (u);
}
printf ("%lld\n ", u);
}
fprintf (stderr, "%.3lf ms\n", 1e3 * clock () / CLOCKS_PER_SEC);
return 0;
}
B. Imbalanced Arrays
https://codeforces.com/contest/1852/problem/B
考虑构造方案,显然
最后考虑可能有解的
最后检验一下就行了,如果实在满足不了就是无解。
/**
* author : OMG_78
* created: 2023-07-24-09.58.56
**/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
typedef long long ll;
typedef pair < int, int > PII;
typedef int itn;
mt19937 RND_MAKER (chrono :: steady_clock :: now ().time_since_epoch ().count ());
inline ll randomly (const ll l, const ll r) {return (RND_MAKER () ^ (1ull << 63)) % (r - l + 1) + l;}
bool Memory_Begins;
const double pi = acos (-1);
//__gnu_pbds :: tree < Key, Mapped, Cmp_Fn = std :: less < Key >, Tag = rb_tree_tag, Node_Upadte = null_tree_node_update, Allocator = std :: allocator < char > > ;
//__gnu_pbds :: tree < PPS, __gnu_pbds :: null_type, less < PPS >, __gnu_pbds :: rb_tree_tag, __gnu_pbds :: tree_order_statistics_node_update > tr;
inline bool ok (char c) {
if (c < '0') return true;
if (c > '9') return true;
return false;
}
template < class Z >
inline void read (Z &tmp) {
Z x = 0, f = 0;
char c = getchar ();
for ( ; ok (c) ; c = getchar ()) f = (c == '-') ? 1 : f;
for ( ; c >= '0' && c <= '9' ; c = getchar ()) x = (x << 1) + (x << 3) + (c & 15);
tmp = !f ? x : -x;
}
struct node {
int id, w;
} qwq[100005];
ll sm[100005], qws[100005];
inline bool cmp (node p1, node p2) {
return p1.w > p2.w;
}
int n, ans[100005];
inline bool solveq (int idx) {
set < int > aq; aq.clear ();
for (int i = 1;i <= n; ++ i) aq.insert (i);
for (int i = 1;i <= idx; ++ i) {
ans[i] = qwq[i].w - i + 1;
if (ans[i] <= 0) {
return false;
}
aq.erase (ans[i]);
}
for (int i = n;i > idx; -- i) {
ans[i] = *aq.rbegin ();
aq.erase (ans[i]);
ans[i] = -ans[i];
}
for (int i = 1;i <= n; ++ i) {
if (qwq[i].w && ans[qwq[i].w] + ans[i] <= 0) return false;
if (qwq[i].w < n && ans[qwq[i].w + 1] + ans[i] >= 0) return false;
}
printf ("YES\n");
for (int i = 1;i <= n; ++ i) qws[qwq[i].id] = ans[i];
for (int i = 1;i <= n; ++ i) printf ("%d ", qws[i]);
printf ("\n");
return true;
}
inline void solve () {
read (n);
for (int i = 1;i <= n; ++ i) read (qwq[i].w), qwq[i].id = i;
sort (qwq + 1, qwq + 1 + n, cmp);
for (int i = 1;i <= n; ++ i) sm[i] = sm[i - 1] + 1ll * qwq[i].w;
int idx = -1;
for (int k = 0;k <= n; ++ k) {
if (qwq[k].w >= k && sm[k] - (1ll * k) * (1ll * k) == sm[n] - sm[k]) {
if (solveq (k)) return ;
}
}
printf ("NO\n");
}
bool Memory_Ends;
signed main () {
fprintf (stderr, "%.3lf MB\n", (&Memory_Begins - &Memory_Ends) / 1048576.0);
int _; read (_);
while (_ --) solve ();
fprintf (stderr, "%.3lf ms\n", 1e3 * clock () / CLOCKS_PER_SEC);
return 0;
}
C. Ina of the Mountain
https://codeforces.com/contest/1852/problem/C
如果没有
可以将
考虑贪心地加
-
首先
与 加同样数量的 。 -
如果
,贡献为 ,那最好,不要再动了。 -
如果
,分两种情况:躺平, 的代价就可以了;给 的所有 都加上一个 ,对差分数组的贡献就是 。第二种情况考虑 反悔贪心。
首先用一个堆存每一个点加上
/**
* author : OMG_78
* created: 2023-07-25-09.39.18
**/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
typedef long long ll;
typedef pair < int, int > PII;
typedef int itn;
mt19937 RND_MAKER (chrono :: steady_clock :: now ().time_since_epoch ().count ());
inline ll randomly (const ll l, const ll r) {return (RND_MAKER () ^ (1ull << 63)) % (r - l + 1) + l;}
bool Memory_Begins;
const double pi = acos (-1);
//__gnu_pbds :: tree < Key, Mapped, Cmp_Fn = std :: less < Key >, Tag = rb_tree_tag, Node_Upadte = null_tree_node_update, Allocator = std :: allocator < char > > ;
//__gnu_pbds :: tree < PPS, __gnu_pbds :: null_type, less < PPS >, __gnu_pbds :: rb_tree_tag, __gnu_pbds :: tree_order_statistics_node_update > tr;
inline bool ok (char c) {
if (c < '0') return true;
if (c > '9') return true;
return false;
}
template < class Z >
inline void read (Z &tmp) {
Z x = 0, f = 0;
char c = getchar ();
for ( ; ok (c) ; c = getchar ()) f = (c == '-') ? 1 : f;
for ( ; c >= '0' && c <= '9' ; c = getchar ()) x = (x << 1) + (x << 3) + (c & 15);
tmp = !f ? x : -x;
}
int n, k, a, prevq;
#define prev prevq
ll ans;
priority_queue < int, vector < int >, greater < int > > heap;
bool Memory_Ends;
signed main () {
fprintf (stderr, "%.3lf MB\n", (&Memory_Begins - &Memory_Ends) / 1048576.0);
int _; read (_);
while (_ --) {
read (n), read (k);
prev = 0;
while (!heap.empty ()) heap.pop ();
ans = 0;
for (int i = 1;i <= n; ++ i) {
read (a);
a %= k;
if (prev > a) {
heap.push (a + k - prev);
}
else {
heap.push (a - prev);
ans += 1ll * heap.top ();
heap.pop ();
}
prev = a;
}
printf ("%lld\n", ans);
}
fprintf (stderr, "%.3lf ms\n", 1e3 * clock () / CLOCKS_PER_SEC);
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通