当前位置: 首页 > news >正文

Codeforces Round 731 (Div 3)(A - F)

Codeforces Round 731 (Div. 3)(A - F)

Dashboard - Codeforces Round 731 (Div. 3) - Codeforces

A. Shortest Path with Obstacle(思维)

思路:显然要计算 A → B 之间的曼哈顿距离 , 要绕开 F 当且仅当 AB形成的直线平行于坐标轴且 F 在 AB之间 , 绕开贡献加2即可。

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;int t;int x_1 , y_1 , x_2 , y_2 , x_3 , y_3 , res;signed main(){IOScin >> t;while(t --){res = 0;cin >> x_1 >> y_1;cin >> x_2 >> y_2;cin >> x_3 >> y_3;bool tag = 0;if(x_1 == x_2 && x_2 == x_3 && y_3 > min(y_1 , y_2) && y_3 < max(y_1 , y_2)) tag = 1;if(y_1 == y_2 && y_2 == y_3 && x_3 > min(x_1 , x_2) && x_3 < max(x_1 , x_2)) tag = 1;res += abs(x_1 - x_2) + abs(y_1 - y_2);if(tag) res += 2;cout << res << "\n";}return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);

B. Alphabetical Strings(双指针)

思路:维护头尾两个指针 , 不断往中间找加入的字母 , 找不到跳出 , 判断指针是否相遇即可。

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;int t , l , r , n;
string s;signed main(){IOScin >> t;while(t --){cin >> s;r = s.size();l = 1;n = r;s = '#' + s;while(l <= r){if(s[r] == (char)('a' - 1 + n)) r -= 1 , n -= 1;else if(s[l] == (char)('a' - 1 + n)) l += 1 , n -= 1;else break;}if(l > r) cout << "YES\n";else cout << "NO\n";}return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);

C. Pair Programming(栈 + 贪心)

思路:需要保证在原数列中的相对位置不变 , 不难想出合并后的操作序列就是一个出栈序列 ,维护两个栈 , 贪心的选 , 有 0 选 0 , 没 0 去检查这两个数是否能处理即可。

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;int n , k , x , y , t;
int a[N] , b[N] , ans[N];	signed main(){IOScin >> t;while(t --){cin >> k >> x >> y;stack<int>st1 , st2;for(int i = 1 ; i <= x; i ++) cin >> a[i];for(int i = x ; i >= 1 ; i --) st1.push(a[i]);for(int i = 1 ; i <= y; i ++) cin >> b[i];for(int i = y ; i >= 1 ; i --) st2.push(b[i]);for(int i = 1 ; i <= x + y ; i ++){if(st1.size() && st1.top() == 0){ans[i] = 0;st1.pop();k += 1;}else if(st2.size() && st2.top() == 0){ans[i] = 0;st2.pop();k += 1;}else{if(st1.size() && k >= st1.top()){ans[i] = st1.top();  st1.pop();}else if(st2.size() && k >= st2.top()){ans[i] = st2.top();st2.pop();}else break;}}if(st1.size() || st2.size()) cout << "-1\n";else{for(int i = 1 ; i <= x + y ; i ++) cout << ans[i] << " ";cout << '\n';}}return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);

D. Co-growing Sequence(位运算)

思路:对于 b 数组 , 我们从前往后处理 , 贪心的让每一位最小就能保证字典序最小。那么怎么让每一位最小呢。对于题中所限制的条件(growing)

x a n d y = x x ~and~y = x x and y=x

即需要 x 为 1 的位 y 当前位 必须是 1

对于 b 数组 , 我们可以考虑 b 是通过异或操作为 a 数组补位来达到题目要求的条件。

b 数组的首位显然贪心的放 0 。 对于其后每一位 ,贪心的考虑当前的 b 最小即可 , 即当且仅当前一个数当前位为 1 , 后一个数当前位为 0 时,需要通过异或操作放 1 补位 , 计算贡献即可。

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;int n , t , a[N] , b[N];signed main(){IOScin >> t;while(t --){cin >> n;for(int i = 1 ; i <= n ; i ++) cin >> a[i];b[1] = 0;for(int i = 2 ; i <= n ; i ++){a[i - 1] ^= b[i - 1];b[i] = 0;for(int j = 0 ; j <= 30 ; j ++){int x = (a[i - 1] >> j & 1);int y = (a[i] >> j & 1);if(x && !y) b[i] += (1 << j);}}for(int i = 1 ; i <= n ; i ++) cout << b[i] << " ";cout << "\n";}return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);

E. Air Conditioners(思维)

