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

Codeforces Round 1050 (Div. 4)补题

补题链接:Codeforces Round 1050 (Div. 4)

文章目录

    • A. Sublime Sequence
    • B. Lasers
    • C. Pacer
    • D. Destruction of the Dandelion Fields
    • E. Split
    • F. Gravity Falls
    • G. Farmer John's Last Wish

A. Sublime Sequence

如果 nnn 为奇数,输出 xxx;否则输出 000.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;const int N = 200010, M = N * 2, mod = 1e9 + 7;ll n, x;void solve()
{cin >> x >> n;if (n & 1) cout << x << endl;else cout << 0 << endl;
}int main()
{cin.tie(0);ios::sync_with_stdio(false);ll Case = 1;cin >> Case;while (Case--) solve();return 0;
}

B. Lasers

输出 n+mn + mn+m.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;const int N = 200010, M = N * 2, mod = 1e9 + 7;ll n, m, x, y;
ll a[N], b[N];void solve()
{cin >> n >> m >> x >> y;for (int i = 1; i <= n; i++) cin >> a[i];for (int i = 1; i <= m; i++) cin >> b[i];cout << n + m << endl;
}int main()
{cin.tie(0);ios::sync_with_stdio(false);ll Case = 1;cin >> Case;while (Case--) solve();return 0;
}

C. Pacer

记录前一次的位置 preprepre 和时间点 curcurcur,设 ddda−cura - curacur,对答案的贡献最大为 dddd−1d - 1d1,取值取决于状态的改变和 ddd 的奇偶性。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;const int N = 200010, M = N * 2, mod = 1e9 + 7;ll n, m;void solve()
{cin >> n >> m;ll res = 0, cur = 0, pre = 0, a, b;while (n--){cin >> a >> b;ll d = a - cur;if (b != pre) res += (d & 1) ? d : d - 1;else res += (d & 1) ? d - 1 : d;cur = a;pre = b;}res += m - a;cout << res << endl;
}int main()
{cin.tie(0);ios::sync_with_stdio(false);ll Case = 1;cin >> Case;while (Case--) solve();return 0;
}

D. Destruction of the Dandelion Fields

如果存在奇数,加上所有偶数,并从大到小加上一半的奇数;否则输出 000.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;const int N = 200010, M = N * 2, mod = 1e9 + 7;ll n;void solve()
{cin >> n;vector<ll> even, odd;for (int i = 1; i <= n; i++){ll t;cin >> t;if (t & 1) odd.push_back(t);else even.push_back(t);}if (odd.size() == 0){cout << 0 << endl;return;}ll res = 0;for (auto i : even) res += i;sort(odd.begin(), odd.end());for (int i = odd.size() - 1; i >= (int)(odd.size() / 2); i--)res += odd[i];cout << res << endl;
}int main()
{cin.tie(0);ios::sync_with_stdio(false);ll Case = 1;cin >> Case;while (Case--) solve();return 0;
}

E. Split

使用双指针,从 111nnn 枚举右指针 rrr,同时记录最小的左指针,满足 [l,r][l, r][l,r] 区间内每个数的数量不超过上界,每次在答案上加上 r−l+1r - l + 1rl+1.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;const int N = 200010, M = N * 2, mod = 1e9 + 7;ll n, k;
ll a[N];
ll up[N], cnt[N];void solve()
{cin >> n >> k;for (int i = 1; i <= n; i++) cin >> a[i], up[i] = cnt[i] = 0;bool flag = true;for (int i = 1; i <= n; i++) up[a[i]]++;for (int i = 1; i <= n; i++)if (up[i] % k != 0){flag = false;break;}else up[i] /= k;if (!flag){cout << 0 << endl;return;}ll l = 1, res = 0;for (int i = 1; i <= n; i++){if (cnt[a[i]] + 1 > up[a[i]]){while (l < i && a[l] != a[i]) cnt[a[l]]--, l++;cnt[a[l]]--, l++;}cnt[a[i]]++;res += i - l + 1;}cout << res << endl;
}int main()
{cin.tie(0);ios::sync_with_stdio(false);ll Case = 1;cin >> Case;while (Case--) solve();return 0;
}

F. Gravity Falls

每次从第 kkk 个位置找到字典序最小的数组,然后将该数组的所有数加入答案,更新 kkk 为数组长度 + 1.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;const int N = 200010, M = N * 2, mod = 1e9 + 7;ll n;
ll s[N];void solve()
{cin >> n;vector<vector<ll>> a(n + 1);set<ll> have;for (int i = 1; i <= n; i++) have.insert(i);ll mx = 0;for (int i = 1; i <= n; i++){cin >> s[i];for (int j = 0; j < s[i]; j++){ll t;cin >> t;a[i].push_back(t);}mx = max(mx, s[i]);}vector<ll> res;for (int i = 0; i < mx; i++){auto tmp = have;ll id, mn = LLONG_MAX;for (auto j : tmp){if (s[j] < i + 1) have.erase(j);else if (a[j][i] < mn){mn = a[j][i];id = j;}else if (a[j][i] == mn){bool flag = true, Equal = true;for (int k = i; k < min(s[j], s[id]); k++){if (a[j][k] != a[id][k]) Equal = false;if (a[j][k] > a[id][k]){flag = false;break;}else if (a[j][k] < a[id][k]) break;}if (Equal && s[j] > s[id]) flag = false;if (flag) id = j;}}for (int j = i; j < s[id]; j++) res.push_back(a[id][j]);have.erase(id);i = s[id] - 1;}for (auto i : res) cout << i << ' ';cout << endl;
}int main()
{cin.tie(0);ios::sync_with_stdio(false);ll Case = 1;cin >> Case;while (Case--) solve();return 0;
}

