Codeforces Global Round 23(A~C)
23 global round Codeforces
2023-06-13 09:14:22 时间
A. Maxmina
题目大意:
- 给定长度为 n 只包含 0,1 的序列 a,和一个整数 k,保证 (2\le k\le n\le 50)。
- 不限次数进行如下操作: 将连续且相邻的两个元素变为较小的一个。 将连续的 k 个区间的元素变为区间内元素最大的哪一个。
- 求给出的序列是否可以变为只包含 1 的序列。
思想:
- 签到题。
- 保证 (2\le k\le n\le 50) 即保证了只要序列里含有 1,便可不断执行操作二,即只要存在 1 即可。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int N = 1e6 + 3;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);
void solve(){
int n, k; cin >> n >> k;
bool flag = 0;
for(int i = 0; i < n; i ++){
int x; cin >> x;
if(x == 1) flag = 1;
}
if(flag) cout << "YES" << endl;
else cout << "NO" << endl;
}
int main(){
IOS;
int _ = 1;
cin >> _;
while(_ --){
solve();
}
return 0;
}
B. Rebellion
题目大意:
- 给定长度为 n 只包含 0,1 的序列 a。
- 不限次数进行如下操作:
- 选择两个下标 1 \le i,j\le n,i\ne j。
- 使得 a_j = a_j + a_i;
- 将 a_i 从 a 中去除。
- 若最终可以通过上述操作将 a 变为非严格单调递增的序列,则求出最小操作次数,否则输出 -1。
思想:
- 思维题。
- 将 a 排序,与原位置不相同时只可能为原序列为 1 而排序后为 1 的情况。
- 此时我们只需要执行操作一即可,等价于交换两数的值。
- 由此我们可以利用双指针操作并记录次数,或者直接统计需要交换位置的数量除以 2 即可。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int N = 1e6 + 3;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);
int a[N], b[N];
void solve(){
int n; cin >> n;
for(int i = 0; i < n; i ++){
cin >> a[i];
b[i] = a[i];
}
sort(b, b + n);
int cnt = 0;
for(int i = 0; i < n; i ++){
if(a[i] != b[i]) cnt ++;
}
cout << cnt / 2 << endl;
}
int main(){
IOS;
int _ = 1;
cin >> _;
while(_ --){
solve();
}
return 0;
}
C. Permutation Operations
题目大意:
- 给定一个长度为 n 的排列序列 a。
- 在 i 次操作中,你可以选择任意的 a 的非空后缀,使得所有的后缀元素加 i。
- 求如何操作,使得操作后的序列 a 不含逆序对。
- 输出第 i 次操作的后缀的起始位置。
- 逆序对:对于 i,j(i\gt j) 满足 a_i \lt a_j。
思想:
- 思维题。
- 记录每一对逆序对的差值,那么第 i 次操作需要补足该差值。
- 由于对后缀的操作不会影响到前面,则我们不需要考虑操作的顺序,只需考虑差值何时补齐即可。
- 显然,我们可以对所有的差值从小到大进行排序,第 i 次操作可以操作差值为 t,t\lt i 的位置后缀,若无法操作输出 1。
- 使用优先队列(小根堆)存储所有的差值,操作后出队,队列为空说明已经补齐了差值,此时仅需输出 1 即可。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int N = 1e6 + 3;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);
int a[N];
void solve(){
int n; cin >> n;
for(int i = 1; i <= n; i ++) cin >> a[i];
priority_queue<PII, vector<PII>, greater<PII>> b;
for(int i = 2; i <= n; i ++){
int t = a[i] - a[i - 1];
if(t <= 0){
b.push({-t + 1, i});
}
}
if(b.empty()){
for(int i = 1; i <= n; i ++) cout << 1 << ' ';
}
else{
auto p = b.top();
for(int i = 1 ; i <= n; i ++){
if(i >= p.fi && !b.empty()){
cout << p.se << ' ';
b.pop(); p = b.top();
}
else cout << 1 << ' ';
}
}
cout << endl;
}
int main(){
IOS;
int _ = 1;
cin >> _;
while(_ --){
solve();
}
return 0;
}
相关文章
- C语言教程学习23-25节
- 23种设计模式之享元模式
- 23种设计模式之状态模式和策略模式的区别
- 23个常见Webshell网站管理工具
- 李飞飞新作登PNAS!AI与人类互动23万次,智能水平提高112%
- ICLR'23 UnderReview | LightGCL: 简单而有效的图对比学习推荐系统
- WSDM'23 推荐系统论文梳理
- [软件下载] Adobe Acrobat XI Pro v11.0.23(安装即激活) pdf编辑器全版本下载
- 2022-12-23:portainer是docker的web可视化工具。如果根据docker部署去写yaml,默认local是k8s,而不是docker,这不
- 2-23聚合架构第五十三讲:业务分析部分之业务对象分析
- BricsCAD 23 for Mac(CAD设计2D 3D建模) 23.1.07中文版
- 每日一题之请描述Vue组件渲染流程_2023-02-23
- 2023-02-23:请用go语言调用ffmpeg,解码mp4文件并保存为YUV420P格式文件。
- 【GoF 23】篇4:原型模式
- 【C++修炼之路】23.哈希封装unordered系列
- Surfer 23: 融合科技与自然的极限体验+全版本安装包
- 为河南捐资5000万!鸿星尔克23日销量增长超52倍
- MySQL数据库的23个注意事项