AtCoder Grand Contest 031 B - Reversi
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的更多相关文章
- AtCoder Grand Contest 031 B - Reversi(DP)
B - Reversi 题目链接:https://atcoder.jp/contests/agc031/tasks/agc031_b 题意: 给出n个数,然后现在你可以对一段区间修改成相同的值,前提是 ...
- AtCoder Grand Contest 031 简要题解
AtCoder Grand Contest 031 Atcoder A - Colorful Subsequence description 求\(s\)中本质不同子序列的个数模\(10^9+7\). ...
- UPC个人训练赛第十五场(AtCoder Grand Contest 031)
传送门: [1]:AtCoder [2]:UPC比赛场 [3]:UPC补题场 参考资料 [1]:https://www.cnblogs.com/QLU-ACM/p/11191644.html B.Re ...
- AtCoder Grand Contest 031题解
题面 传送门 题解 比赛的之后做完\(AB\)就开始发呆了--简直菜的一笔啊-- \(A - Colorful\ Subsequence\) 如果第\(i\)个字母选,那么它前面任意一个别的字母的选择 ...
- Atcoder Grand Contest 031 D - A Sequence of Permutations(置换+猜结论)
Atcoder 题面传送门 & 洛谷题面传送门 猜结论神题. 首先考虑探究题目中 \(f\) 函数的性质,\(f(p,q)_{p_i}=q_i\leftarrow f(p,q)\circ p= ...
- AtCoder Grand Contest 031 (AGC031) D - A Sequence of Permutations 其他
原文链接https://www.cnblogs.com/zhouzhendong/p/AGC031D.html 前言 比赛的时候看到这题之后在草稿纸上写下的第一个式子就是 $$f(p,q) = pq^ ...
- AtCoder Grand Contest 012
AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...
- AtCoder Grand Contest 011
AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...
- AtCoder Grand Contest 010
AtCoder Grand Contest 010 A - Addition 翻译 黑板上写了\(n\)个正整数,每次会擦去两个奇偶性相同的数,然后把他们的和写会到黑板上,问最终能否只剩下一个数. 题 ...
随机推荐
- Win7系统的虚拟机中安装win7系统
今天因兼职需要,在家里的win7电脑上安装WIN7虚拟机. 之前在xp和win10系统的虚拟机中,安装各种版本的windows系统都很轻松,这一次居然折腾了很久都没搞定. 下载了好几个系统ios镜像都 ...
- 女生可不可以进入IT行业做Linux运维工程师?
不知从何时起有那么一个不成文的理论:女生不适合做IT.在很多人看来,IT is a men’s world,女生学IT是件匪夷所思的事情.在传统的思维当中,女生只适合从事像教师.会计.公务员等稳定的职 ...
- 洛谷 P4515 [COCI2009-2010#6] XOR
题意 平面直角坐标系中有一些等腰直角三角形,且直角边平行于坐标轴,直角顶点在右下方,求奇数次被覆盖的面积.N<=10.输入为x,y,r,分别表示三角形顶点的坐标与三角形的边长. 如: 总面积为0 ...
- bs4 CSS选择器
#https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-all #beautifulSoup可以解析HTML ...
- Android : 网络adb配置及有线端口占用解决方法
一.调试环境: Android Debug Bridge version 1.0.40: Nexus6P平板(Android 8.0系统): 二.网络ADB调试: 1. Android设备除了用有线u ...
- C数据结构 : 线性表 与 链表
一.线性表 一般表现为数组,使用一组地址连续的存储单元依次存储数据元素,如图: 它具有如下特点: 长度固定,必须在分配内存之前确定数组的长度. 存储空间连续,即允许元素的随机访问. 存储密度大,内存中 ...
- 编译varnish 报No package 'libpcre' found
pcre的lib目录未指定 假如安装pcre的目录为/usr/local/pcre 那么lib目录为/usr/local/pcre/lib export PKG_CONFIG_PATH=/usr/lo ...
- 实力封装:Unity打包AssetBundle(四)
→→前情提要:窗口初现←← 让用户选择要打包的文件 时至今日,我们选择打包文件的方式依然是在Project面板或Hierarchy面板中用鼠标点选.现在既然有了窗口,我们自然希望可以将所有文件罗列在窗 ...
- dos命令:系统命令
系统命令 一.mode命令 1.介绍 配置系统设备. 2.语法 串行端口: MODE COMm[:] [BAUD=b] [PARITY=p] [DATA=d] [STOP=s] [to=on|off] ...
- shell脚本实例-shell 分析系统瓶颈脚本
#!/usr/bin/bash PS3="Your choice is: [10 for quit]" #检查是那个系统 os_check() { if [ -e /etc/red ...