CF1794C Scoring Subsequences题解
文中 \(a\) 为题目中给的 \(a\)。
如果我们要求 \(a_1, a_2, a_3, \dots, a_m\) 的结果,
那么我们可以把 \(a\) 数组从后往前依次除以 \(i\),\(i\) 从 \(1\) 到 \(n\),
即为 \(\frac{a_1}{m},\frac{a_2}{m - 1},\frac{a_3}{m - 2},\dots,\frac{a_{m - 1}}{2},\frac{a_m}{1}\),并将其保存在数组 \(s\) 中。
因为 \(a_1 \leq a_2 \leq a_3 \leq \dots \leq a_m\),且 \(\frac{1}{i}\) 单调递增,所以 \(s_1 \leq s_2 \leq s_3 \dots \leq s_m\)。
那么我们自然而然地可以想到,每一次的结果就是末尾的几个数字的乘积(因为 \(s\) 越大越好),即 \(s_k \times s_{k + 1} \times \dots \times s_m\)。
那么 \(k\) 取多少呢?
我们只取对自己有利的部分,所以当 \(s_k \geq 1\) 且 \(s_{k - 1} < 1\) 时,我们可以达到最大值 \(ans = s_k \times s_{k + 1} \times \dots \times s_m\)。
因为 \(s\) 单调不下降,所以可以使用二分来得出要保留的数字 \(m - k\)。
对每一个 \(m\) 进行操作,\(1 \leq m \leq n\)。
时间复杂度:\(O(n \log n)\)。
C++代码
/*******************************
| Author: SunnyYuan
| Problem: C. Scoring Subsequences
| Contest: Codeforces Round 856 (Div. 2)
| URL: https://codeforc.es/contest/1794/problem/C
| When: 2023-03-06 08:30:32
|
| Memory: 256 MB
| Time: 2500 ms
*******************************/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 100010;
int n, a[N];
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
int l = -1, r = i + 1;
while (l + 1 < r) {
int mid = (l + r) / 2;
if (a[i - mid + 1] >= mid) l = mid;
else r = mid;
}
cout << l << ' ';
}
cout << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) solve();
return 0;
}
CF1794C Scoring Subsequences题解的更多相关文章
- Codeforces Round #674 (Div. 3) F. Number of Subsequences 题解(dp)
题目链接 题目大意 给你一个长为d只包含字符'a','b','c','?' 的字符串,?可以变成a,b,c字符,假如有x个?字符,那么有\(3^x\)个字符串,求所有字符串种子序列包含多少个abc子序 ...
- CodeForces 689 D Friends and Subsequences
Friends and Subsequences 题解: 如果左端点来说, 那么对于a[i]来说是向上的一条折线, b[i]来说是向下的一条折线, 那么如果这2个折线求交点个数的话, 我们可以二分去求 ...
- 算法与数据结构基础 - 深度优先搜索(DFS)
DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...
- [LeetCode]题解(python):115-Distinct Subsequences
题目来源: https://leetcode.com/problems/distinct-subsequences/ 题意分析: 给定字符串S和T,判断S中可以组成多少个T,T是S的子串. 题目思路: ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- CodeForces 689D Friends and Subsequences (RMQ+二分)
Friends and Subsequences 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/H Description Mi ...
- Codeforces Testing Round #12 C. Subsequences 树状数组维护DP
C. Subsequences Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...
- 115. Distinct Subsequences
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
- HDU 2227 Find the nondecreasing subsequences(DP)
Problem Description How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3 ...
- LeetCode之“动态规划”:Distinct Subsequences
题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A s ...
随机推荐
- Redis(六)集群
Redis集群 1.1 存在的问题 容量不够Redis如何扩容 并发写操作,Redis如何分摊 当主机或者从机宕机,薪火相传.反客为主等主从模式都会导致ip发生变化,应用程序中的配置需要对应修改主机地 ...
- Vue路由实现的底层原理
在Vue中利用数据劫持defineProperty在原型prototype上初始化了一些getter,分别是router代表当前Router的实例 . router代表当前Router的实例.rout ...
- Java学习笔记14
1.Arrays类 Arrays类包含用于操作数组的各种方法(如排序和搜索).该类没有构造函数,直接使用类名.方法名()的方法调用需要的方法. 常用方法 方法 作用 public static S ...
- Python表达式及运算符
表达式 由一个或者几个数字或者变量或者运算符合成的一行代码 通常返回一个结果 运算符 由一个以上的值经过一系列的运算得到新值的过程叫运算 用来操作运算的符号叫运算符 运算符分类 算数运算符 比较或者关 ...
- [ZJOI2020] 序列 线性规划做法/贪心做法
线性规划做法 同时也作为线性规划对偶的一个小小的学习笔记. 以下 \(\cdot\) 表示点积,\(b,c,x,y\) 是行向量. \(A\) 是矩阵,对于向量 \(u,v\) 若 \(\forall ...
- 几种常见的Python数据结构
摘要:本文主要为大家讲解在Python开发中常见的几种数据结构. 本文分享自华为云社区<Python的常见数据结构>,作者: timerring . 数据结构和序列 元组 元组是一个固定长 ...
- 2022-01-06:N个结点之间,表世界存在双向通行的道路,里世界存在双向通行的传送门. 若走表世界的道路,花费一分钟. 若走里世界的传送门,不花费时间,但是接下来一分钟不能走传送门. 输入: T为
2022-01-06:N个结点之间,表世界存在双向通行的道路,里世界存在双向通行的传送门. 若走表世界的道路,花费一分钟. 若走里世界的传送门,不花费时间,但是接下来一分钟不能走传送门. 输入: T为 ...
- Grafana系列-统一展示-9-Jaeger数据源
系列文章 Grafana 系列文章 配置 Jaeger data source Grafana内置了对Jaeger的支持,它提供了开源的端到端分布式跟踪.本文解释了针对Jaeger数据源的配置和查询. ...
- vue全家桶进阶之路17:组件与组件间的通信
在 Vue2 中,组件与组件之间的通信可以通过以下几种方式来实现: Props 和 Events 这是 Vue2 中最基础和常用的父子组件通信方式.父组件通过属性传递数据给子组件,子组件通过事件触发向 ...
- Typo in static class property declarationeslint
eslint 检测提示 Typo in static class property declarationeslint 找了半天原来是propTypes 写成了PropTypes (就是一个首字母大写 ...