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

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

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() {
    FastIO
    int 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代码 :

1

事后ps:

B. osu!mania

Codeforces Round 971 (Div. 4)

原题链接:https://codeforces.com/problemset/problem/2009/Bicon-default.png?t=O83Ahttps://codeforces.com/problemset/problem/2009/B

题目标签分类:constructive algorithms,games

题目难度:1700

题目大意:

输入:

输出:

题目做法:

AC代码 :

1

事后ps:

E. Coloring Game

Pinely Round 4 (Div. 1 + Div. 2)icon-default.png?t=O83Ahttps://codeforces.com/contest/1991

原题链接:https://codeforces.com/problemset/problem/1991/Eicon-default.png?t=O83Ahttps://codeforces.com/problemset/problem/1991/E

题目标签分类:constructive algorithms,games

题目难度:1700

题目大意:

交互题,给一个 n 个节点,m 条边的无向图,每个节点可以被染成1,2,3,这三种颜色。最开始所有的点没有被染过色。

输入:

t 组测试数据,每组测试数据的第一行输入两个变量  n 和 m 分别代表 n 个顶点 m 条边。紧接着的m 每行两个变量 ui 和 vi 代表 ui 和 vi 间有一条边。

输出:

题目做法:

AC代码 :

1

事后ps:


http://www.kler.cn/news/309458.html

相关文章:

  • 常见数据湖的优劣对比
  • Rust表达一下中秋祝福,群发问候!
  • Spring Boot-依赖冲突问题
  • Verdin AM62 引脚复用配置
  • 检查和测绘室内防撞无人机技术详解
  • 机器学习的网络们
  • mysql 8.0 搭建主从集群注意事项
  • 从登录到免登录:JSP与Servlet结合Cookie的基本实现
  • react 组件通讯
  • 面试题篇: 跨域问题如何处理(Java和Nginx处理方式)
  • Linux 使用 tar 命令
  • C++掉血迷宫
  • 在vmvare安装飞牛私有云 fnOS体验教程
  • 自动化测试框架pytest命令参数
  • 如何在@GenericGenerator中显式指定schema
  • 友思特方案 | 搭建红外桥梁:嵌入式视觉接口助力红外热像仪传输
  • SpringBoot入门(黑马)
  • 【数据可视化】Arcgis api 4.x 专题图制作之分级色彩,采用自然间断法(使用simple-statistics JS数学统计库生成自然间断点)
  • 0072__ActiveX插件的使用
  • Linux云计算 |【第二阶段】SHELL-DAY5
  • pdf文件怎么转换成ppt?介绍几种pdf转ppt的方法
  • 传感技术的应用
  • 利用正则表达式匹配格式并且获取替换内容中数据并保留
  • VS+QT--实现二进制和十进制的转换(含分数部分)
  • 去中心化的力量:探索Web3的分布式网络
  • 工商银行银企直联接口清单
  • Java高级Day40-QQ项目全代码
  • 使用SQL数据构建问答系统的完整指南
  • Nginx泛域名 解析的匹配前缀绑定或转发到子目录
  • APP测试基本流程与APP测试要点总结