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

AtCoder ABC402 A~D 题解

A - CBC

题目大意

给点字符串 S S S,输出其中所有大写字母。

思路

根据题意模拟即可。

代码

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;int main()
{string s;cin >> s;for (int i = 0; i < s.size(); i++)if ('A' <= s[i] && s[i] <= 'Z')cout << s[i];return 0;
}

B - Restaurant Queue

题目大意

一些人排队,初始队列为空, Q Q Q 次操作,每一次新增一个人或者输出队列第一个人的数字并移除。

思路

显然可以使用 std::queue,根据题意模拟即可。

代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;queue<int> q;
int Q;int main()
{cin >> Q;while (Q--){int op;cin >> op;if (op == 1){int x;cin >> x;q.push(x);}else{cout << q.front() << endl;q.pop();}}return 0;
}

C - Dislike Foods

题目大意

N N N 种原料和 M M M 种 食物,第 i i i 种食物由 K i K_i Ki 种原料组成,分别是 A i , 1 , A i , 2 , … , A i , K i A_{i, 1},A_{i,2},\dots, A_{i,K_i} Ai,1,Ai,2,,Ai,Ki。刚开始,Snuke 不喜欢任何原料。第 i i i 天,他接受了第 B i B_i Bi 种原料,且只能吃仅由 B 1 , B 2 , … , B i B_1,B_2,\dots, B_i B1,B2,,Bi 组成的食物。问每一天他能吃多少个食物。

思路

发现数据范围很大,而且 i − 1 i-1 i1 天 Snuke 能吃的食物在第 i i i 天的时候也可以吃。这个单调性起到了至关重要的作用,我们不必重复枚举,每一次只需要考虑包含 B i B_i Bi 的食物能否使用即可。可以使用数组维护每一种食物的原料有多少个可以吃,当原料个数与数组值相等的时候,这个食物可以食用。

代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;int n, m, ans;
vector<int> a[300010];
vector<int> v[300010];
int b[300010];
int k[300010];
int c[300010];int main()
{cin >> n >> m;for (int i = 1; i <= m; i++){cin >> k[i];a[i].push_back(0);for (int j = 1; j <= k[i]; j++){int aa; cin >> aa;a[i].push_back(aa);v[aa].push_back(i);}sort(a[i].begin(), a[i].end());}for (int i = 1; i <= n; i++)cin >> b[i];for (int i = 1; i <= n; i++){for (int j = 0; j < v[b[i]].size(); j++){c[v[b[i]][j]]++;if (c[v[b[i]][j]] == k[v[b[i]][j]])ans++;}cout << ans << endl;}return 0;
}

D - Line Crossing

题目大意

N N N 个点均匀分布在圆周上,它们之间连了 M M M 条线,求这 M M M 条线中有多少对是交叉的。

思路

直接求交叉不容易,我们不妨先求出来平行的有多少对,再用总对数减去求出答案。观察题中的图片可以发现,部分平行线 A B AB AB C D CD CD 满足 A + B = C + D A+B=C+D A+B=C+D,而另外一种情况是由循环问题导致的。在 8 8 8 个点的时候,两端分别是 1 1 1 2 2 2 的直线与两端分别是 4 , 7 4,7 4,7 的直线平行。所以,我们将等式完善一下: A + B A+B A+B N N N 取模的结果等于 C + D C+D C+D N N N 取模的结果。

代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;int n, m;
int cnt[2000010];struct node
{int a, b;
} ;node p[300010];int main()
{cin >> n >> m;for (int i = 1; i <= m; i++){cin >> p[i].a >> p[i].b;cnt[(p[i].a + p[i].b) % n]++;}long long ans = 1LL * m * (m - 1);for (int i = 0; i < n; i++)ans -= 1LL * cnt[i] * (cnt[i] - 1);ans /= 2;cout << ans << endl;return 0;
}
http://www.xdnf.cn/news/380.html

相关文章:

  • 数据驱动未来:大数据在智能网联汽车中的深度应用
  • Visio导出清晰图片步骤
  • npm 常用操作和配置
  • uv:重新定义Python开发效率的下一代工具链
  • 高可靠 ZIP 压缩方案兼容 Office、PDF、TXT 和图片的二阶段回退机制
  • 【今日三题】打怪(模拟) / 字符串分类(字符串哈希) / 城市群数量(dfs)
  • Cril 截取字段-生成hostname
  • Git命令归纳
  • 少儿编程路线规划
  • Docker Overlay 网络的核心工作(以跨节点容器通信为例)
  • 公务员行测之速算分数记忆检验-无答案版本
  • 《从理论到实践:CRC校验的魔法之旅》
  • Benewake(北醒) TF-NOVA 在通过TTL-USB转接板更改配置教程
  • VUE快速入门-4:简单入门案例
  • eplan许可证无法识别硬件信息
  • if/switch语句初始化功能
  • MySQL内置函数:字符串函数,数值函数,日期函数,流程控制函数
  • 【unity实战】Unity动画层级(Animation Layer)的Sync同步和Timing定时参数使用介绍,同步动画层制作角色的受伤状态
  • 数据结构基本概念
  • 如何导出pip下载的paho-mqtt包
  • 1.了解开发行业
  • 解析:深度优先搜索、广度优先搜索和回溯搜索
  • OPC Client第3讲(wxwidgets):wxFormBuilder;基础框架;事件处理
  • JavaScript 所有操作数组的方法
  • Spring Bean 全方位指南:从作用域、生命周期到自动配置详解
  • pip 的包下载之后存放在哪?
  • 【AI提示词】退休规划顾问专家
  • SonarQube 集成教程
  • Python读取Excel表格数据并写成JSON格式文件(精简版)
  • 3.指令与权限