比赛地址: https://codeforces.com/contest/2040

A. Game of Division

题目

https://codeforces.com/contest/2040/problem/A

题意

给你一个长度为 \(n\) 的整数数组 \(a_1, a_2, \ldots, a_n\) 和一个整数数组 \(k\) 。

两个玩家正在玩一个游戏。第一个玩家选择一个索引 \(1 \le i \le n\) 。然后第二个玩家选择不同的索引 \(1 \le j \le n, i \neq j\) 。如果 \(|a_i - a_j|\) 不能被 \(k\) 整除,则第一个玩家获胜。否则,第二位棋手获胜。

我们扮演第一个玩家。确定是否可能获胜,如果可能,应该选择哪个索引 \(i\) 。

数字 \(x\) 的绝对值用 \(|x|\) 表示,如果是 \(x \ge 0\) ,则等于 \(x\) ,否则等于 \(-x\) 。

思路

模拟

AC代码

点击查看代码
#define _USE_MATH_DEFINES // To use the definition of cmath

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long; // mp.reserve(1024), mp.max_load_factor(0.75);
// Used only for basic types, pair and tuple.
template<typename T>
struct custom_hash_base {
size_t operator()(const T& x) const {
static const size_t seed = chrono::steady_clock::now().time_since_epoch().count();
return _Hash_bytes(&x, sizeof(x), seed);
}
}; static const auto _ = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifndef ONLINE_JUDGE
freopen("../in.txt", "r", stdin);
#endif
return nullptr;
}(); int nums[101], k;
int n;
int st[101]; inline void solve() {
cin >> n >> k;
memset(st, 0, sizeof(int) * (k + 1));
for (int i = 1; i <= n; ++i) {
cin >> nums[i];
}
for (int i = 1; i <= n; ++i) {
bool flag = true;
for (int j = 1; j <= n && flag; ++j) {
if (i == j) continue;
if (abs(nums[i] - nums[j]) % k == 0) flag = false;
}
if (flag) {
cout << "YES\n" << i << "\n";
return;
}
}
cout << "NO\n";
} int main() {
int T;
for (cin >> T; T > 0; --T) {
solve();
}
return 0;
}

B. Paint a Strip

题目

https://codeforces.com/contest/2040/problem/B

题意

您有一个长度为 \(n\) 的零数组 \(a_1, a_2, \ldots, a_n\) 。

你可以对它进行两种操作:

  1. 在 \(1 \le i \le n\) 和 \(a_i = 0\) 之间选择一个索引 \(i\) ,并将 \(1\) 赋值给 \(a_i\) ;
  2. 选择一对索引 \(l\) 和 \(r\) ,使得 \(1 \le l \le r \le n\) , \(a_l = 1\) , \(a_r = 1\) , \(a_l + \ldots + a_r \ge \lceil\frac{r - l + 1}{2}\rceil\) ,并将所有 \(l \le i \le r\) 的 \(1\) 赋值给 \(a_i\) 。

要使数组中的所有元素都等于 1,至少需要进行多少次第一种类型的运算?

思路

第 \(i\) 次第一种类型的运算,可覆盖的最大范围为第 \(i - 1\) 次的范围加1,再乘2。

先初始化每一个i的范围,再二分查找。

AC代码

点击查看代码
#define _USE_MATH_DEFINES // To use the definition of cmath

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long; // mp.reserve(1024), mp.max_load_factor(0.75);
// Used only for basic types, pair and tuple.
template<typename T>
struct custom_hash_base {
size_t operator()(const T& x) const {
static const size_t seed = chrono::steady_clock::now().time_since_epoch().count();
return _Hash_bytes(&x, sizeof(x), seed);
}
}; static const auto _ = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifndef ONLINE_JUDGE
freopen("../in.txt", "r", stdin);
#endif
return nullptr;
}(); int n;
constexpr int N = 20;
ll st[N]; static const auto init= []() {
st[1] = 1;
for (int i = 2; i < N; ++i) {
st[i] = (st[i - 1] + 1) << 1;
}
return 0;
}(); inline void solve() {
cin >> n;
int p = lower_bound(st + 1, st + N, n) - st - 1;
while (st[p] < n) ++p;
cout << p << '\n';
} int main() {
int T;
for (cin >> T; T > 0; --T) {
solve();
}
return 0;
}

