Pinely Round 4 (Div. 1 + Div. 2)
题目链接:Pinely Round 4 (Div. 1 + Div. 2)
总结:被B卡了一年。
A. Maximize the Last Element
tag:模拟
Description:给定一个长度为奇数的数组,每次可以删除两个相邻的元素,直到剩下一个元素为止,求该元素的最大值。
Solution:模拟发现之后保留奇数位的值。
void solve(){
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i ++)
cin >> a[i];
int ans = 0;
for (int i = 0; i < n; i += 2)
ans = max(ans, a[i]);
cout << ans << endl;
}
B. AND Reconstruction
tag:构造
Description:给定一个长为\(n - 1\)的数组\(b\),构造一个长度为\(n\)的数组\(a\),满足\(a_i \& a_{i + 1} == b_i\),否则输出\(-1\)。
Solution:我们令\(a_1 == b_1\)。然后我们发现\(a_i\)会影响\(b_i\)与\(b_{i - 1}\),那么\(a_i\)包含最少的\(1\)时,必须为\(b_{i - 1} | b_i\)。最后我们令\(a_n == b_{n - 1}\)。
Competing:一开始就想到了,但是思路不清晰,气。
void solve(){
cin >> n;
vector<int> b(n), a(n);
for (int i = 0; i < n - 1; i ++){
cin >> b[i];
}
a[0] = b[0];
for (int i = 1; i < n - 1; i ++){
a[i] = b[i] | b[i - 1];
}
a[n - 1] = b[n - 2];
for (int i = 0; i < n - 1; i ++){
if (b[i] != (a[i] & a[i + 1])){
cout << -1 << endl;
return;
}
}
for (int i = 0; i < n; i ++)
cout << a[i] << " \n"[i + 1 == n];
}
C. Absolute Zero
tag:构造
Description:给定一个长度为\(n\)的数组,每次操作可以选定一个\(x\),将\(a_i\)更新为\(|a_i - x|\)。构造一个操作序列不操作\(40\)次,使数组\(a\)全为\(0\).
Solution:模拟几组样例发现,如果数组中所有元素的奇偶性必须相同,否则当一个数变为奇数,另一个数会变成偶数。(开始时这两个数奇偶性不同)。
- 我们只需要每次减去\((r + l) / 2\),逐渐缩小上界即可。
- 可以使用\(2^{31}, 2^{30}, ... 1\)。逐渐缩小上界。
Competing:将\(r +l\)写为\(r - l\)
void solve(){
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i ++){
cin >> a[i];
}
sort(a.begin(), a.end());
for (int i = 1; i < n; i ++){
if ((a[i] - a[i - 1]) & 1){
cout << -1 << endl;
return;
}
}
if (a[0] == a[n - 1]){
if (a[0] == 0){
cout << 0 << endl << endl;
return;
}
cout << 1 << endl;
cout << a[0] << endl;
return;
}
vector<int> ans;
while (a[0] != a[n - 1]){
int t = (a[n - 1] + a[0]) / 2;
ans.eb(t);
for (int i = 0; i < n; i ++){
a[i] = abs(a[i] - t);
}
sort(a.begin(), a.end());
}
if (a[0] != 0)
ans.eb(a[0]);
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i ++)
cout << ans[i] << " \n"[i + 1 == ans.size()];
}
D. Prime XOR Coloring
tag:构造
四色定理:“将平面任意地细分为不相重叠的区域,每一个区域总可以用1234这四个数字之一来标记而不会使相邻的两个区域得到相同的数字。”这里所指的相邻区域是指有一整段边界是公共的。如果两个区域只相遇于一点或有限多点就不叫相邻的。
Description:有\(n\)个顶点,编号从\(1\)到\(n\),当\(u \oplus v\)为质数时,\(u\)和\(v\)之间有一条边。用最少的颜色给所有顶点染色,要求相邻顶点的颜色不能相同。
Solution:考虑哪些点之间连边太难,考虑哪些点之间不连边。
- 根据四色定理知,当\(n >= 6\)时,一定需要四种颜色。
- 考虑所有质数,发现除了\(2\)都是奇数。\(2\)的二进制\(01\),\(3\)的二进制\(10\),\(4\)的二进制\(00\)。那么我们对\(4\)取余,\(mod4\)相同的数异或之后一定不是质数。
- 然后特判\(n <= 5\)。
void solve(){
cin >> n;
if (n == 1){
cout << 1 << endl << 1 << endl;
}
else if (n == 2){
cout << 2 << endl;
cout << 1 << " " << 2 << endl;
}
else if (n == 3){
cout << 2 << endl;
cout << "1 2 2\n";
}
else if (n == 4){
cout << 3 << endl;
cout << "1 2 2 3\n";
}
else if (n == 5){
cout << 3 << endl;
cout << "1 2 2 3 3\n";
}
else{
cout << 4 << endl;
for (int i = 1; i <= n; i ++){
cout << (i % 4) + 1 << " \n"[i == n];
}
}
}
E. Coloring Game
tag:二分图 + 交互
Description:给定一个图,一共\(n\)轮操作,每轮操作:
- \(Alice\)选择三个颜色中选择两个颜色(\(1, 2, 3\))。
- \(Bob\)选择一个未染色的顶点进行染色。
- 如果存在相邻顶点颜色相同\(Alice\)赢,否则\(Bob\)赢。
- 你需要选择玩家,并给出获胜策略。
Solution:显然\(Alice\)想赢每次给出两个相同的颜色最好。模拟一下发现如果是二分图则\(Bob\)必赢,否则\(Alice\)必赢。
- 如果不是二分图,\(Alice\)获胜,每次输出\(1, 2\)即可。
- 如果是二分图\(Bob\)获胜,先用\(1\)或者\(2\)将其中一种颜色染完,剩下的一种用\(3\)进行染色。因为\(1\)的个数加\(2\)的个数等于\(n\),因此必定有一种颜色会被染完。
void solve(){
cin >> n >> m;
vector g(n, vector<int>());
vector st(n, -1);
for (int i = 0; i < m; i ++){
int x, y;
cin >> x >> y;
x --, y --;
g[x].eb(y);
g[y].eb(x);
}
bool flag = true;
vector<int> f1, f2;
auto dfs = [&](auto self, int x, int fa, int c) -> void {
st[x] = c;
if (c == 1){
f1.eb(x);
}
else
f2.eb(x);
for (auto i : g[x]){
if (i == fa)
continue;
if (st[i] == -1){
self(self, i, x, !c);
}
else{
if (st[i] == c){
flag = false;
}
}
}
};
dfs(dfs, 0, -1, 1);
if (flag){
cout << "Bob" << endl;
int s1 = f1.size(), s2 = f2.size();
for (int i = 0; i < n; i ++){
int a, b;
cin >> a >> b;
if (a > b)
swap(a, b);
if (a == 1){
if (s1){
s1 --;
cout << f1.back() + 1 << " " << a << endl;
f1.pop_back();
}
else{
s2 --;
cout << f2.back() + 1 << " " << b << endl;
f2.pop_back();
}
}
if (a == 2){
if (s2){
s2 --;
cout << f2.back() + 1 << " " << a << endl;
f2.pop_back();
}
else{
s1 --;
cout << f1.back() + 1 << " " << b << endl;
f1.pop_back();
}
}
}
}
else{
cout << "Alice" << endl;
for (int i = 1; i <= n; i ++){
int a, b;
cout << "1 2" << endl;
cin >> a >> b;
}
}
}
F. Triangle Formation
tag:大氛围结论 + 小范围暴力
Description:给定\(n\)根木棍,每根木棍的长度为\(a_i\)。有\(q\)次询问,每次询问给定\(l, r\)。判断每次询问是否能选出\(6\)根木棍组成两个三角形。\(l <= i <= r, r - l + 1 >= 6, 1 <= a_i <= 10^9, 1 <= q <= 10^5\)。
Solution:最长的不能组成三角形的序列:斐波拉契序列,因为\(a_1 + a_2 == a3\),那么\(a_1 + a_2 <= a4\)。我们尽可能维护前置条件。
根据\(a_i <= 1e9\),我们得到当\(F_{48}\)时,最大。因此序列长度超过\(48\)时,一定能够组成三角形。我们令区间长度为\(60\),那么当\(r - l >= 60\)时,一定有解。
考虑\(r - l <= 60\),我们先将区间内的数排序。要想三边构成三角形,那么三条边要即可能接近。如给定\(a, b, c\),需要满足\(a + b <= c\),假设\(b\)固定,那么\(a\)要尽可能大,\(c\)要尽快能小。因此三条边越接近越好。如果能从区间内找到两个无交集的三角形区间即符合条件。
考虑连续\(6\)个数组成两个三角形,假设现在有\(7\)个数\(1, 2, 3, 4, 5, 6, 7\)。第一个三角形为\({1, 6, 7}\),那么第二个三角形为\({2, 3, 4}或{3,4,5}\)。如果为\({2, 3, 4}\),那么\({1, 6, 7}\)不如\({5, 6, 7}\)。同理如果为\({3, 4, 5}\),那么\({1,6,7}\)不如\({2, 6, 7}\)。
假设现在有\(a, b, c, d, e, f\)六条边。其中\(f\)一定是长边,\(a, b\)一定是短边。
- 如果\(c\)是长边,则\(a + b > c, d + e > f\),已讨论。
- 如果\(d\)是长边,则\(a + b > d, c + e > f; a + c > d, b + e > f; b + c > d, a + e > f\),三种情况。
- 如果\(e\)是长边,则\(a + b > e, c + d > f; a + c > e, b + d > f;a + d > e, b + c > f;b + c > e, a + d > f\),四种情况。
void solve(){
int q;
cin >> n >> q;
vector<int> a(n + 1);
for (int i = 1; i <= n; i ++)
cin >> a[i];
while (q --){
int l, r;
cin >> l >> r;
if (r - l >= 60){
cout << "YES\n";
}
else{
vector<int> b;
for (int i = l; i <= r; i ++)
b.eb(a[i]);
sort(b.begin(), b.end());
int len = b.size();
int mi = -1, ma = -1;
for (int i = 1; i + 1 < len; i ++){
if (b[i - 1] + b[i] > b[i + 1]){
if (mi == -1){
mi = i;
continue;
}
ma = i;
}
}
if (ma - mi >= 3){
cout << "YES\n";
}
else{
bool ok = false;
for (int i = 0; i + 5 < len; i ++){
vector<int> c(6);
for (int j = 0; j < 6; j ++)
c[j] = b[i + j];
if (c[0] + c[1] > c[3] && c[2] + c[4] > c[5]){
ok = true;
break;
}
else if (c[0] + c[2] > c[3] && c[1] + c[4] > c[5]){
ok = true;
break;
}
else if (c[1] + c[2] > c[3] && c[0] + c[4] > c[5]){
ok = true;
break;
}
else if (c[0] + c[1] > c[4] && c[2] + c[3] > c[5]){
ok = true;
break;
}
else if (c[0] + c[2] > c[4] && c[1] + c[3] > c[5]){
ok = true;
break;
}
else if (c[0] + c[3] > c[4] && c[1] + c[2] > c[5]){
ok = true;
break;
}
else if (c[1] + c[2] > c[4] && c[0] + c[3] > c[5]){
ok = true;
break;
}
}
if (ok)
cout << "YES\n";
else
cout << "NO\n";
}
}
}
}
Pinely Round 4 (Div. 1 + Div. 2)的更多相关文章
- CF Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)
1. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort 暴力枚举,水 1.题意:n*m的数组, ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- Codeforces Beta Round #27 (Codeforces format, Div. 2)
Codeforces Beta Round #27 (Codeforces format, Div. 2) http://codeforces.com/contest/27 A #include< ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 39 (Rated for Div. 2) G
Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...
随机推荐
- 2023年10月,红米(小米)note 8 pro 优化记
看了红米的note 13 pro和note 12 turbo的参数和价格后,我决定下单买个note8 pro的手机壳,确实有新手机的感觉了. 我note8 pro手机参数如下 MIUI 12.0.5 ...
- iconfont图标库的使用
https://www.iconfont.cn/ -- 点击链接进入官网 择自己需要的图标加购物车 点击资源管理->我的项目 选择你需要的项目->下载到本地 将下载的压缩包进行解压,解压后 ...
- js中栈的运用
JavaScript 中栈的运用 在 JavaScript 中,栈(Stack)是一种非常有用的数据结构,它遵循后进先出(Last In First Out,LIFO)的原则.在本文中,我们将深入探讨 ...
- Django之开发restful接口
django中的开发接口有两种模式FBV和CBV,分别是基于函数视图和基于类视图,详细的可以看看菜鸟教程的Django 视图 - FBV 与 CBV,由于本文的用户管理是一个restful风格的api ...
- 【docker-compose】ElasticSearch安装教程
仅供学习参考 ,请勿轻易在生产环境使用 0. 目录树 1. 创建目录 mkdir -p /docker/elasticsearch/conf /docker/elasticsearch/data /d ...
- Java设计模式——适配器模式的精妙应用:探秘 JDK 源码中的 Set 类
在 Java 编程的世界里,JDK 源码犹如一座神秘的宝藏,其中的 Set 类更是我们日常开发中频繁使用的利器.今天,就让我们像勇敢的探险家一样,深入 JDK 源码,揭开 Set 类的神秘面纱,重点剖 ...
- 使用Flex布局的几个小技巧
前情 Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性,任何一个容器都可以指定为 Flex 布局,如果说目前我开发中离不开的布局方式 ...
- powershell禁止系统运行脚本
错误信息: set-executionpolicy remotesigned set-executionpolicy : Windows PowerShell 已成功更新你的执行策略,但在更具体的作业 ...
- WCF Bindings Needed For HTTPS
原文地址:https://weblogs.asp.net/srkirkland/wcf-bindings-needed-for-https 我刚刚完成了我的第一个 WCF 应用,它在我的开发机上顺利工 ...
- grpc与http2的关系
https://nullget.sourceforge.io/?q=node/895 grpc与http2的关系 grpc client 发送包到原生的http2 server client收到报错: ...