Codeforces Round #697 (Div. 3)A~G解题报告
题 A Odd Divisor
题目介绍
解题思路
乍一想本题,感觉有点迷迷糊糊,但是证难则反,直接考虑没有奇数因子的情况,即 N =
2
i
2^{i}
2i,那么当N !=
2
i
2^i
2i时,就有 奇数因子
注意使用 LL
AC代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
bool Check(LL x)
{
while (x != 1)
{
if (x & 1)
{
return true;
}
x >>= 1;
}
return false;
}
int main()
{
LL n;
int t;
cin >> t;
while (t -- )
{
scanf("%lld", &n);
if (Check(n))
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
return 0;
}
题 B New Year’s Number
题目介绍
解题思路
直接就是一个 dp裸题,倘若 x 是2021 与 2020 的若干和,那么 x == 2020 或 x ==2021或者x - 2020 满足要求 或者 x - 2021满足要求
有了上述的递推公式,直接开bool数组进行动态规划即可
AC代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 1000010;
bool st[N];
int main()
{
int n;
int t;
cin >> t;
st[2020] = st[2021] = true;
for (int i = 2023; i < N; i ++ )
{
st[i] = st[i - 2021] | st[i - 2020];
}
while (t -- )
{
scanf("%d", &n);
if (st[n])
{
cout << "YES\n";
}
else
{
cout << "NO\n";
}
}
return 0;
}
题 C Ball in Berland
题目介绍
解题思路
同样是直接统计不太方便,我们直接反向思考,计算出总方案数量,减去不合法方案数量,得到结果
总方案数量 = k * (k - 1) / 2
不合法方案数量 = 同一个男生被选中两次 + 同一个女生被选中两次
记得开 LL
AC代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 200010;
int boy_cnt[N], girl_cnt[N];
int n, m, k;
int main()
{
int t;
cin >> t;
LL res = 0;
while (t -- )
{
scanf("%d%d%d", &n, &m, &k);
res = LL(k) * (k - 1);
memset(boy_cnt, 0, sizeof boy_cnt);
memset(girl_cnt, 0, sizeof girl_cnt);
for (int i = 1; i <= k; i ++ )
{
static int tmp;
scanf("%d", &tmp);
boy_cnt[tmp] ++;
}
for (int i = 1; i <= k; i ++ )
{
static int tmp;
scanf("%d", &tmp);
girl_cnt[tmp] ++;
}
for (int i = 1; i <= n; i ++ )
{
res -= LL(boy_cnt[i]) * (boy_cnt[i] - 1);
}
for (int i = 1; i <= m; i ++ )
{
res -= LL(girl_cnt[i]) * (girl_cnt[i] - 1);
}
/// cout << "#############\n";
printf("%lld\n", res / 2);
/// cout << res / 2 << endl;
}
return 0;
}
题 D Cleaning the Phone
题目介绍
解题思路
错误思路
本来将题目想成了 dp 进行求解,直接超时没商量,考虑一下复杂度,确实有问题
O(N*2N)太大
正解
这个题目应该进行贪心的,先处理出来
b
i
b_i
bi=1数组,
b
i
b_i
bi=2数组,然后对数组可以清空的内存进行排序。
排序后进行求取数组的前缀和,方便我们下面两种做法降低复杂度。
下面有两种问题的求解办法,
方法一、二分
对于 对于 每个
b
i
b_i
bi=1的下标进行枚举,然后对
b
i
b_i
bi=2数组进行二分,查找到满足释放内存的最小前缀数组的下标。
O
(
N
l
o
g
(
N
)
)
O(Nlog(N))
O(Nlog(N))
方法二、双指针
先找到一个合法解,然后数组下标进行移动,另一个指针作相应的调整即可。
O
(
N
)
O(N)
O(N)
但是算上排序,最终复杂度为
O
(
N
l
o
g
(
N
)
)
O(Nlog(N))
O(Nlog(N))
但是本题有一个最狗的地方,cmp函数被卡了,可以直接写归并排序,或者cmp函数别写等号,否则会超时
AC代码
双指针
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 200010, INF = 0x3f3f3f3f;
LL a[N];
LL c[N], d[N];
LL n, m;
int idx1, idx2;
bool cmp(LL x, LL y){ return x > y; }
void show()
{
for (int i = 1; i <= idx1; i ++ ) cout << c[i] << " "; cout << endl;
for (int i = 1; i <= idx2; i ++ ) cout << d[i] << " "; cout << endl;
}
int main()
{
int T; cin >> T;
while (T -- )
{
scanf("%lld %lld", &n, &m);
for (LL i = 1; i <= n; i ++ )
scanf("%lld", &a[i]);
c[0] = d[0] = 0LL;
idx1 = idx2 = 0;
for (LL i = 1, b; i <= n; i ++ )
{
scanf("%lld", &b);
if (b & 1) c[++ idx1] = (a[i]);
else d[++ idx2] = (a[i]);
}
sort(c + 1, c + idx1 + 1, cmp);
sort(d + 1, d + idx2 + 1, cmp);
/// show();
for (int i = 1; i <= idx1; i ++ ) c[i] += c[i - 1];
for (int i = 1; i <= idx2; i ++ ) d[i] += d[i - 1];
/// show();
if (c[idx1] + d[idx2] < m)
{
printf("-1\n");
}
else
{
int i, j, res = INF;
for (i = 0; i <= idx1; i ++ ) // 尺取法的起点
if (c[i] + d[idx2] >= m) // c[i] 的开头
break;
j = idx2;
res = min(res, i + j + j);
// 此时i, j可以进行 尺取法 了
while (i <= idx1 && j >= 0)
{
while (j >= 0 && i <= idx1 && c[i] + d[j] < m)
{
i ++;
}
if (i <= idx1 && j >= 0)
res = min(res, i + j + j);
while (i <= idx1 && j >= 0 && c[i] + d[j] >= m)
{
if (i <= idx1 && j >= 0)
res = min(res, i + j + j);
j --;
}
}
if (res == INF) res = -1;
printf("%d\n", res);
}
}
return 0;
}
二分
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 200010, INF = 0x3f3f3f3f;
int n, m;
int a[N];
LL c[N], d[N];
int szc, szd;
bool cmp(LL a, LL b)
{
return a > b;
}
int main()
{
int T; cin >> T;
while (T -- )
{
static int b;
cin >> n >> m;
for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
c[0] = d[0] = szc = szd = 0;
for (int i = 1; i <= n; i ++ )
{
scanf("%d", &b);
if (b & 1)
c[++ szc] = a[i];
else
d[++ szd] = a[i];
}
sort(c + 1, c + szc + 1, cmp);
sort(d + 1, d + szd + 1, cmp);
for (int i = 1; i <= szc; i ++ ) c[i] += c[i - 1];
for (int i = 1; i <= szd; i ++ ) d[i] += d[i - 1];
if (c[szc] + d[szd] < m)
{
puts("-1");
}
else
{
int res = INF;
for (int i = 0; i <= szc; i ++ )
{
if (c[i] + d[szd] < m)
continue;
else if (c[i] >= m)
{
res = min(res, i);
break;
}
else
{
static int tmp;
tmp = lower_bound(d + 1, d + 1 + szd, m - c[i]) - d;
res = min(res, tmp + tmp + i);
}
}
cout << res << endl;
}
}
return 0;
}
题 E Advertising Agency
题目介绍
解题思路
肯定是先对 博主的 粉丝数量进行排序,贪心的请博主即可,这个题目主要是求解 排列组合问题。
C
i
j
C_i^j
Cij=
C
i
−
1
j
C_{i-1}^j
Ci−1j+
C
i
−
1
j
−
1
C_{i-1}^{j-1}
Ci−1j−1
利用dp直接进行求解,关键是初始化写好就可以了
C
i
i
C_i^i
Cii=
C
i
0
C_i^0
Ci0=1
AC代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 1010, INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int n, k;
int a[N];
int f[N][N];
bool cmp(LL a, LL b)
{
return a > b;
}
int main()
{
memset(f, 0, sizeof f);
for (int i = 0; i < N; i ++ )
f[i][i] = f[i][0] = 1;
for (int i = 1; i < N; i ++ )
for (int j = 1; j <= i; j ++ )
f[i][j] = (f[i - 1][j] + f[i - 1][j - 1]) % MOD;
int T; cin >> T;
while (T -- )
{
cin >> n >> k;
for (int i = 1; i <= n; i ++ )
scanf("%d", &a[i]);
sort(a + 1, a + n + 1, cmp);
static int x, sidx, eidx;
x = a[k], sidx = -1, eidx = -1;
for (int i = 1; i <= n; i ++ )
{
if (a[i] == x)
{
if (sidx == -1) sidx = i;
eidx = i;
}
}
cout << f[eidx - sidx + 1][k - sidx + 1] << endl;
}
return 0;
}
题 F Unusual Matrix
题目介绍
解题思路
题目问的是能否从 A n ∗ n A_{n*n} An∗n矩阵转换到 B n ∗ n B_{n*n} Bn∗n矩阵,由转换的性质,同一个行/列转换两次是没有任何作用的,因此我们枚举第一行需要操作/与不需要操作,那么第一行的元素能操作的对象只有列,因此列是否需要操作就得以确定,列确定,那么,反过来行也就得以确定,最终得到结果。
AC代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 1010, INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int n;
char a[N][N], b[N][N];
char c[N][N];
inline void Change(char c[][N], int x, bool row);
bool Same(char c[][N], char d[][N], int x)
{
for (int i = 1; i <= n; i ++ )
if (c[x][i] != d[x][i])
return false;
return true;
}
bool Check(char c[][N], char d[][N])
{
// 第一行是不需要动的,我们看看 列 的影响
for (int i = 1; i <= n; i ++ )
if (c[1][i] != d[1][i])
Change(c, i, false);
for (int i = 2; i <= n; i ++ )
{
if (c[i][1] != d[i][1]) // 修改 行
Change(c, i, true);
if (!Same(c, d, i))
return false;
}
return true;
}
inline void Change(char c[][N], int x, bool row)
{
if (row) // row
for (int i = 1; i <= n; i ++ )
{
c[x][i] = 97 - c[x][i]; // 48 + 49 - c[i]
}
else // col
for (int i = 1; i <= n; i ++ )
{
c[i][x] = 97 - c[i][x]; // 48 + 49 - c[i]
}
}
int main()
{
int T; cin >> T;
while (T -- )
{
cin >> n;
for (int i = 1; i <= n; i ++ )
scanf("%s", a[i] + 1);
for (int i = 1; i <= n; i ++ )
scanf("%s", b[i] + 1);
memcpy(c, a, sizeof c);
if (Check(c, b) || (memcpy(c, a, sizeof c), Change(c, 1, true), Check(c, b)))
puts("YES");
else
puts("NO");
}
return 0;
}
题 G Strange Beauty
题目介绍
解题思路
这个题目是一个比较巧妙地dp题目,对于一个
B
e
u
t
i
f
u
l
A
r
r
a
y
Beutiful Array
BeutifulArray我们将其非降序排序之后可以发现,后面的数字都是可以整除前面的,这是一个充分必要的条件
那么最长的数组对应着最长的整除序列
而且还有 一个坑点,鸡儿数字还可能相等,也就是我们需要先预处理出
X
X
X出现的次数
定义一个数组
f
i
f_i
fi表示,以数字 i 作为最大值,可以构成
B
e
a
u
t
i
f
u
l
A
r
r
a
y
BeautifulArray
BeautifulArray的最大长度,
f
i
f_i
fi =
i
i
i出现次数+
m
a
x
max
max{因子的 j 的
f
j
f_j
fj}
下面是dp过程,而且为了方便书写,降低时间复杂度,直接将因子的相加写入了 因子的循环中
AC代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 200010, INF = 0x3f3f3f3f;
int cnt[N];
int a[N];
bool st[N];
int f[N];
int n;
int main()
{
int T; cin >> T;
while (T -- )
{
static int res;
cin >> n;
for (int i = 1; i <= n; i ++ )
scanf("%d", &a[i]);
res = INF;
sort(a + 1, a + 1 + n);
memset(cnt, 0, sizeof cnt);
memset(st, false, sizeof st);
memset(f, 0, sizeof f);
for (int i = 1; i <= n; i ++ ) cnt[a[i]] ++;
for (int i = 1, val; i <= n; i ++ )
{
val = a[i];
if (st[val]) continue;
st[val] = true;
// cnt[val] = max(cnt[val], 1);
f[val] = f[val] + cnt[val]; // 给自己加的
for (int j = val + val; j < N; j += val)
{
f[j] = max(f[j], f[val]); // 给别的数字加的
}
res = min(res, n - f[val]);
}
cout << res << endl;
}
return 0;
}
本次CF小结
- 小心快排可能被卡,导致超时,可以通过 修改cmp函数,或者是直接使用 归并排序来解决
- 其次,考虑问题的时候,尤其是数量的问题,可以使用容斥定理,证难则反
- 贪心、结合二分、或者是双指针来优化复杂度,有时候考虑dp背包复杂度太高
- 求解组合数的常用方法要记住, dp,逆元,卢卡斯定理