G. Farmer John’s Last Wish

O(nn)O(n \sqrt n)O(nn) 的时间复杂度内求出第 iii 个位置前所有数的所有因子的个数,该位置的答案是数量小于 iii 的因子的数量最大值。使用 setsetset 的时间复杂度会达到 O(nnlogn)O(n \sqrt n log n)O(nnlogn),会超时。

技巧:第 iii 个位置的答案一定大于等于前一个位置的答案,如果答案比前一个位置大,取到答案的因子一定是 a[i−1]a[i - 1]a[i1]a[i]a[i]a[i] 的因子。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
#define x first
#define y secondconst int N = 200010, M = N * 2, mod = 1e9 + 7;int n;
int a[N];
int mp[N];
vector<int> fac[N + 10];void init()
{for (int i = 1; i <= N; i++)for (int j = 1; j * j <= i; j++)if (i % j == 0){if (j != 1) fac[i].push_back(j);if (j * j != i) fac[i].push_back(i / j);}
}void solve()
{cin >> n;for (int i = 1; i <= n; i++) cin >> a[i], mp[i] = 0;vector<int> res;int mx = 0;for (int i = 1; i <= n; i++){for (auto j : fac[a[i]]) mp[j]++;for (auto j : fac[a[i]])if (mp[j] != i)mx = max(mx, mp[j]);for (auto j : fac[a[i - 1]])if (mp[j] != i)mx = max(mx, mp[j]);res.push_back(mx);}for (auto i : res) cout << i << ' ';cout << endl;
}int main()
{cin.tie(0);ios::sync_with_stdio(false);init();int Case = 1;cin >> Case;while (Case--) solve();return 0;
}

文章转载自:

http://n7CHdzHO.qhtLq.cn
http://Tuj0B9vO.qhtLq.cn
http://ESQjsEsh.qhtLq.cn
http://fjvAEljb.qhtLq.cn
http://TbKAbvMT.qhtLq.cn
http://BKkbX5C0.qhtLq.cn
http://DsPF4o7t.qhtLq.cn
http://oYPCLdDY.qhtLq.cn
http://tZddSa1W.qhtLq.cn
http://Bw6KEdKk.qhtLq.cn
http://sdGbiwzN.qhtLq.cn
http://VYIEqjhQ.qhtLq.cn
http://7VMWFhOP.qhtLq.cn
http://SdPEpyDI.qhtLq.cn
http://8aWC7LGx.qhtLq.cn
http://rTJQBzuJ.qhtLq.cn
http://OYMMcJiG.qhtLq.cn
http://Nx7nOMOJ.qhtLq.cn
http://ljthIa7i.qhtLq.cn
http://1zBgmnTB.qhtLq.cn
http://8TKGXpcx.qhtLq.cn
http://tjC5z687.qhtLq.cn
http://qallxWyL.qhtLq.cn
http://xygymaoB.qhtLq.cn
http://Pvg1Dn4W.qhtLq.cn
http://71SFLVeQ.qhtLq.cn
http://Th7Ak1cK.qhtLq.cn
http://BrrbEz74.qhtLq.cn
http://7hSsxLhG.qhtLq.cn
http://ysjybb6e.qhtLq.cn
http://www.dtcms.com/a/383549.html

相关文章:

  • 【Java后端】Spring Boot 多模块项目实战:从零搭建父工程与子模块
  • c++命名空间详解
  • 第15课:知识图谱与语义理解
  • HarmonyOS图形处理:Canvas绘制与动画开发实战
  • ffmpeg 有什么用处?
  • 如何重置Gitlab的root用户密码
  • LeetCode算法日记 - Day 41: 数据流的中位数、图像渲染
  • 计算机网络(二)物理层数据链路层
  • 零基础从头教学Linux(Day 33)
  • collections模块
  • 【前端】【高德地图WebJs】【知识体系搭建】图层知识点——>热力图,瓦片图层,自定义图层
  • 关系模型的数据结构
  • Spring Boot 与前端文件上传跨域问题:Multipart、CORS 与网关配置
  • MySQL的事务特性和高可用架构
  • AI重构车载测试:从人工到智能的跨越
  • 前端梳理体系从常问问题去完善-基础篇(html,css,js,ts)
  • 文件查找 find
  • LeetCode 2110.股票平滑下跌阶段的数目
  • 解锁仓储智能调度、运输路径优化、数据实时追踪,全功能降本提效的智慧物流开源了
  • FPGA学习篇——Verilog学习MUX的实现
  • hadoop单机伪分布环境配置
  • Vue3 响应式失效 debug:Proxy 陷阱导致数据更新异常的深度排查
  • el-table的隔行变色不影响row-class-name的背景色
  • 【深度学习新浪潮】游戏中的agents技术研发进展一览
  • Condor 安装
  • 类和对象 (中)
  • [数据结构——lesson10.2堆的应用以及TopK问题]
  • 可可图片编辑 HarmonyOS(6)水印效果
  • 机器学习(四):支持向量机
  • 给定一个有序的正数数组arr和一个正数range,如果可以自由选择arr中的数字,想累加得 到 1~range 范围上所有的数,返回arr最少还缺几个数。