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

Codeforces practice C++ 2024/9/11 - 2024/9/13

D. Mathematical Problem

Codeforces Round 954 (Div. 3)

原题链接:https://codeforces.com/contest/1986/problem/D

题目标签分类:brute force,dp,greedy,implementation,math,two pointers

题目难度:1400

题目大意:

给你一个长度为 n 的字符串,只由 0 到 9 这些字符组成,你需要往这个字符串中插入 n - 2 个 运算符,规定插入的运算符只有 * 和 + 两种。 

输入:

输出:

题目做法:

AC代码 :

#include<bits/stdc++.h>
//#define int long long
//#define int __int128
#define loop(i, n) for(int i = 0; i < n; i++)
#define repeat(i, start, end, step) for(int i = start; i < end; i += step)
#define FastIO ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);using namespace std;
using ll = long long;
using bigint = __int128;const int maxn = 2e5 + 10;
const int modi = 1e9 + 7;int n, inips = 0, news[20];
string s, str[20];
int tonum(string t) {int res = 0;for(int i = 0; i < t.length(); i++) {res *= 10;res += t[i] - '0';}return res;
}
int construct(int x = -1, int y = -1) {inips = 0;for(int i = 0; i < n; i++) str[i] = s[i];for(int i = 0; i < n; i++) {if(i == x || i == y) {str[i + 1] = str[i] + s[i + 1];str[i] = "-";}}for(int i = 0; i < n; i++) if(str[i] != "-") news[inips++] = tonum(str[i]);int dp[20][2];dp[0][0] = dp[0][1] = news[0];for(int i = 1; i < inips; i++) {dp[i][0] = min(dp[i - 1][1] + news[i], dp[i - 1][0] + news[i]);dp[i][1] = min({dp[i][0], dp[i - 1][1] * news[i], dp[i - 1][0] * news[i]});}return dp[inips - 1][1];
}
void solve() {cin >> n >> s;int res = 1e9 + 10;if(n == 2) {cout << construct(0) << '\n';return ;}for(int i = 0; i < n - 1; i++) res = min(res, construct(i));cout << res << '\n';
}signed main() {FastIOint TestCase = 1;cin >> TestCase;while(TestCase--) solve();
}

事后ps:Dp比较好像也很好写,就两种状态,预处理也不是很难。

D. Maximize the Root 

Educational Codeforces Round 168 (Rated for Div. 2)

原题链接:https://codeforces.com/problemset/problem/1997/D

题目标签分类:binary search,dfs and similar,dp,greedy,trees

题目难度:1500

题目大意:

给一颗树,规定编号为1的节点为根节点,每个节点上初始会有一个值,你每次可以进行操作,这个操作是选定一个非叶子节点的节点,给此节点上的值加一,此节点除了该节点外的子树种的节点上的值均减去1。规定每次操作后树上所有节点的值非负。

输入:

共 t 组输入数据。

每组数据有三行,第一行输入一个变量 n,代表这棵树有 n 个节点,第二行有 n 个变量 ai 分别代表初始的时候每个节点上的值,第三行有 n - 1 个变量代表树上每个节点的父节点,n - 1 是因为从下标从2开始,1号节点默认为根节点。

输出:

进行不限次数的合法调整过后根节点上的最大值。

题目做法:

如果根节点上的值确定了,那么这棵树的所有节点上的最小数值也就决定了。

因为结果显然是呈线性变化的,所以可以对答案进行二分,满足则往更大里搜索答案,反之往更小搜索,check 函数的写法应该是 dfs 一整颗树来检测当前二分的答案是否满足。dfs的时间复杂度是 n,二分的复杂度是 log\left ( n \right ),所以总复杂度是 O\left ( nlogn \right ) 。

AC代码 :

