https://atcoder.jp/contests/agc031/tasks/agc031_b

B - Reversi


Time Limit: 2 sec / Memory Limit: 1024 MB

Problem Statement

There are N stones arranged in a row. The i-th stone from the left is painted in the color Ci.

Snuke will perform the following operation zero or more times:

  • Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones.

Find the number of possible final sequences of colors of the stones, modulo 1e9+7.

Constraints

  • 1≤N≤2e5 1≤N≤2e5
  • 1≤Ci≤2e5(1≤i≤N)1≤Ci≤2e5(1≤i≤N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
C1
:
CN

Output

Print the number of possible final sequences of colors of the stones, modulo 1e9+7.

题意:有N个有颜色的石头,你可以选择两个颜色相同的石头使他们之间的石头颜色全部变成他们的颜色,重复这个操作任意次(可以为0次),问共有多少种情况

题解:DP.在N-1个石头的方案数dp[N-1]上面考虑第N个石头对结果的贡献,可以得到-->贡献==第N块石头与前面与他颜色相同的石头进行操作的总可能数,可以想到这个可能数==离他最近的与他颜色相同的石头q的总方案数dp[q],因为第N块石头连向更远的石头都可以看成是先连向离他最近的石头之后再向前连接,所以状态方程就是dp[i]=dp[i-1]+dp[q],其中q是离i最近的与他颜色相同的石头的编号.

 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
using namespace std;
typedef long long ll;
const ll mod=1e9+;
int a[],c[],b[];
ll dp[];
int main(){
int n,m;
cin>>n;
for(int i=;i<=n;i++){scanf("%d",&a[i]);}
c[]=a[];
int tot=;
for(int i=;i<=n;i++){
if(a[i]!=c[tot])c[++tot]=a[i];
}
dp[]=;
for(int i=;i<=tot;i++){
if(b[c[i]])dp[i]=(dp[i-]+dp[b[c[i]]])%mod;
else dp[i]=dp[i-];
dp[i]%=mod;
b[c[i]]=i;
}
cout<<dp[tot]<<endl;
return ;
}

AtCoder Grand Contest 031 B - Reversi的更多相关文章

  1. AtCoder Grand Contest 031 B - Reversi(DP)

    B - Reversi 题目链接:https://atcoder.jp/contests/agc031/tasks/agc031_b 题意: 给出n个数,然后现在你可以对一段区间修改成相同的值,前提是 ...

  2. AtCoder Grand Contest 031 简要题解

    AtCoder Grand Contest 031 Atcoder A - Colorful Subsequence description 求\(s\)中本质不同子序列的个数模\(10^9+7\). ...

  3. UPC个人训练赛第十五场(AtCoder Grand Contest 031)

    传送门: [1]:AtCoder [2]:UPC比赛场 [3]:UPC补题场 参考资料 [1]:https://www.cnblogs.com/QLU-ACM/p/11191644.html B.Re ...

  4. AtCoder Grand Contest 031题解

    题面 传送门 题解 比赛的之后做完\(AB\)就开始发呆了--简直菜的一笔啊-- \(A - Colorful\ Subsequence\) 如果第\(i\)个字母选,那么它前面任意一个别的字母的选择 ...

  5. Atcoder Grand Contest 031 D - A Sequence of Permutations(置换+猜结论)

    Atcoder 题面传送门 & 洛谷题面传送门 猜结论神题. 首先考虑探究题目中 \(f\) 函数的性质,\(f(p,q)_{p_i}=q_i\leftarrow f(p,q)\circ p= ...

  6. AtCoder Grand Contest 031 (AGC031) D - A Sequence of Permutations 其他

    原文链接https://www.cnblogs.com/zhouzhendong/p/AGC031D.html 前言 比赛的时候看到这题之后在草稿纸上写下的第一个式子就是 $$f(p,q) = pq^ ...

  7. AtCoder Grand Contest 012

    AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...

  8. AtCoder Grand Contest 011

    AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...

  9. AtCoder Grand Contest 010

    AtCoder Grand Contest 010 A - Addition 翻译 黑板上写了\(n\)个正整数,每次会擦去两个奇偶性相同的数,然后把他们的和写会到黑板上,问最终能否只剩下一个数. 题 ...

随机推荐

  1. POJ 2373 Dividing the Path(DP + 单调队列)

    POJ 2373 Dividing the Path 描述 农夫约翰的牛发现,在他的田里沿着山脊生长的三叶草是特别好的.为了给三叶草浇水,农夫约翰在山脊上安装了喷水器. 为了使安装更容易,每个喷头必须 ...

  2. laravel Eloquent 查询数据库判断获取的内容是否为空

    原文地址:https://www.cnblogs.com/love-snow/articles/7205338.html 在使用 Laravel Eloquent 模型时,我们要判断取出的结果集是否为 ...

  3. Win10系列:JavaScript动画3

    "交叉进出"动画也是Windows动画库中的动画效果."交叉进出"动画的动画效果是在应用程序界面上隐藏一个元素并同时在相同位置显示另一个元素的时候,被隐藏的元素 ...

  4. tomcat访问端口

    问题描述:今天,访问服务器上的应用,tomcat服务器已经启动,确怎么都打不开:问题原因:原来有人改了端口.具体操作:进入tomcat的安装目录,进入conf文件夹下,找到server.xml文件.用 ...

  5. xshell提示必须安装最新的更新

    今天大家的xshell基本都出了这个问题 调整时间,调整到比较前的时间,打开xshell即可. 然后工具->选项 把更新去了

  6. sqlalchem表关联(一对多,一对一,多对多)

    简介: 一:一对多关系 1.表示一对多的关系时,在子表类中通过 foreign key (外键)限制本列的值,然后,在父表类中通过 relationship() 方法来引用子表的类. 2.示例代码: ...

  7. 十六. Python基础(16)--内置函数-2

    十六. Python基础(16)--内置函数-2 1 ● 内置函数format() Convert a value to a "formatted" representation. ...

  8. (C/C++学习笔记) 二十二. 标准模板库

    二十二. 标准模板库 ● STL基本介绍 标准模板库(STL, standard template library): C++提供的大量的函数模板(通用算法)和类模板. ※ 为什么我们一般不需要自己写 ...

  9. 小程序之setData特殊情况 三种情况的wx:if

    比如data{ “a”:{}, "b":{} } 你想完成这样的结构 //创建一个对象 var readyData={} //对象[key] =另一个对象 readyData[ke ...

  10. hibernate查询oracle数据库表报错SQL state [null]; error code [17027]; 流已被关闭; nested exception is java.sql.SQLException: 流已被关闭

    把表字段类型 long 修改为 number类型即可