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. docker图形化管理工具portainer

    本章主要介绍docker的web图形化管理工具.这里使用 portainer(类似与dockui不过dockerui只支持单节点) 镜像名称 portainer/portainer 一.启动porta ...

  2. python学习 面向对象高级编程

    ---恢复内容开始--- 面向对象编程---oop,是一种编程思想,oop把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机程序视为一系列的命令集合,即一组函数 ...

  3. [POJ3416]Crossing

    Problem 给你n个点,m个询问,每个询问有x, y 问以(x,y)为原点建立的平面直角坐标系分割的第一象限和第三象限的点数和减去第二象限和第四象限的点数和 Solution 用2个树状数组维护一 ...

  4. 【转】用深度学习做crowd density estimation

    本博文主要是CVPR2016的<Single-Image Crowd Counting via Multi-Column Convolutional Neural Network>这篇文章 ...

  5. 第三篇 功能实现(1) (Android学习笔记)

    第三篇 功能实现(1) 第8章 Android应用程序组成 ●Android的一些中.底层基础知识 ※ Android Framework 启动过程 Android手机系统本质上是一个基于Linux的 ...

  6. Android: apk反编译 及 AS代码混淆防反编译

    一.工具下载: 1.apktool(资源文件获取,如提取出图片文件和布局文件) 反编译apk:apktool d file.apk –o path 回编译apk:apktool b path –o f ...

  7. SharePoint REST API - 确定REST端点URL

    博客地址:http://blog.csdn.net/FoxDave SharePoint REST端点URI的结构 在你能够通过REST访问SharePoint资源之前,首先你要做的就是找出对应的 ...

  8. poj1742(多重背包分解+01背包二进制优化)

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

  9. 四则运算 来源:一位热心的网友 http://www.tqcto.com/article/software/336297.html

    功能:实现真分数的四则运算 语言:Java  平台:JDK下的eclipse github地址:https://github.com/yeershao/hello-world/commit/9920a ...

  10. ios读取plist文件:

    @property (nonatomic,strong) NSArray *imageData;//定义一个数组 //懒加载数据 -(NSArray *)imageDate { if(_imageDa ...