#include<bits/stdc++.h>
#define int long long
//#define int __int128
#define pb push_back
#define loop(i, n) for(int i = 0; i < n; i++)
#define rep(i, start, end, step) for(int i = start; i <= end; i += step)
#define FastIO ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);using namespace std;
using ll = long long;
using bigint = __int128;const int maxn = 2e5 + 10;
const int modi = 1e9 + 7;int a[maxn] ,tval;
vector<int> tr[maxn];
bool check(int now, int val) {bool judge = 1;if(val < 0 || val >= 1e18 + 10) return 0;if(tr[now].size() == 0) return (max((ll)0, val - a[now]) <= 0);for(auto it:tr[now]) {judge &= check(it,val * (now != 1) + max((ll)0, val - a[now]));}return judge;
}void solve() {int n, l = 0, r = 1e18 + 10, ans = 0 ,t;cin >> n;loop(i, n) cin >> a[i + 1], tr[i + 1].clear();rep(i, 2, n, 1) cin >> t, tr[t].pb(i);while(l <= r) {int mid = (l + r) / 2;if(check(1, mid)) {ans = max(ans, mid);l = mid + 1;}else r = mid - 1;}cout << ans << '\n';
}signed main() {FastIO int TestCase = 1;cin >> TestCase;while(TestCase--) solve();
}

事后ps:分析整体情况后还是比较容易想到二分答案的,dfs的时候有细节需要注意可能会爆long long,我二分写得挺烂的,还是按板子来比较稳妥。

A. Distanced Coloring

​​​​​​EPIC Institute of Technology Round August 2024 (Div. 1 + Div. 2)

原题链接:https://codeforces.com/problemset/problem/2002/A

题目标签分类:constructive algorithms,implementation,math

题目难度:800

题目大意:

给出 n * m 的网格和一个 k ,满足如下条件的两个点为同一颜色,求填满网格的最小颜色数目。

条件:

题目做法:

在一行上最多就用k个颜色就能填满,如果k小于n的话,k大于n的话就用n个。在同一列上同理。这样最小,还是比较好像的,因为等于k的时候间距最小。

AC代码 :

#include<bits/stdc++.h>
//#define int long long
//#define int __int128
#define pb push_back
#define loop(i, n) for(int i = 0; i < n; i++)
#define rep(i, start, end, step) for(int i = start; i <= end; i += step)
#define FastIO ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);using namespace std;
using ll = long long;
using bigint = __int128;const int maxn = 2e5 + 10;
const int modi = 1e9 + 7;void solve() {int n, m, k;cin >> n >> m >> k;cout << min(n, k) * min(m, k) << '\n';
}signed main() {FastIO int TestCase = 1;cin >> TestCase;while(TestCase--) solve();
}

B. Removals Game

​​​​​​EPIC Institute of Technology Round August 2024 (Div. 1 + Div. 2)

原题链接:https://codeforces.com/problemset/problem/2002/B

题目标签分类:constructive algorithms,games

题目难度:1000

题目大意:

Alice 和 Bob 一开始会分别拿到两个全排列数组,

输入:

输出:

题目做法:

AC代码 :

#include<bits/stdc++.h>
#define int long long
//#define int __int128
#define pb push_back
#define loop(i, n) for(int i = 0; i < n; i++)
#define rep(i, start, end, step) for(int i = start; i <= end; i += step)
#define FastIO ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);using namespace std;
using ll = long long;
using bigint = __int128;const int maxn = 2e5 + 10;
const int modi = 1e9 + 7;void solve() {int n;cin >> n;int a[n],b[n];for(int i = 0; i < n; i++) cin >> a[i];for(int i = 0; i < n; i++) cin >> b[i];bool is = 1, is2 = 1;rep(i, 0, n - 1, 1) is &= (a[i] == b[i]), is2 &= (a[n - 1 - i] == b[i]);if(is || is2) {cout << "Bob" << '\n';return ;}cout << "Alice" << '\n';
}signed main() {FastIO int TestCase = 1;cin >> TestCase;while(TestCase--) solve();
}

事后ps:

A. Legs

Codeforces Round 962 (Div. 3)

原题链接:https://codeforces.com/contest/1996/problem/A

题目标签分类:binary search,math,ternary search

题目难度:800

题目大意:

输入:

输出:

题目做法:

AC代码 :

#include<bits/stdc++.h>
#define int long long
//#define int __int128
#define pb push_back
#define loop(i, n) for(int i = 0; i < n; i++)
#define rep(i, start, end, step) for(int i = start; i <= end; i += step)
#define FastIO ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);using namespace std;
using ll = long long;
using bigint = __int128;const int maxn = 2e5 + 10;
const int modi = 1e9 + 7;void solve() {int n;cin >> n;cout << n / 4 + (n % 4 != 0) << '\n';
}signed main() {FastIO int TestCase = 1;cin >> TestCase;while(TestCase--) solve();
}

D. Paint the Tree

Codeforces Round 947 (Div. 1 + Div. 2)

原题链接:https://codeforces.com/problemset/problem/1975/D

