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. 逆袭之旅.DAY08东软实训.多态~

    2018年7月4日

  2. add()方法和Put()方法的差别

    add()和put()方法都是集合框架中的添加元素的方法. 但是put()方法应用于map集合中,add()方法应用于collection集合中. 二者的主要区别是:返回值类型不一样. add()放回 ...

  3. outline: none;

    outline: none:用在去掉某个选中后显示的外边框,(追求细节) http://www.w3school.com.cn/cssref/pr_outline.asp

  4. awk使用教程

    gawk - pattern scanning and processing language 基本用法:gawk [options] 'program' FILE ... program:PATTE ...

  5. C++基础知识:构造与析构

    1.构造函数的定义: C++中的类可以定义与类名相同的特殊成员函数这种与类名相同的成员函数叫做构造函数构造函数在定义时可以有参数,但是没有任何返回类型的声明 2.构造函数的调用: 一般情况下C++编译 ...

  6. 深入理解java虚拟机---虚拟机工具jstat(十七)

    jstack---没什么用 jstack用于生成java虚拟机当前时刻的线程快照.线程快照是当前java虚拟机内每一条线程正在执行的方法堆栈的集合,生成线程快照的主要目的是定位线程出现长时间停顿的原因 ...

  7. net core2 采坑-- session 缓存

    引用 Microsoft.Extensions.Caching.SqlServer 可以设置存在数据库 Microsoft.Extensions.Caching.Redis 存在redis 参考 ht ...

  8. js 将文本转换为数据 string number

    <span class="Span" > <p>123.81</p> <a> dejiw</a> </span&g ...

  9. jquery获取URL的参数和锚点

    由于经常会用到替换URL参数值,而网上写的方法代码都太长了,所以在这里写了一个简单的方法,供大家使用. 1)获取url参数 function getUrlParam(name) { var reg = ...

  10. softmax与多分类

    sotfmax 函数在机器学习和深度学习中有着广泛的应用, 主要用于多分类问题. softmax 函数 1. 定义 假定数组V,那么第i个元素的softmax值为 也就是该元素的指数 除以 所有元素的 ...