思路:很好的一道思维题 , 对于每一个位置 , 我们分别考虑其前边的空调和其后边的空调对其的影响。以前边空调举例 , 由于一个点前面的空调对当前点的影响是 温度 + 距离 ,这个总和是不变的, 我们不妨把前面的空调都等价到一号坐标点 ,这样距离都固定了 , 我们只需要维护一个最小温度即可 。 对于后边空调的影响反向处理一遍即可。

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;int t , n , k;
int ans[N] , pos[N] , val[N];signed main(){IOScin >> t;while(t --){cin >> n >> k;for(int i = 1 ; i <= n ; i ++){ans[i] = 9e18;val[i] = 0;}for(int i = 1 ; i <= k ; i ++) cin >> pos[i];for(int i = 1 ; i <= k ; i ++) cin >> val[pos[i]];int minn = 9e18;for(int i = 1 ; i <= n ; i ++){if(val[i]) minn = min(minn , val[i] - (i - 1));ans[i] = min(ans[i] , minn + (i - 1));}minn = 9e18;for(int i = n ; i >= 1 ; i --){if(val[i]) minn = min(minn , val[i] - (n - i));ans[i] = min(ans[i] , minn + (n - i));}for(int i = 1 ; i <= n ; i ++) cout << ans[i] << " ";cout << "\n";}return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);

F. Array Stabilization (GCD version)(st表维护区间gcd)

思路:假如原数组是 a 数组 , 我们模拟一下这个过程

a1a2a3a4
gcd(a1 , a2)gcd(a2 , a3)gcd(a3 , a4)gcd(a4 , a1)
gcd(a1 , a2 , a3)gcd(a2 , a3 , a4)gcd(a3 , a4 , a2)gcd(a3 , a4 , a1)
gcd(a1 , a2 , a3 , a4)gcd(a2 , a3 , a4 , a1)gcd(a3 , a4 , a2 , a1)gcd(a3 , a4 , a1 , a2)

不难发现最多 n - 1 次是一定能让所有的数字相等的 , 而且这个次数满足二分性 , 考虑二分 , 那么问题就转化成了如何处理区间gcd的问题 , 考虑st表处理即可 。 对于环 , 我们把数组变成二倍长度断环为链。复杂度

O ( n l o g n ) O(nlogn) O(nlogn)

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;int t , n;int a[N] , st[N][21];int get(int x , int y){int l = x , r = x + y - 1 , len = y;int now = log2(len);return __gcd(st[l][now] , st[r - (1 << now) + 1][now]);
}bool judge(int x){for(int i = 1 ; i < n ; i ++) if(get(i , x + 1) != get(i + 1 , x + 1)) return 0;return 1;
}signed main(){IOScin >> t;while(t --){cin >> n;for(int i = 1 ; i <= n ; i ++) cin >> a[i];for(int i = n + 1 ; i <= 2 * n ; i ++) a[i] = a[i - n];for(int i = 1 ; i <= 2 * n ; i ++) st[i][0] = a[i];for(int j = 1 ; j <= 20 ; j ++){for(int i = 1 ; i + (1 << j) - 1 <= 2 * n ; i ++){st[i][j] = __gcd(st[i][j - 1] , st[i + (1 << (j - 1))][j - 1]);}}int l = 0 , r = n ;while(l < r){int mid = (l + r) >> 1;if(judge(mid)) r = mid;else l = mid + 1;}cout << l << "\n";}return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
http://www.lryc.cn/news/162087.html

相关文章:

  • Python的sort()与sorted()函数详解
  • 用python实现基本数据结构【04/4】
  • “必抓!”算法
  • 【监控系统】Promethus整合Alertmanager监控告警邮件通知
  • 【韩顺平】Linux基础
  • 好奇一下各个大模型对华为mate60系列的看法
  • UMA 2 - Unity Multipurpose Avatar☀️五.如何使用别人的Recipe和创建自己的服饰Recipe
  • 代码随想录训练营第五十六天| 583. 两个字符串的删除操作 、72. 编辑距离
  • hive解决了什么问题
  • Lumion 和 Enscape 应该选择怎样的笔记本电脑?
  • ICCV 2023 | MoCoDAD:一种基于人体骨架的运动条件扩散模型,实现高效视频异常检测
  • Mac电脑怎么使用NTFS磁盘管理器 NTFS磁盘详细使用教程
  • Java设计模式-结构性设计模式(代理设计模式)
  • 线性空间、子空间、基、基坐标、过渡矩阵
  • 【MySQL】CRUD (增删改查) 基础
  • Socks5代理IP:保障跨境电商的网络安全
  • macOS通过钥匙串访问找回WiFi密码
  • Debian11之稳定版本Jenkins安装
  • kakfa 3.5 kafka服务端处理消费者客户端拉取数据请求源码
  • 【Linux】进程概念I --操作系统概念与冯诺依曼体系结构
  • BRAM/URAM资源介绍
  • 分享一个基于python的个性推荐餐厅系统源码 餐厅管理系统代码
  • Mysql5.7开启SSL认证且支持Springboot客户端验证
  • 微信小程序的页面滚动事件监听
  • 数据可视化:四大发明的现代转化引擎
  • HarmonyOS实现几种常见图片点击效果
  • 3D视觉测量:计算两个平面之间的夹角(附源码)
  • deepin V23通过flathub安装steam畅玩游戏
  • C语言是否快被时代所淘汰?
  • 简化转换器:使用您理解的单词进行最先进的 NLP — 第 1 部分 — 输入