题目标签分类:constructive algorithms,games

题目难度:1700

题目大意:

输入:

输出:

题目做法:

AC代码 :

#include<bits/stdc++.h>
#define int long long
//#define int __int128
#define pb push_back
#define loop(i, n) for(int i = 0; i < n; i++)
#define rep(i, start, end, step) for(int i = start; i <= end; i += step)
#define FastIO ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);using namespace std;
using ll = long long;
using bigint = __int128;const int maxn = 2e5 + 10;
const int modi = 1e9 + 7;void solve() {int n;cin >> n;int a[n],b[n];for(int i = 0; i < n; i++) cin >> a[i];for(int i = 0; i < n; i++) cin >> b[i];bool is = 1, is2 = 1;rep(i, 0, n - 1, 1) is &= (a[i] == b[i]), is2 &= (a[n - 1 - i] == b[i]);if(is || is2) {cout << "Bob" << '\n';return ;}cout << "Alice" << '\n';
}signed main() {FastIO int TestCase = 1;cin >> TestCase;while(TestCase--) solve();
}

事后ps:

相关文章:

Codeforces practice C++ 2024/9/11 - 2024/9/13

D. Mathematical Problem Codeforces Round 954 (Div. 3) 原题链接&#xff1a;https://codeforces.com/contest/1986/problem/D 题目标签分类&#xff1a;brute force&#xff0c;dp&#xff0c;greedy&#xff0c;implementation&#xff0c;math&#xff0c;two pointers…...

RabbitMQ创建交换机和队列——配置类 注解

交换机的类型 Fanout&#xff1a;广播&#xff0c;将消息交给所有绑定到交换机的队列。 Direct&#xff1a;订阅&#xff0c;基于RoutingKey&#xff08;路由key&#xff09;发送给订阅了消息的队列。 Topic&#xff1a;通配符订阅&#xff0c;与Direct类似&#xff0c;只不…...

proteus+51单片机+AD/DA学习5

目录 1.DA转换原理 1.1基本概念 1.1.1DA的简介 1.1.2DA0832芯片 1.1.3PCF8591芯片 1.2代码 1.2.1DAC8053的代码 1.2.2PCF8951的代码 1.3仿真 1.3.1DAC0832的仿真 1.3.2PFC8951的仿真 2.AD转换原理 2.1AD的基本概念 2.1.1AD的简介 2.1.2ADC0809的介绍 2.1.3XPT2…...

【Python机器学习】长短期记忆网络(LSTM)

目录 随时间反向传播 实践 模型的使用 脏数据 “未知”词条的处理 字符级建模&#xff08;英文&#xff09; 生成聊天文章 进一步生成文本 文本生成的问题&#xff1a;内容不受控 其他记忆机制 更深的网络 尽管在序列数据中&#xff0c;循环神经网络为对各种语言关系…...

【Go】使用Goland创建第一个Go项目

✨✨ 欢迎大家来到景天科技苑✨✨ &#x1f388;&#x1f388; 养成好习惯&#xff0c;先赞后看哦~&#x1f388;&#x1f388; &#x1f3c6; 作者简介&#xff1a;景天科技苑 &#x1f3c6;《头衔》&#xff1a;大厂架构师&#xff0c;华为云开发者社区专家博主&#xff0c;…...

STM32学习笔记(一、使用DAP仿真器下载程序)

我们想要使用32单片机&#xff0c;总共包含四个步骤&#xff1a; 1、硬件连接 2、仿真器配置 3、编写程序 4、下载程序 一、第一个问题&#xff08;硬件连接&#xff09;&#xff1a;如何进行硬件连接&#xff0c;才能够启动32板子并能够下载程序呢&#xff1f; 答&#…...

储能运维管理云平台解决方案EMS能量管理系统

在储能行业蓬勃发展的今天&#xff0c;储能运维管理的重要性日益凸显。而储能运维管理云平台的出现&#xff0c;正为储能系统的稳定运行和高效管理注入了新的活力。 一、储能运维管理面临的挑战 传统的储能运维管理方式往往依赖人工巡检和现场操作&#xff0c;存在诸多问题。比…...

网络药理学:16、速通流程版

一、筛选疾病靶点 GeneCards 下载数据得到GeneCards-SearchResult.csv通过Relevance score≥1.0得到GeneCards.csv步骤2只保留Gene Symbol&#xff0c;即基因名这一列得到GeneCards_gene_names.csv OMIM 下载数据得到OMIM-Gene-Map-Retrieval.xlsx只保留Gene/Locus&#xf…...

