当前位置:网站首页>统计特殊子序列数目(0,1,2)DP
统计特殊子序列数目(0,1,2)DP
2022-06-13 10:57:00 【重生之我会拧瓶盖】
今天参加Nordic Collegiate Programming Contest 2020,发现这个G题和上周力扣的周赛的第三道题一模一样,我都怀疑农大的参加完周赛直接改的这个题。不过由于英语问题,我读错题了,我第一次知道了must have 是一定是的意思,这个单词如果按照必须有这个意思跟周赛的题一模一样,可惜大意失荆州。
先上力扣周赛题
题目链接
特殊序列 是由 正整数 个 0 ,紧接着 正整数 个 1 ,最后 正整数 个 2 组成的序列。
比方说,[0,1,2] 和 [0,0,1,1,1,2] 是特殊序列。
相反,[2,1,0] ,[1] 和 [0,1,2,0] 就不是特殊序列。
给你一个数组 nums (仅 包含整数 0,1 和 2),请你返回 不同特殊子序列的数目 。由于答案可能很大,请你将它对 109 + 7 取余 后返回。
一个数组的 子序列 是从原数组中删除零个或者若干个元素后,剩下元素不改变顺序得到的序列。如果两个子序列的 下标集合 不同,那么这两个子序列是 不同的 。
示例 1:
输入:nums = [0,1,2,2]
输出:3
解释:特殊子序列为 [0,1,2,2],[0,1,2,2] 和 [0,1,2,2] 。
1 <= nums.length <= 105
0 <= nums[i] <= 2
class Solution {
private:
static constexpr int mod = 1000000007;
public:
int countSpecialSubsequences(vector<int>& nums) {
int f0 = 0, f1 = 0, f2 = 0;
for (int num: nums) {
if (num == 0) {
f0 = (f0 * 2 + 1) % mod;
}
else if (num == 1) {
f1 = (f1 * 2 % mod + f0) % mod;
}
else {
f2 = (f2 * 2 % mod + f1) % mod;
}
}
return f2;
}
};
简单的DP,虽然当时周赛我没做出来,但是我补过了,最近没咋做DP题,基础课还没学到就好了。
这里得把题粘上must have = 一定是,意思是除了第一首歌和最后一首歌全是评级为二的歌。。。。。。。。。。。。。。。。。。。。。。无语
Your friend Tóti is an aspiring musician. He has written nn songs, each of which has a hype rating of either 11, 22, or 33.A higher hype rating means the song has more energy. Tóti is planning his first live performance and needs your help. He wants to know how many setlists he can make. A setlist consist of at least three songs, the first song must have hype rating 11, the last song must have hype rating 33, and all other songs must have hype rating 22. Tóti also wants to play the songs in the same order he wrote them.
Given the hype rating of each of Tóti’s songs in the order he wrote them, how many setlists can he make?
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define L long long
const L mod(1e9+7);
int n;
L f1,f2,f3;
int main(){
scanf("%d",&n);
for(int i=1,x;i<=n;++i){
scanf("%d",&x);
if(x==1){
f1=(f1+1)%mod;
}
if(x==2){
f2=(f2*2%mod+f1)%mod;
}
if(x==3){
f3=(f3+f2)%mod;
}
}
printf("%lld\n",f3);
}
麻了!!!
再来一道变形例题,可倒着分析
题目链接++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
边栏推荐
- Pytorch basis (II) -- tensor and gradient
- 微众银行OSPO建设之路:如何通过OSPO的建设推动企业开源?
- 测试人员必须掌握的测试用例
- [cloud enjoying freshness] community weekly · vol.66- Huawei partners and Developers Conference 2022 wonderful agenda announcement
- The first laravel workflow engine released the official version of v1.0
- Matplotlib learning notes
- 宝塔添加一个网站:PHP项目
- 记几次略有意思的 XSS 漏洞发现
- Brief introduction to memory structure of virtual machine
- d求值两次map
猜你喜欢

Actual combat simulation │ real time error alarm of enterprise wechat robot

Matplotlib 学习笔记

Solution to qt5.12 unable to input Chinese (unable to switch Chinese input method) in deepin system

网传互联网公司加班表,排名第一的没有悬念!

Experience of electric competition - program-controlled wind pendulum

Electrolytic capacitor, tantalum capacitor, ordinary capacitor

Redis相关

Web3和NFT中的匿名性问题

Spark source code (I) how spark submit submits jars and configuration parameters to spark server

数据库学习笔记(第十五章)
随机推荐
winform 解决黑屏 频繁刷新
vivo大规模 Kubernetes 集群自动化运维实践
[cloud enjoying freshness] community weekly · vol.66- Huawei partners and Developers Conference 2022 wonderful agenda announcement
Spark source code (I) how spark submit submits jars and configuration parameters to spark server
D evaluate twice map
宝塔中查看mysql默认密码
COM的模式变化引起的IPdu Handling【接收截止日期监控】
Read pgstat [this time Gauss is not a mathematician]
go-zero微服务实战系列(三、API定义和表结构设计)
Actual combat simulation │ real time error alarm of enterprise wechat robot
flutter简单优秀的开源dialog使用free_dialog
Go zero microservice Practice Series (III. API definition and table structure design)
技术管理进阶——管理者可以使用哪些管理工具
AcWing第 55 场周赛
Ubuntu installs MySQL compressed package for future reference
《自然-通讯》| 用机器学习和时间序列数据为气候变化下的武装冲突风险建模
Navicat connection MySQL in Pagoda
Nature communications - modeling armed conflict risk under climate change using machine learning and time series data
Understand an article: Spark operation mode
Install Kubernetes 1.24