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

【CF】⭐Day104——Codeforces Round 840 (Div. 2) CE (思维 + 分类讨论 | 思维 + 图论 + DP)

C. Another Array Problem

题目:

思路:

纯思维题,但是理解错题意

我们题目中说可以执行任意次操作,那么我们如果对同一个地方执行两次操作呢?

显然这一块都将变成 0,那么我们就能执行最优操作了

我们分类讨论一下:

①. n >= 4

此时我们无论最大值 mx 处于哪里,我们都能将所有值变为 mx,如果处于中间,那么就直接将两边全变为 0,然后再变成 mx 即可,如果处于端点,那么也是一样的操作,如果处于次边缘(如第二个),那么就要有点细节了,先把一边全变成 mx,然后再把第一个和第二个变成 0,最后再变成 mx

②. n == 2

此时暴力枚举即可,一个是不改变即 a0 + a1,一个是变成 2 * abs(a0 - a1) 

③. n == 3

此时有些细节,首先肯定是有不改变这个操作,即 a0 + a1 + a2

然后根据 mx 的位置再次讨论,如果 mx 处于两边,那么显然可以变成 3 * a0 或 3 * a2

如果处于中间呢?那么我们肯定是将 a1 与左右两边的试图合并一下,即有 abs(a0 - a1) 或 abs(a1 - a2),那么对于这个值我们同样也可以全变成他,即变成 3 * abs(***) 

最后我们将所有结果取 max 即可

代码:

#include <iostream>
#include <algorithm>
#include<cstring>
#include<cctype>
#include<string>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <memory>
#include <complex>
using namespace std;
#define int long long
#define yes cout << "Yes\n"
#define no cout << "No\n"void solve()
{int n;cin >> n;vector<int> a(n,0);for (int i = 0; i < n; i++){cin >> a[i];}if (n == 2)cout << max({ 2 * abs(a[0] - a[1]), a[0] + a[1] });else if (n == 3)cout << max({ 3 * (abs(a[0] - a[1])), 3 * (abs(a[2] - a[1])), 3 * a[0], 3 * a[2], a[0] + a[1] + a[2] });else{int mx = 0;for (auto i : a)mx = max(i, mx);cout << n * mx;}cout << endl;
}signed main()
{cin.tie(0)->sync_with_stdio(false);int t = 1;cin >> t;while (t--){solve();}return 0;
}

E. Node Pairs

题目:

思路:

感觉遇到相互到达什么的就应该往联通上想

首先题目让我最小化节点数,那么根据题目中的定义,一个大小为 s 的强连通分量的奉献就是 s * (s - 1) / 2,因为其中的点两两互达

所以我们可以做一个完全背包,具体实现在代码中

对于第二问要让我们单项节点对数最大,那么对于所有节点来说任意两点都有一条边是最优的,即有 tot * (tot - 1) 条边,但是由于其中 p 条都是双向的,以此还需要减去 p

具体如何构造呢?我们直接将强连通分量缩点,然后对于每两个点之间就是 size_a * sizeb_b 个单项边了

代码:

#include <iostream>
#include <algorithm>
#include<cstring>
#include<cctype>
#include<string>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <memory>
#include <complex>
using namespace std;
#define int long long
#define yes cout << "Yes\n"
#define no cout << "No\n"void solve()
{int p;cin >> p;//dp[i] 代表 含有 i 对节点时的最少节点数vector<int> dp(p+1, 1e18);dp[0] = 0;//完全背包,枚举 节点对数 ifor (int i = 1; i <= p; ++i){//枚举强联通分量大小,大小为 s 的强联通分量能产生 s * (s - 1) / 2 个节点对,其含有的节点数为 sfor (int s = 1; (s * (s - 1)) / 2 <= i; ++s){dp[i] = min(dp[i], dp[i - (s * (s - 1)) / 2] + s);}}//对于所有节点我们能构造出 cnt * (cnt - 1) / 2 个点对,其中 p 个是双向的,而我们恰好能构造出 tot - p 的图cout << dp[p] << " " << dp[p] * (dp[p] - 1) / 2 - p << endl;
}signed main()
{cin.tie(0)->sync_with_stdio(false);int t = 1;while (t--){solve();}return 0;
}

http://www.lryc.cn/news/592535.html

相关文章:

  • 数据结构入门:像整理收纳一样简单!
  • 文件流导出文件
  • spring boot 实战之分布式锁
  • 【Nginx】nginx+lua+redis实现限流
  • docker,防火墙关闭后,未重启docker,导致端口映射失败
  • 产品需求文档(PRD)格式全解析:从 RP 到 Word 的选择与实践
  • 前端性能优化“核武器”:新一代图片格式(AVIF/WebP)与自动化优化流程实战
  • 新手向:图片批量裁剪工具
  • 力扣 hot100 Day48
  • AWS(基础)
  • (nice!!!)(LeetCode 每日一题) 2163. 删除元素后和的最小差值 (贪心+优先队列)
  • #vscode# #SSH远程# #Ubuntu 16.04# 远程ubuntu旧版Linux
  • 网工知识——vlan技术
  • go安装使用gin 框架
  • 在 Jenkins 中使用 SSH 部署密钥
  • mac系统安装、启动Jenkins,创建pytest接口自动化任务
  • 周志华《机器学习导论》第9章 聚类
  • 一文讲清楚React的render优化,包括shouldComponentUpdate、PureComponent和memo
  • 【Lua】闭包可能会导致的变量问题
  • python-pptx 的layout 布局
  • 人工智能概念之九:深度学习概述
  • JavaSE -- 对象序列化和反序列化详细讲解
  • MySQL的关键日志
  • QML vscode语法高亮和颜色区分。
  • 根据用户id自动切换表查询
  • 7月18日总结
  • UNet改进(23):如何用SLCAM模块提升UNet的分割性能
  • Linux C 进程间通信基本操作
  • 对Yii2中开启`authenticator`后出现的跨域问题-修复
  • 高通8255 Android Virtio Virtio-SPI 配置方法