P2515 [HAOI2010] 软件安装

~~~~~ P2515 [HAOI2010] 软件安装 ~~~~~ 总题单链接 思路 ~~~~~ 发现构成的图是一个森林和一些环。 ~~~~~ 对于森林&#xff0c;建一个虚点然后树形 D P DP DP 即可。 ~~~~~ 对于环&#xff0c;发现要么把这个环上的每一个点都选了&#xff0c;要么每一个都不选。所以可以先缩…...

51单片机快速入门之定时器和计数器

51单片机快速入门之定时器 断开外部输入 晶振振荡 假设为 12MHz 12分频之后,为1MHz 当其从0-65536 时,需要65536μs 微秒 也就是65.536ms 毫秒 溢出(值>65536 时)>中断>执行中断操作 假设需要1ms后产生溢出,则需要设置初始值为64536 此时定时器会从 64536 开始计…...

【计算机网络 - 基础问题】每日 3 题(一)

✍个人博客&#xff1a;Pandaconda-CSDN博客 &#x1f4e3;专栏地址&#xff1a;http://t.csdnimg.cn/fYaBd &#x1f4da;专栏简介&#xff1a;在这个专栏中&#xff0c;我将会分享 C 面试中常见的面试题给大家~ ❤️如果有收获的话&#xff0c;欢迎点赞&#x1f44d;收藏&…...

Unity全面取消Runtime费用 安装游戏不再收版费

Unity宣布他们已经废除了争议性的Runtime费用&#xff0c;该费用于2023年9月引入&#xff0c;定于1月1日开始收取。Runtime费用起初是打算根据使用Unity引擎安装游戏的次数收取版权费。2023年9月晚些时候&#xff0c;该公司部分收回了计划&#xff0c;称Runtime费用只适用于订阅…...

IDEA测试类启动报 “java: 常量字符串过长” 解决办法

目录标题 问题描述问题分析解决办法其他办法 问题描述 问题分析 字符串长度过长&#xff0c;导致 idea 默认使用的 javac 编译器编译不了。 查询资料发现&#xff0c;原因是javac在编译期间&#xff0c;常量字符串最大长度为65534。 解决办法 Javac 编译器改为 Eclipse 编译…...

计算机科学基础 -- 访存单元

访存单元&#xff08;Memory Access Unit&#xff09;的概念 访存单元&#xff08;Memory Access Unit&#xff09; 是处理器中的一个关键模块&#xff0c;负责处理指令中的内存访问操作&#xff0c;包括从内存中读取数据和将数据写入内存。由于内存访问速度通常比处理器执行速…...

Linux压缩、解压缩、查看压缩内容详解使用(tar、gzip、bzip2、xz、jar、war、aar)

在Linux环境中&#xff0c;你可以使用各种命令来压缩、解压缩和查看不同类型的压缩包。以下是常用的命令和操作说明&#xff0c;包括tar、gzip、bzip2、xz、jar、war、aar等类型的包文件。 1. tar命令&#xff1a;压缩、解压、查看tar包 压缩&#xff1a; tar -cvf archive.…...

StreamReader 和 StreamWriter提供自动处理字符编码的功能

FileStream、StreamReader 和 StreamWriter 都用于文件操作&#xff0c;但它们的设计目标和使用方式有所不同。下面是它们之间的主要差异以及如何结合使用的说明&#xff1a; 1. FileStream 用途&#xff1a;提供对文件的字节流访问&#xff0c;用于读写二进制数据。特点&…...

Gitlab备份、迁移、恢复和升级(Gitlab Backup, migration, recovery, and upgrade)

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:Linux运维老纪的首页…...

MySQL:INSERT command denied to user

异常&#xff1a; INSERT command denied to user 解决办法&#xff1a; 请检查一下 MySQL 帐号是否有相应的权限...

【Android安全】Ubuntu 16.04安装GDB和GEF

1. 安装GDB sudo apt install gdb-multiarch 2. 安装GEF(GDB Enhanced Features) 官网地址&#xff1a;https://github.com/hugsy/gef 2.1 安装2021.10版本 但是在Ubuntu 16.04上&#xff0c;bash -c "$(curl -fsSL https://gef.blah.cat/sh)"等命令不好使&…...