C. Ordered Permutations

题目

https://codeforces.com/contest/2040/problem/C

  • time limit per test: 2 seconds
  • memory limit per test: 256 megabytes
  • input: standard input
  • output: standard output

Consider a permutation\(^{\text{∗}}\) \(p_1, p_2, \ldots, p_n\) of integers from \(1\) to \(n\). We can introduce the following sum for it\(^{\text{†}}\):

\[S(p) = \sum_{1 \le l \le r \le n} \min(p_l, p_{l + 1}, \ldots, p_r)
\]

Let us consider all permutations of length \(n\) with the maximum possible value of \(S(p)\). Output the \(k\)-th of them in lexicographical\(^{\text{‡}}\)order, or report that there are less than \(k\) of them.

\(^{\text{∗}}\)A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).

\(^{\text{†}}\)For example:

  • For the permutation \([1, 2, 3]\) the value of \(S(p)\) is equal to \(\min(1) + \min(1, 2) + \min(1, 2, 3) + \min(2) + \min(2, 3) + \min(3) =\) \(1 + 1 + 1 + 2 + 2 + 3 = 10\)
  • For the permutation \([2, 4, 1, 3]\) the value of \(S(p)\) is equal to \(\min(2) + \min(2, 4) + \min(2, 4, 1) + \min(2, 4, 1, 3) \ +\) $ \min(4) + \min(4, 1) + \min(4, 1, 3) \ +$ \(\min(1) + \min(1, 3) \ +\) \(\min(3) =\) \(2 + 2 + 1 + 1 + 4 + 1 + 1 + 1 + 1 + 3 = 17\).

\(^{\text{‡}}\)An array \(a\) is lexicographically smaller than an array \(b\) if and only if one of the following holds:

  • \(a\) is a prefix of \(b\), but \(a \ne b\); or
  • in the first position where \(a\) and \(b\) differ, the array \(a\) has a smaller element than the corresponding element in \(b\).

Input

Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.

The only line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\); \(1 \le k \le 10^{12}\)) — the length of the permutation and the index number of the desired permutation.

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10 ^ 5\).

Output

For each test case, if there are less than \(k\) suitable permutations, print \(-1\).

Otherwise, print the \(k\)-th suitable permutation.

Example

点击查看测试样例

Input

6
3 2
3 3
4 11
4 6
6 39
7 34

Output

1 3 2
2 3 1
-1
2 4 3 1
-1
2 3 4 5 7 6 1

Note

Let us calculate the required sum for all permutations of length \(3\) (ordered lexicographically):

Permutation Value of \(S(p)\)
\([1, 2, 3]\) \(10\)
\([1, 3, 2]\) \(10\)
\([2, 1, 3]\) \(9\)
\([2, 3, 1]\) \(10\)
\([3, 1, 2]\) \(9\)
\([3, 2, 1]\) \(10\)

In the first test case, you have to print the second suitable permutation of length \(3\). Looking at the table, we see that it is the permutation \([1, 3, 2]\).

In the second test case, you have to print the third suitable permutation of length \(3\). Looking at the table, we see that it is the permutation \([2, 3, 1]\).

题意

考虑从 \(1\) 到 \(n\) 的整数 \(^{\text{∗}}\) 的排列。从 \(1\) 到 \(n\) 的整数的排列组合 \(p_1, p_2, \ldots, p_n\) 。我们可以为它引入下面的和 \(^{\text{†}}\) :

