当前位置:网站首页>【Acwing】第58场周赛 题解
【Acwing】第58场周赛 题解
2022-07-04 21:38:00 【玄澈_】

AcWing 4488. 寻找1

AC代码
#include <iostream> using namespace std; int main() { int n; cin >> n; bool f = false; while(n -- ) { int num; cin >> num; if(num == 1) f = true; } if(f) puts("YES"); else puts("NO"); return 0; }
AcWing 4489. 最长子序列
输入样例1:
10 1 2 5 6 7 10 21 23 24 49输出样例1:
4输入样例2:
5 2 10 50 110 250输出样例2:
1输入样例3:
6 4 7 12 100 150 199输出样例3:
3
要充分利用好题目中的 严格单调递增 这一条件
对于
,其下一个满足的数应该要满足这两个条件
由单调性可知,
的范围完全覆盖了
,所以选择后面的数可以使选择的范围更大
综上,当后面的数满足小于等于前面数的两倍时,必选这个数(贪心)
AC代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 200010;
int a[N];
int main()
{
int res = 0;
int n; cin >> n;
for(int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
for(int i = 0; i < n; i ++ )
{
int j = i + 1;
while(a[j] <= 2 * a[j - 1] && j < n) j ++ ; j -- ;
res = max(res, j - i + 1);
i = j;
}
cout << res << endl;
return 0;
}AcWing 4490. 染色
输入样例1:
6 1 2 2 1 5 2 1 1 1 1 1输出样例1:
3输入样例2:
7 1 1 2 3 1 4 3 3 1 1 1 2 3输出样例2:
5
由于染色操作具有覆盖性,而且对于一棵(子)树的根节点只有在根节点这一点能够对其进行染色
如果从子节点进行染色的话,最后到达根节点时的染色将会覆盖之前的操作,不满足 最少操作 这一性质
所以我们需要从根节点开始进行染色,然后不断递归处理每个子节点直到叶节点(贪心思路)
- 如果子节点和父节点的颜色相同,则不需要重复染色
- 如果子节点和父节点的颜色不同,则需要多一次染色操作 , ret ++
AC代码
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1000010;
int n;
int e[N], ne[N], h[N], idx;
int w[N];
int ret;
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
void dfs(int u)
{
if(u == 1) ret ++ ;
// 叶节点
if(h[u] == -1) return;
for(int i = h[u]; ~i; i = ne[i])
{
// 是根节点
int j = e[i];
if(h[j] == -1)
{
if(w[j] != w[u]) ret ++ ;
}
else
{
// 不是根节点
if(w[j] != w[u]) ret ++ ;
dfs(j);
}
}
return ;
}
int main()
{
cin >> n;
memset(h, -1, sizeof h);
for(int i = 2; i <= n; i ++ )
{
int p; cin >> p;
add(p, i);
}
for(int i = 1; i <= n; i ++ )
{
cin >> w[i];
}
dfs(1);
cout << ret << endl;
return 0;
}边栏推荐
- 网上开户哪家证券公司佣金最低,我要开户,网上开户安全吗
- gtest从一无所知到熟练运用(1)gtest安装
- How much is the minimum stock account opening commission? Is it safe to open an account online
- 更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
- ArcGIS 10.2.2 | solution to the failure of ArcGIS license server to start
- Use of class methods and class variables
- [public class preview]: basis and practice of video quality evaluation
- PMO:比较25种分子优化方法的样本效率
- 电话加密,中间4为****代替
- Open3D 曲面法向量计算
猜你喜欢
随机推荐
GTEST from ignorance to proficiency (3) what are test suite and test case
如何使用ConcurrentLinkedQueue做一个缓存队列
面试官:说说XSS攻击是什么?
如何借助自动化工具落地DevOps
Bookmark
从RepVgg到MobileOne,含mobileone的代码
Shutter textfield example
输入的查询SQL语句,是如何执行的?
TCP三次握手,四次挥手,你真的了解吗?
How was MP3 born?
面试题 01.08. 零矩阵
KDD2022 | 什么特征进行交互才是有效的?
gtest从一无所知到熟练运用(1)gtest安装
Lambdaquerywrapper usage
i.MX6ULL驱动开发 | 24 - 基于platform平台驱动模型点亮LED
服务线上治理
【C语言进阶篇】数组&&指针&&数组笔试题
服装企业为什么要谈信息化?
gtest从一无所知到熟练使用(3)什么是test suite和test case
GTEST from ignorance to proficiency (4) how to write unit tests with GTEST




![[weekly translation go] how to code in go series articles are online!!](/img/bf/77253c87bfa1512f4b8d3d8f7ebe80.png)








