Atcoder Grand Contest 031B(DP,思维)

#include<bits/stdc++.h>
using namespace std;
int a[200007];
int b[200007];
long long dp[200007];
long long sum[200007];
const long long mod =1e9+7;
int main(){
    dp[0]=1;
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    int cnt=0;
    for(int i=1;i<=n;i++) {
  if(a[i]!=a[i-1])
            b[++cnt]=a[i];//缩点
 }
 for(int i=1;i<=cnt;i++){
        dp[i]=dp[i-1];//i这个点不染色
        dp[i]=(dp[i]+sum[b[i]])%mod;//sum[b[i]]如果i这点染色多出的情况
        sum[b[i]]=(sum[b[i]]+dp[i-1])%mod;//sum[b[i]]记录了每一个b[i]颜色前面i-1有多少种情况(包括什么都不染)
 }
 printf("%lld",dp[cnt]);
    return 0;
}

原文地址:https://www.cnblogs.com/ldudxy/p/10548431.html

时间: 2024-07-30 22:22:43

Atcoder Grand Contest 031B(DP,思维)的相关文章

AtCoder Grand Contest 024 Problem E(动态规划)

www.cnblogs.com/shaokele/ AtCoder Grand Contest 024 Problem E Time Limit: 2 Sec Memory Limit: 1024 MB Description Find the number of the possible tuples of sequences (\(A_0,A_1,-,A_N\)) that satisfy all of the following conditions, modulo \(M\): ? Fo

【Atcoder Grand Contest 020 E】 Encoding Subsets

Atcoder Grand Contest 020 E 题意:给一个\(0-1\)字符串,如果其中有一段重复,就可以表示成\((\)这一块的表示\(\times\)出现次数\()\). 问这个字符串的所有子集中有多少种表示方法. 思路:考虑\(dp(s)\)表示字符串\(s\)的答案. 那么我们得考虑第一个表示成的位置是什么. ①第一位就是表示的第一位,不参与循环.那么转移到\(dp(s.substr(1))\),并且如果这位是\(1\),那么乘上\(2\),因为这位可能是\(0\). ②一个前

AtCoder Grand Contest 025 Problem D

www.cnblogs.com/shaokele/ AtCoder Grand Contest 025 Problem D Time Limit: 2 Sec Memory Limit: 1024 MB Description Takahashi is doing a research on sets of points in a plane. Takahashi thinks a set \(S\) of points in a coordinate plane is a good set w

AtCoder Grand Contest 011

AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\(n\)个乘客到达了飞机场,现在他们都要坐车离开机场.第\(i\)个乘客到达的时间是\(T_i\),一个乘客必须在\([T_i,T_i+k]\)时刻做到车,否则他会生气.一辆车最多可以坐\(C\)个人.问最少安排几辆车可以让所有人都不生气. 题解 从前往后贪心即可. #include<iostream

AtCoder Grand Contest 014

AtCoder Grand Contest 014 A - Cookie Exchanges 有三个人,分别有\(A,B,C\)块饼干,每次每个人都会把自己的饼干分成相等的两份然后给其他两个人.当其中有一个人的饼干数量是奇数的时候停止,求会进行几次这样子的操作,或者会永远进行下去. 首先无解的情况一定是三个数都是相等的偶数. 否则直接暴力模拟就行了.(盲猜答案不会很大) 证明一下答案的范围:不妨令\(A\le B\le C\),那么最大值和最小值之间的差就是\(C-A\),那么执行完一次操作之后

AtCoder Grand Contest 016

AtCoder Grand Contest 016 A - Shrinking 你可以进行一个串的变换,把一个长度为\(n\)的串\(S\)可以变成长度为\(n-1\)的串\(T\),其中\(T_i\)要么是\(S_i\)要么是\(S_{i+1}\). 现在问你最少进行多少次这个操作,能够使最终得到的\(T\)只由一个字符构成. \(|S|\le 100\) 首先枚举最终字符是哪一个.那么首先在\(S\)末尾加上一个这个字符,那么这个最小步数等于对于所有位置而言,离它最近的枚举的字符到这个位置的

Atcoder Grand Contest 018 E - Sightseeing Plan

Atcoder Grand Contest 018 E - Sightseeing Plan 枚举从第二个矩形的 \((x_1,y_1)\) 进入,\((x_2,y_2)\) 出来,那么中间可以选的点的数量是 \(x_2+y_2-x_1-x_2+1\) ,也就是说对于每一条合法路线,从 \((x_1,y_1)\) 进入的贡献为 \(-x_1-x_2\) ,从 \((x_2,y_2)\) 出来的贡献为 \(x_2+y_2+1\) ,枚举一下第二个矩形边界上的点,我们只需要分别计算某个点到第一个矩形

Atcoder Grand Contest 037B(DP,组合数学,思维)

#include<bits/stdc++.h>using namespace std;const long long mod = 998244353;string s;int a[300007];long long x[7],y[7];int main(){ int n; cin>>n; cin>>s; int len=s.size(); for(int i=0;i<len;++i){ if(s[i]=='B') a[i]=1; else if(s[i]=='G'

AtCoder Grand Contest 031 B - Reversi(DP)

B - Reversi 题目链接:https://atcoder.jp/contests/agc031/tasks/agc031_b 题意: 给出n个数,然后现在你可以对一段区间修改成相同的值,前提是左右端点的值相同.问最后这n个数有多少种不同的值. 题解: 设dp[i]表示只考虑1~i这段,有多少不同的值.然后对于当前第i位,有两种选择,修改或者不修改,不修改的话就是dp[i-1]:修改的话就是dp[k],这里k表示上一个相同颜色的位置. 注意一下如果i-1和i的颜色相同,当前要跳过,这个时候