\[S(p) = \sum_{1 \le l \le r \le n} \min(p_l, p_{l + 1}, \ldots, p_r)
\]

让我们考虑所有长度为 \(n\) 的排列,其最大可能值为 \(S(p)\) 。按词典 \(^{\text{‡}}\) 顺序输出其中的第 \(k\) 个,或者报告它们的数量少于 \(k\) 。

\(^{\text{∗}}\) 长度为 \(n\) 的排列是由 \(n\) 个不同的整数组成的数组,这些整数从 \(1\) 到 \(n\) 按任意顺序排列。例如, \([2,3,1,5,4]\) 是一个排列,但 \([1,2,2]\) 不是一个排列( \(2\) 在数组中出现了两次), \([1,3,4]\) 也不是一个排列( \(n=3\) ,但数组中有 \(4\) )。

\(^{\text{†}}\) 例如

  • 对于 \([1, 2, 3]\) 这个排列, \(S(p)\) 的值等于 \(\min(1) + \min(1, 2) + \min(1, 2, 3) + \min(2) + \min(2, 3) + \min(3) =\) 。 \(1 + 1 + 1 + 2 + 2 + 3 = 10\)
  • 对于排列 \([2, 4, 1, 3]\) 来说, \(S(p)\) 的值等于 \(\min(2) + \min(2, 4) + \min(2, 4, 1) + \min(2, 4, 1, 3) \ +\) $ \min(4) + \min(4, 1) + \min(4, 1, 3) \ +$ 。 \(\min(1) + \min(1, 3) \ +\) \(\min(3) =\) \(2 + 2 + 1 + 1 + 4 + 1 + 1 + 1 + 1 + 3 = 17\) .

\(^{\text{‡}}\) 当且仅当以下条件之一成立时,数组 \(a\) 的lexicographically小于数组 \(b\) :

  • \(a\) 是 \(b\) 的前缀,但是 \(a \ne b\) ;或者
  • 在 \(a\) 和 \(b\) 不同的第一个位置,数组 \(a\) 中的元素小于 \(b\) 中的相应元素。

输入

每个测试包含多个测试用例。第一行包含测试用例的数量 \(t\) ( \(1 \le t \le 10^4\) )。测试用例说明如下。

每个测试用例的唯一一行包含两个整数 \(n\) 和 \(k\) ( \(1 \le n \le 2 \cdot 10^5\) ;{47522742})。( \(1 \le n \le 2 \cdot 10^5\) ; \(1 \le k \le 10^{12}\) ) - 排列的长度和所需排列的索引号。

保证所有测试用例中 \(n\) 的总和不超过 \(2 \cdot 10 ^ 5\) 。

输出

对于每个测试用例,如果合适的排列组合少于 \(k\) ,则打印 \(-1\) 。

否则,打印 \(k\) 个合适的排列。

备注

让我们计算所有长度为 \(3\) (按词典顺序排列)的排列所需的和:

Permutation \(S(p)\) 的值
\([1, 2, 3]\) \(10\)
\([1, 3, 2]\) \(10\)
\([2, 1, 3]\) \(9\)
\([2, 3, 1]\) \(10\)
\([3, 1, 2]\) \(9\)
\([3, 2, 1]\) \(10\)

在第一个测试用例中,您必须打印长度为 \(3\) 的第二个合适的排列。观察表格,我们会发现是长度为 \([1, 3, 2]\) 的排列。

在第二个测试用例中,您必须打印长度为 \(3\) 的第三个合适的排列。观察表格,我们会发现是长度为 \([2, 3, 1]\) 的排列。

思路

通过打表,可以观察到。值为最大的 \(S(p)\) 排列的数量为 \(2^{n - 1}\)。

所以只要值为最大的 \(S(p)\) 排列的数量不超过 \(k\) 则有解,否则输出 -1

这里给出 \(n\) 为 5 的情况:

 1: 1 2 3 4 5 = 35

 2: 1 2 3 5 4 = 35

 3: 1 2 4 5 3 = 35