ISO 21434与网络安全管理系统(CSMS)的协同作用

ISO/SAE 21434与CSMS&#xff08;网络安全管理系统&#xff09;之间的关系主要体现在以下几个方面&#xff1a; 提供指导框架&#xff1a;ISO/SAE 21434《道路车辆—网络安全工程》是一项国际标准&#xff0c;它为汽车行业提供了实施网络安全管理系统的国际认可的方法和最佳实…...

uniapp 对接腾讯云IM群组成员管理(增删改查)

UniApp 实战&#xff1a;腾讯云IM群组成员管理&#xff08;增删改查&#xff09; 一、前言 在社交类App开发中&#xff0c;群组成员管理是核心功能之一。本文将基于UniApp框架&#xff0c;结合腾讯云IM SDK&#xff0c;详细讲解如何实现群组成员的增删改查全流程。 权限校验…...

设计模式和设计原则回顾

设计模式和设计原则回顾 23种设计模式是设计原则的完美体现,设计原则设计原则是设计模式的理论基石, 设计模式 在经典的设计模式分类中(如《设计模式:可复用面向对象软件的基础》一书中),总共有23种设计模式,分为三大类: 一、创建型模式(5种) 1. 单例模式(Sing…...

C++:std::is_convertible

C++标志库中提供is_convertible,可以测试一种类型是否可以转换为另一只类型: template <class From, class To> struct is_convertible; 使用举例: #include <iostream> #include <string>using namespace std;struct A { }; struct B : A { };int main…...

盘古信息PCB行业解决方案:以全域场景重构,激活智造新未来

一、破局&#xff1a;PCB行业的时代之问 在数字经济蓬勃发展的浪潮中&#xff0c;PCB&#xff08;印制电路板&#xff09;作为 “电子产品之母”&#xff0c;其重要性愈发凸显。随着 5G、人工智能等新兴技术的加速渗透&#xff0c;PCB行业面临着前所未有的挑战与机遇。产品迭代…...

通过Wrangler CLI在worker中创建数据库和表

官方使用文档&#xff1a;Getting started Cloudflare D1 docs 创建数据库 在命令行中执行完成之后&#xff0c;会在本地和远程创建数据库&#xff1a; npx wranglerlatest d1 create prod-d1-tutorial 在cf中就可以看到数据库&#xff1a; 现在&#xff0c;您的Cloudfla…...

CMake基础:构建流程详解

目录 1.CMake构建过程的基本流程 2.CMake构建的具体步骤 2.1.创建构建目录 2.2.使用 CMake 生成构建文件 2.3.编译和构建 2.4.清理构建文件 2.5.重新配置和构建 3.跨平台构建示例 4.工具链与交叉编译 5.CMake构建后的项目结构解析 5.1.CMake构建后的目录结构 5.2.构…...

鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个医院挂号小程序

一、开发准备 ​​环境搭建​​&#xff1a; 安装DevEco Studio 3.0或更高版本配置HarmonyOS SDK申请开发者账号 ​​项目创建​​&#xff1a; File > New > Create Project > Application (选择"Empty Ability") 二、核心功能实现 1. 医院科室展示 /…...

抖音增长新引擎:品融电商,一站式全案代运营领跑者

抖音增长新引擎&#xff1a;品融电商&#xff0c;一站式全案代运营领跑者 在抖音这个日活超7亿的流量汪洋中&#xff0c;品牌如何破浪前行&#xff1f;自建团队成本高、效果难控&#xff1b;碎片化运营又难成合力——这正是许多企业面临的增长困局。品融电商以「抖音全案代运营…...

python如何将word的doc另存为docx

将 DOCX 文件另存为 DOCX 格式&#xff08;Python 实现&#xff09; 在 Python 中&#xff0c;你可以使用 python-docx 库来操作 Word 文档。不过需要注意的是&#xff0c;.doc 是旧的 Word 格式&#xff0c;而 .docx 是新的基于 XML 的格式。python-docx 只能处理 .docx 格式…...

QT: `long long` 类型转换为 `QString` 2025.6.5

在 Qt 中&#xff0c;将 long long 类型转换为 QString 可以通过以下两种常用方法实现&#xff1a; 方法 1&#xff1a;使用 QString::number() 直接调用 QString 的静态方法 number()&#xff0c;将数值转换为字符串&#xff1a; long long value 1234567890123456789LL; …...