4: 1 2 5 4 3 = 35 5: 1 3 4 5 2 = 35
6: 1 3 5 4 2 = 35
7: 1 4 5 3 2 = 35
8: 1 5 4 3 2 = 35 9: 2 3 4 5 1 = 35
10: 2 3 5 4 1 = 35
11: 2 4 5 3 1 = 35
12: 2 5 4 3 1 = 35
13: 3 4 5 2 1 = 35
14: 3 5 4 2 1 = 35
15: 4 5 3 2 1 = 35
16: 5 4 3 2 1 = 35

假设 \(k\) 取 7:0b0111

会发现 7 是从 3:0b0011 的后 3 位,往前挪动 1 位形成的。

而 3 是从 1:0b0001 的后两位,往前挪动 1 位形成的。

同样的 4:0b0100 是以同样的方式从 2:0b0010 转移过来的。


得出结论:第 \(k\) 个排列是第 prev_k(把 \(k\) 移除二进制的最高位,如果二进制bit 1的个数为1则左移1位)个排列的后 m(k 二进制的最左边1的位置) 位往前挪动1位形成的。

AC代码

点击查看代码
#define _USE_MATH_DEFINES // To use the definition of cmath

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long; // mp.reserve(1024), mp.max_load_factor(0.75);
// Used only for basic types, pair and tuple.
template<typename T>
struct custom_hash_base {
size_t operator()(const T& x) const {
static const size_t seed = chrono::steady_clock::now().time_since_epoch().count();
return _Hash_bytes(&x, sizeof(x), seed);
}
}; static const auto _ = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifndef ONLINE_JUDGE
freopen("../in.txt", "r", stdin);
#endif
return nullptr;
}(); ll n, k; inline ll get_next(ll x) {
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
++x;
return x;
} inline void dfs(ll ck, vector<ll>& v) {
if (ck <= 1)
return;
if (__builtin_popcountll(ck) == 1)
dfs(ck >> 1, v);
else
dfs(ck - (get_next(ck) >> 1), v);
ck = get_next(ck);
int i = 64 - __builtin_clzll(ck) - 1;
ll x = v[i];
v.erase(v.begin() + i);
v.insert(v.begin(), x);
} inline void solve() {
cin >> n >> k;
const int ci = 64 - __builtin_clzll(get_next(k));
if (n < ci) {
cout << -1 << '\n';
return;
}
vector<ll> v(n);
iota(v.rbegin(), v.rend(), 1LL);
dfs(k, v);
ranges::reverse(v);
ranges::copy(v, ostream_iterator<ll>(cout, " "));
cout << '\n';
} int main() {
int T;
for (cin >> T; T > 0; --T) {
solve();
}
return 0;
}

阅读原文

Codeforces Round 992 (Div. 2) 解题报告的更多相关文章

  1. Codeforces Round #324 (Div. 2)解题报告

    ---恢复内容开始--- Codeforces Round #324 (Div. 2) Problem A 题目大意:给二个数n.t,求一个n位数能够被t整除,存在多组解时输出任意一组,不存在时输出“ ...

  2. Codeforces Round #382 (Div. 2) 解题报告

    CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...

  3. Codeforces Round #380 (Div. 2) 解题报告

    第一次全程参加的CF比赛(虽然过了D题之后就开始干别的去了),人生第一次codeforces上分--(或许之前的比赛如果都参加全程也不会那么惨吧),终于回到了specialist的行列,感动~.虽然最 ...

  4. Codeforces Round #216 (Div. 2)解题报告

    又范低级错误! 只做了两题!一道还被HACK了,囧! A:看了很久!应该是到语文题: 代码:#include<iostream> #include<];    ,m2=;    ;i ...

  5. Codeforces Round #281 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/493 A题 写完后就交了,然后WA了,又读了一遍题,没找出错误后就开始搞B题了,后来回头重做的时候才发现,球员被红牌罚下场后还可 ...

  6. Codeforces Round #277 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/486 A题.Calculating Function 奇偶性判断,简单推导公式. #include<cstdio> ...

  7. Codeforces Round #276 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/485 A题.Factory 模拟.判断是否出现循环,如果出现,肯定不可能. 代码: #include<cstdio> ...

  8. Codeforces Round #350 (Div. 2)解题报告

    codeforces 670A. Holidays 题目链接: http://codeforces.com/contest/670/problem/A 题意: A. Holidays On the p ...

  9. Codeforces Round #479 (Div. 3)解题报告

    题目链接: http://codeforces.com/contest/977 A. Wrong Subtraction 题意 给定一个数x,求n次操作输出.操作规则:10的倍数则除10,否则减1 直 ...

  10. Codeforces Round #515 (Div. 3) 解题报告(A~E)

    题目链接:http://codeforces.com/contest/1066 1066 A. Vova and Train 题意:Vova想坐火车从1点到L点,在路上v的整数倍的点上分布着灯笼,而在 ...

随机推荐

  1. Unity中的光源类型(向前渲染路径进行光照计算)

    Unity中的光源类型 Unity中共支持4种光源类型: 平行光 点光源 聚光灯 面光源(在光照烘焙时才可以发挥作用) 光源的属性: 位置 方向(到某个点的方向) 颜色 强度 衰减(到某个点的衰减) ...

  2. 均值回归策略在A股ETF市场获利的可能性

    如何在股票市场获利 曾经有人告诉我一个在股票市场赚钱的秘诀,只要掌握这个秘诀,赚钱就像捡钱一样容易.他说:这个秘诀其实很简单,就是在股票价格低的时候买入,在价格高的时候卖出. 啧啧,不愧是秘诀,明明是 ...

  3. 《MongoDB游记之轻松入门到进阶》代码下载

    <MongoDB游记之轻松入门到进阶>代码下载,看看有没有用 http://pan.baidu.com/s/1boKG28R https://item.jd.com/12236244.ht ...

  4. CentOS开放端口的方法(转载)

    CentOS开放端口的方法 转载:https://www.itfeichai.com/centos-open-porter/ Centos升级到7之后,内置的防火墙已经从iptables变成了fire ...

  5. Android 基于 Choreographer 的渲染机制详解

    本文介绍了 App 开发者不经常接触到但是在 Android Framework 渲染链路中非常重要的一个类 Choreographer.包括 Choreographer 的引入背景.Choreogr ...

  6. uniapp电子签名盖章实现详解

    项目开发中用到了电子签名.签好名的图片需要手动实现横竖屏旋转.并将绘制的签名图片放到pdf转换后的base64的图片上,可以手动拖动签名到合适的位置,最后合成签名和合同图片并导出.和以往一样,先发一下 ...

  7. 云原生周刊:Kubernetes v1.31 发布

    开源项目推荐 Kardinal Kardinal 是一个用于在共享 Kubernetes 集群中创建超轻量级临时开发环境的框架. Anteon Anteon(以前称为 Ddosify)是一个开源的.基 ...

  8. Nuxt.js 应用中的 app:templates 事件钩子详解

    title: Nuxt.js 应用中的 app:templates 事件钩子详解 date: 2024/10/18 updated: 2024/10/18 author: cmdragon excer ...

  9. 强化学习算法笔记之【DDPG算法】

    强化学习笔记之[DDPG算法] 目录 强化学习笔记之[DDPG算法] 前言: 原论文伪代码 DDPG 中的四个网络 代码核心更新公式 前言: 本文为强化学习笔记第二篇,第一篇讲的是Q-learning ...

  10. Python-提高-2

    阅读目录 1.多继承以及MRO顺序 2.再论静态方法和类方法 3.property属性-讲解 4.property属性-应用 5.魔法属性 6.面向对象设计 7.with与"上下文管理器&q ...