prompt
string
response
string
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
from collections import defaultdict import sys input = sys.stdin.buffer.readline mod = 10**9+7 N = int(input()) dp = [0] * (N+1) dp[0] = 1 prev = defaultdict(lambda: -1) for i in range(1, N+1): c = int(input()) dp[i] = dp[i-1] if prev[c] >= 0: if prev[c] != i-1: dp[i] = (dp[i] + dp[prev...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
n,*cc = map(int, open(0).read().split()) MOD = 10**9+7 from collections import defaultdict d = {} dp = defaultdict(lambda: 1) for i,c in enumerate(cc): dp[i] = dp[i-1] if c in d: j = d[c] if i - j > 1: dp[i] += dp[j] dp[i] %= MOD d[c] = i print(dp[n-1])
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
n=int(input()) c=list(int(input()) for i in range(n)) dp=[0]*n rec=[0]*(2*10**5+1) dp[0]=1 rec[c[0]]=1 for i in range(1,n): if c[i]==c[i-1]: dp[i]=dp[i-1] continue dp[i]=(dp[i-1]+rec[c[i]])%(10**9+7) rec[c[i]]=dp[i] print(dp[-1])
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
n = int(input()) c = [int(input()) for _ in range(n)] mod = 10 ** 9 + 7 # 解説AC dp = [1] * n color = [-1] * max(c) for i, ci in enumerate(c): dp[i] = dp[i - 1] if 0 <= color[ci - 1] < i - 1: dp[i] += dp[color[ci - 1]] color[ci - 1] = i print(dp[-1] % mod)
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
n = int(input()) c = [] for _ in range(n): c.append(int(input())) mod = 10**9+7 dp = [0] * n dp[0] = 1 pre = [-1] * n tmp = [-1] * (2*10**5+10) for i in range(n): pre[i] = tmp[c[i]-1] tmp[c[i]-1] = i for i in range(1,n): dp[i] = dp[i-1] if pre[i] != -1 and c[i] != c[i-1]: dp[i] += dp[pre...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); long[] dp = new long[N+1]; int[] last = new int[200001]; dp[0] = 1; for(int i = 1; i <= N; i++) { int c = sc.nextInt(); dp[i] = dp[i-1]; if(last[c] != 0 && l...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<bits/stdc++.h> using namespace std; const int _ = 2e5 + 7 , MOD = 1e9 + 7; int dp[_] , sum[_] , arr[_] , pos , N; int main(){ scanf("%d" , &N); int pre = 0; for(int i = 1 , x ; i <= N ; ++i){ scanf("%d" , &x); if(pre != x) arr[++pos] = pre = x; } dp[1] = sum[arr[1]] = 1; for(int i = 2 ; i <= pos ; +...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#!/usr/bin/env python2 """ This file is part of https://github.com/cheran-senthil/PyRival Copyright 2019 Cheran Senthilkumar <hello@cheran.io> """ from __future__ import division, print_function import itertools import os import sys from atexit import register from cStringIO import StringIO from collections import Co...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.net.ConnectException; import java.rmi.dgc.Lease; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Deque; import jav...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
N=int(input()) A=[int(input()) for _ in range(N)] mod=10**9+7 B=[A[0]-1] for i in range(1,N): if A[i]!=A[i-1]: B.append(A[i]-1) n=len(B) used=[-1]*(max(A)+1) used[B[0]]=0 dp=[0]*n dp[0]=1 for i in range(1,n): if used[B[i]]==-1: dp[i]=dp[i-1] else: a=used[B[i]] dp[i]=(dp[i-1]+dp[a])%mod used[B[i]...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define rep(i,l,r) for (int i=(l); i<=(r); i++) typedef long long ll; using namespace std; const int N=200010,mod=1e9+7; int n,lst[N],c[N],p[N],f[N]; int main(){ scanf("%d",&n); rep(i,1,n){ scanf("%d",&c[i]); lst[i]=p[c[i]]; p[c[i]]=i; }...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
from collections import defaultdict def solve(): ans = 0 N = int(input()) C = [int(input()) for _ in range(N)] d = defaultdict(lambda: -1) dp = [1]*N d[C[0]] = 0 mod = 10**9+7 for i in range(1,N): dp[i] = dp[i-1] if d[C[i]]>-1 and d[C[i]]!=i-1: dp[i] += dp[d[C[i]]] dp[i] %= mod d...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF 1LL<<62 #define inf 1000000007 ll c[200010],dp[200010],ch[200010]; int main() { ll n; cin>>n; for(ll i=1;i<=n;i++){ cin>>c[i]; } dp[0]=1; for(ll i=1;i<=n;i++){ if(c[i-1]!=c[i]){ ch[c[i-1]]=i-1; if(ch[c[i]]!=0){ dp[i]+=...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
N = int(input()) C = [int(input()) for _ in range(N)] C = [x for x,y in zip(C[:-1],C[1:]) if x!=y] + [C[-1]] DP = [1] dic = {} for i in range(len(C)): DP.append((DP[-1]+dic.get(C[i],0))%(10**9+7)) dic[C[i]] = DP[-1] print(DP[-1])
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
N = int(input()) pre_stone = 0 div = 10**9+7 dp = 1 dp_dict = {} for _ in range(N): c = int(input()) if pre_stone != c: pre_stone = c if c not in dp_dict: dp_dict[c] = 0 dp += dp_dict[c] dp_dict[c] = dp print(dp%div)
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include <cstdio> #include <iostream> using namespace std; const int mod = 1e9 + 7; int n, c[200010], lst[200010], f[200010]; int main () { scanf ("%d", &n); for (int i = 1; i <= n; ++i) scanf ("%d", &c[i]); f[0] = 1; for (int i = 1; i <= n; ++i) { f[i] = f[i - 1]; if (lst[c[i]] && lst[c[i]] != i - 1) (f[...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<bits/stdc++.h> using namespace std; const int N = 200005, mod = 1e9+7; int n, a[N], dt[N], s[N]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); if(a[i] == a[i-1]) { i--; n--; } } dt[n+1] = 1; s[a[n]] = 1; for(int...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
N = int(input()) C = dict() i, cp, hist, ans = 0, 0, [0]*N, 0 for _ in range(N): c = int(input()) if c != cp: if c in C.keys(): temp = hist[C[c]] + 1 hist[i] = hist[i-1] + temp ans += temp else: hist[i] += hist[i-1] C[c], cp = i, c ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<bits/stdc++.h> #define int long long using namespace std; const int mod=1000000007; int N,A[200100],B[200100]; signed main(){ cin>>N; for(int i=0;i<200000;i++)A[i]=-1; B[0]=1; for(int i=0;i<N;i++){ int a; cin>>a; B[i+1]=(B[i]+(A[a-1]!=i && A[a-1]!=-1?B[A[a-1]]:0))%mod; A[a-1]=i+1; } ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import sys sy=sys.stdin.readline n=int(sy()) if n==1: print(1) quit() dp=[0]*(n+1) lis=[0]*(2*10**5+1) dp[0],dp[1],dp[2]=0,1,1 c1=int(sy()) c2=int(sy()) lis[c1]=1 lis[c2]=2 for i in range(3,n+1): c1=c2 c2=int(sy()) if c1==c2 or lis[c2]==0: dp[i]=dp[i-1] else: dp[i]=dp[i-1]+dp[li...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<bits/stdc++.h> using namespace std ; const int N = 1e9+7 ; const int n = 2e5+7 ; int a[n],s[n] ; int t,x,y ; int main(){ cin >> t ; s[0] = 1 ; for(int i=0;i<t;i++){ cin >> x ; if(x==y) continue ; s[x] = (s[x]+s[y]) % N ; y = x ; } cout << s[x] << endl ; }
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include <bits/stdc++.h> #define N 200002 #define P 1000000007 using namespace std; int n,f[N],lst[N]; int main(){ scanf("%d",&n); f[0]=1; for(int i=1,x;i<=n;++i){ scanf("%d",&x); f[i]=f[i-1]; if(lst[x] && lst[x]+1<i){ f[i]+=f[lst[x]]; if(f[i]>=P)f[i]-=P; ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<bits/stdc++.h> using namespace std ; const int N = 2e5 + 10 , mod = (int)1e9 + 7 ; int n , a[N] , dp[N] , sum[N] ; int32_t main() { cin >> n ; for(int i = 1 ; i <= n ; ++i) cin >> a[i] ; dp[0] = 1 ; for(int i = 1 ; i <= n ; ++i) { dp[i] = dp[i - 1] ; if(a[i] != a[i - 1]) { dp[i] += s...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<bits/stdc++.h> #define fo(i,a,b)for(int i=a,_e=b;i<=_e;++i) #define fd(i,a,b)for(int i=b,_e=a;i>=_e;i--) #define ll long long using namespace std; const int N=2e5+5,mo=1e9+7; int n,x,y; int a[N],la[N]; ll ans,f[N]; int main(){ scanf("%d\n",&n); f[0]=1; fo(i,1,n){ scanf("%d",&a[i]); f[i]=f[i-1]; if(la[...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<cstdio> using namespace std; const int md=1000000007; int a[200001],f[200001],pre[200001]; int main(){ int n; scanf("%d",&n); f[0]=1; for(int i=1;i<=n;i++){ int o; scanf("%d",&o); f[i]=(f[i-1]+(pre[o]==i-1||pre[o]==0?0:f[pre[o]]))%md; // printf("%d %d %d\n",f[i],pre[o],f[i-1]+(pre[o]==i-1||pre[o]=...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<iostream> #define MOD 1000000007 using namespace std; int n,i,Dp[200005],Sum[200005],prv,x; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n; Dp[0]=1; prv=0; for(i=1; i<=n; i++) { cin>>x; if(prv!=x) Sum[x]=(Sum[x]+Dp[i-1])%MOD; Dp[i]...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
from collections import defaultdict MOD = 10 ** 9 + 7 N = int(input()) c = [int(input()) for _ in range(N)] dp = [0 for _ in range(N + 1)] dp[0] = 1 d = defaultdict(lambda: -1) for i, cc in enumerate(c, 1): dp[i] = dp[i - 1] if 0 <= d[cc] < i - 1: dp[i] += dp[d[cc]] dp[i] %= MOD d[cc] = i ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] c = new int[n]; int max = 0; for (int i = 0; i < n; i++) { c[i] = sc.nextInt(); if(max < c[i]) max = c[i...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import sys input = sys.stdin.readline INF = 10 ** 9 MOD = 10 **9 + 7 N = int(input()) C = [int(input()) for _ in range(N)] lst = [INF] * (2 * 10 ** 5 + 10) #各番号がどこにあるかを管理するリスト ans = [1] * N lst[C[-1]] = N - 1 for i in range(N - 2, -1, -1): c = C[i] if lst[c] == INF or lst[c] == i + 1: ans[i] = ans...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<bits/stdc++.h> using namespace std; const int maxn=200005,tt=1e9+7; int n,c[maxn],las[maxn],f[maxn]; int main(){ scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&c[i]); f[0]=1; for (int i=1;i<=n;i++){ f[i]=f[i-1]; if (las[c[i]]!=i-1&&las[c[i]]) f[i]=(f[i]+f[las[c[i]]])%tt; ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include <bits/stdc++.h> using namespace std; typedef long long ll; int last,x,c[200019]; ll ans; int main() { int n; cin >> n; ans=1; last=-1; for(int i=0; i<n; i++){ cin >> x; if(x==last){continue;} last=x; ans = (ans+c[x])%1000000007; c[x]=ans; } cout << ans << endl; return 0...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
n = int(input()) a = [int(input()) for _ in range(n)] MOD = 10**9+7 na = [a[0]] for i in range(n-1): if a[i] != a[i+1]: na.append(a[i+1]) m = len(na) cur = [-1]*(max(na)+1) dp = [0]*(m+1) dp[0] = 1 for i in range(m): if cur[na[i]] != -1: dp[i+1] = (dp[i] + dp[cur[na[i]]]) % MOD ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
# include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn(2e5 + 5); const int mod(1e9 + 7); int n, c[maxn], f[maxn], lst[maxn], pre[maxn]; int main() { int i; scanf("%d", &n); for (i = 1; i <= n; ++i) scanf("%d", &c[i]), lst[i] = pre[c[i]], pre[c[i]] = i; for (f[0] = i = 1; i <= n; ++i)...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=2e5+10; const ll p=1e9+7; ll n, a[N], f[N], last[N]; int main() { scanf( "%lld", &n ); for ( int i=1; i<=n; i++ ) scanf( "%lld", &a[i] ); f[1]=1; last[a[1]]=1; for ( int i=2; i<=n; i++ ) { if ( a[i]==a[i-1] ) { f[i]=f[i-1]; last[a...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include <bits/stdc++.h> #define cinf(n,x) for(int i=0;i<(n);i++) cin >> x[i]; typedef long long int ll; using namespace std; ll mod=1e9+7 ; ll dp[200010]; int chnum[200010]; int main(){ int n;cin>>n; dp[0]=1; for(int i=0;i++<n;){ int C; cin>>C; dp[i]=dp[i-1]+(chnum[C]!=0&&chnum[C]<i-1?dp[chnum[C]]:0); dp[i]%...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
MOD = 10**9+7 n = int(input()) c = [int(input())] for _ in range(n-1): x = int(input()) if x != c[-1]: c.append(x) n = len(c) t = [-1] * (10**5*2+1) t[c[0]] = 0 dp = [1] + [0] * (n-1) for i in range(1, n): dp[i] += dp[i-1] if t[c[i]] != -1: dp[i] += dp[t[c[i]]] dp[i] %= MOD t[c[i]] = i ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=2e5+5; const int mod=1e9+7; int a[N]; int nxt[N],head[N]; int d[N]; int n; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) nxt[i]=head[a[i]],head[a[i]]=i; d[0]=1; for(int i=...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include <algorithm> #include <cstdio> using namespace std; const int M = 1000000007; const int N = 2e5 + 10; int n, dp[N], s[N], c[N]; int main() { scanf("%d", &n); dp[0] = 1; for(int i = 1; i <= n; i ++) { scanf("%d", c + i); if(c[i] == c[i - 1]) { i --; n --; } } for(int i = 1; i <= n; i ++) { dp[i] =...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import java.util.*; public class Main{ public static final int MAX = (int)1e5*2+9; public static final int MOD = (int)1e9+7; public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.next()); int[] c = new int[MAX]; ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
pos=[0]*(200005) ind=0 for i in range(int(input())): x=int(input()) if x!=pos[ind]: ind+=1 pos[ind]=x ans=[0]*(ind+1) per=[0]*200005 ans[0]=1 for i in range(1,ind+1): per[pos[i]]=(per[pos[i]]+ans[i-1])%1000000007 ans[i]=per[pos[i]] print(ans[i])
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
from collections import defaultdict N = int(input()) C = [int(input())] for _ in range(N - 1) : c = int(input()) if c != C[-1] : C.append(c) mod = 10 ** 9 + 7 ret = 1 dp = defaultdict(int) for c in C : tmp = dp[c] dp[c] += ret ret += tmp dp[c] %= mod ret %= mod print(ret)
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include <bits/stdc++.h> using namespace std; int a[222222], d[222222] = { 1 }; int main() { int n, p = 1e9 + 7; cin >> n; for (int i = 0; i < n; i++) { int c; scanf("%d", &c); d[i + 1] = (a[c] && a[c] < i ? d[a[c]] + d[i] : d[i]) % p; a[c] = i + 1; } cout << d[n] << endl; return 0; }
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.System.exit; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Main { static final int MOD =...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int color[] = new int[num]; for(int i = 0; i < num; i ++) { color[i] = sc.nextInt(); } int index[] = new int[2 * (int) Math.pow(10, 5) + 10]; int same[] = new ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include <bits/stdc++.h> using namespace std; #define int long const int mod = 1e9 + 7; signed main() { int n; cin >> n; vector<int> c(n), dp(n, 0); for (int& i : c) cin >> i; map<int, int> m; dp[0] = 1; for (int i = 0; i < n; ++i) { if (i) dp[i] = dp[i - 1]; if (m.count(c[i]...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include <bits/stdc++.h> using namespace std; #define M 200005 int Col[M],dp[M],last[M]; const int P=1e9+7; int main(){ int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&Col[i]); int len=1;Col[len]=Col[1]; for(int i=2;i<=n;i++) if(Col[i]!=Col[i-1]) Col[++len]=Col[i]; dp[0]=1; for(int i=1;i<=len;i++...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
n = int(input()) l = [0 for i in range(2*(10**5)+1)] a = [1] co = 0 for i in range(n): k = int(input()) b = a[-1] if l[k] != i and l[k] != 0: b += a[l[k]] l[k] = i+1 a.append(b%(10**9+7)) print(a[-1])
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import java.util.*; import java.io.*; import static java.lang.Math.*; import static java.util.Arrays.*; import static java.util.Collections.*; public class Main{ static final long mod=1000000007; // static int dx[]={-1,0,1,0}; // static int dy[]={0,-1,0,1}; public static void main(String[] args) th...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
from itertools import groupby from collections import defaultdict mod = 10**9+7 N = int(input()) C = [int(input()) for _ in range(N)] C = [k for k, g in groupby(C)] num = defaultdict(int) dp = [1] + [0] * len(C) for i, c in enumerate(C): dp[i+1] = num[c] = (dp[i] + num[c]) % mod print(dp[-1])
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import java.io.*; import java.util.*; public class Main { public static final long mod = (long)1e9+7; public static final long INF = Long.MAX_VALUE/2; public static final int inf = Integer.MAX_VALUE/2; //agc31 b static void solve(InputReader in, PrintWriter out){ int n = in.ni(); in...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Iterator; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.Writer; import java.io.OutputStreamW...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
n = int(raw_input()) c = [int(raw_input()) for i in range(n)] mod = 1000000007 dp = [0] * len(c) last = [-1] * 200001 dp[0] = 1 last[c[0]] = 0 for i in range(1, len(c)): dp[i] = dp[i - 1] if last[c[i]] >= 0 and last[c[i]] < i - 1: dp[i] = (dp[i] + dp[last[c[i]]]) % mod last[c[i]] = i print dp[-1]
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<bits/stdc++.h> using namespace std; const int mod=1e9+7; const int MAXN=200005; int f[MAXN],g[MAXN]; int a[MAXN],ln,n; void ad(int &x,int y){x+=y;if(x>=mod)x-=mod;} int main() { scanf("%d",&n);int lst=0; for(int i=1;i<=n;i++) { int x;scanf("%d",&x); if(x==lst)continue; if(lst)a[++ln]=lst; lst=x; } ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import java.util.*; public class Main { static Scanner scanner = new Scanner(System.in); static long mod = 1000000007; public static void main(String[]$) { int n = scanner.nextInt(); int[] before = new int[n]; Map<Integer, List<Integer>> map = new HashMap<>(); for (int i =...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 200005; const ll M = ll(1e9) + 7; int n, d[N], c[N]; int main(){ scanf("%d", &n); d[0] = 1; for(int i = 1, x, pv = -1; i <= n; i++){ scanf("%d", &x); d[i] = d[i - 1]; if(x == pv) continue; pv = x; (d[i] += c[x]) %= M; (...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
from collections import defaultdict MOD = 10**9+7 n = int(input()) dp = [1] d = defaultdict(list) cur = int(input()) d[cur].append(0) for i in range(1,n): pre,cur = cur,int(input()) c = dp[-1] if pre != cur and d[cur]: c += dp[d[cur][-1]] dp.append(c) d[cur].append(i) print(dp[-1]%MOD)
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import sys from collections import defaultdict N = int(input()) C = [0] for _ in range(N): c = int(sys.stdin.readline()) if C[-1] == c: continue else: C.append(c) ans = 1 dic = defaultdict(lambda: 0) for c in C: a = dic[c] dic[c] += ans ans += a print(ans % (10 ** 9 + 7))
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import sys fin = sys.stdin.readline MOD = 10**9 + 7 N = int(fin()) C_list = tuple(int(fin()) for _ in range(N)) dp = [0] * N dp[0] = 1 past_idx = {C_list[0]: 0} for i, C in enumerate(C_list[1:], start=1): if C in past_idx: dp[i] = dp[i - 1] + dp[past_idx[C]] if i - past_idx[C] > 1 else dp[i - 1] els...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
from collections import defaultdict def search(colors): dp = [1] * len(colors) c_dict = defaultdict(list) c_sum = defaultdict(int, {colors[0]:1}) for i in range(1, len(colors)): color = colors[i] dp[i] = dp[i-1] if color != colors[i-1]: dp[i] += c_sum[color] ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import java.io.*; import java.math.*; import java.util.*; import java.util.stream.*; public class Main { static final int C = 200_000; void submit() { int n = nextInt(); int[] a = new int[n]; int sz = 0; for (int i = 0; i < n; i++) { int x = nextInt() - 1; if (sz > 0 && x == a[sz - 1]) { cont...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include <bits/stdc++.h> using namespace std; #define N 200005 #define Mod 1000000007 int n, c[N], f[N], s[N]; int main() { cin >> n; for (int i = 1; i <= n; ++i) scanf("%d", c + i); s[0] = 1; for (int i = 1; i <= n; ++i) if (c[i] != c[i - 1]) (s[c[i]] += s[c[i - 1]]) %= Mod; cou...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int a[200010]; int last[200010]; long long dp[200010]; int main(){ int n; scanf("%d", &n); for (int i = 1; i <= n; i++){ scanf("%d", &a[i]); } dp[0] = 1; for (int i = 1; i <= n; i++){ dp[i] = dp[i - 1]; if (last[a[i]] && last[a[i...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
def solve(): n = int(input()) c = [int(input()) for _ in range(n)] mod = 10**9 +7 dp = [1]*n dp[0] = 1 place = [-1]*200001 for i in range(n): if place[c[i]-1] != -1: if place[c[i]-1] == i-1: dp[i] = dp[i-1] else: dp[i] = (dp[i-1]+dp[place[c[i]-1]])%mod else: ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<bits/stdc++.h> #define M 200005 #define LL long long using namespace std; const int mod=1e9+7; int n,A[M],B[M],m=0; LL pre[M],dp[M]; int main(){ cin>>n; for(int i=1;i<=n;i++) scanf("%d",&A[i]); for(int i=1;i<=n;i++) if(A[i]!=A[i-1])B[++m]=A[i]; n=m; dp[0]=1; for(int i=1;i<=n;i++){ dp[i]=(dp[i-1]+pr...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<iostream> using namespace std; int main(){ long long n,i,c[222222],dp[222222],p[222222],mod=1000000007; cin>>n; for(i=1;i<=n;i++) cin>>c[i]; for(i=0;i<222222;i++) p[i]=-1; dp[0]=1; for(i=1;i<=n;i++){ dp[i]=dp[i-1]; if(p[c[i]]!=-1) if(p[c[i]]!=i-1) dp[i]=(dp[i]+dp[p[c[i...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include <bits/stdc++.h> using namespace std; const int mod = int(1e9) + 7; int n, dp[200005][2], sum[200005], arr[200005]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", arr + i); dp[0][0] = 1; for (int i = 1; i <= n; ++i) { dp[i][0] = (dp[i - 1][1] + dp[i - 1][0]) % mod; if (arr[i]...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
from collections import defaultdict MOD = 10**9+7 n = int(input()) dp = [1] d = defaultdict(list) cur = int(input()) d[cur].append(0) for i in range(1,n): pre,cur = cur,int(input()) c = dp[-1] if pre != cur and d[cur]: c += dp[d[cur][-1]] dp.append(c%MOD) d[cur].append(i) print(dp[-1])
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include <bits/stdc++.h> #define For(i, a, b) for (int i = a; i <= b; i++) using namespace std; const int mod = 1000000000 + 7, MAXN = 200000 + 5; int val[MAXN], lastF[MAXN]; int main(){ int n; scanf("%d", &n); val[0] = 1; For(i, 1, n){ int k; scanf("%d", &k); val[i] = val[i - 1]...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
# coding: utf-8 # Your code here! N = int(input()) C = [] prev = -1 for _ in range(N): tmp = int(input()) if tmp != prev: C.append(tmp) prev = tmp dp = [0]*(len(C)+1) place = {} dp[0] = 1 for i in range(len(C)): if place.get(C[i]) != None: dp[i+1] = dp[i] + dp[place.get(C[i])] e...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define SF scanf #define PF printf #define MAXN 200010 #define MOD 1000000007 using namespace std; int dp[MAXN]; int n,a[MAXN]; int las[MAXN]; int main(){ SF("%d",&n); for(int i=1;i<=n;i++) SF("%d",&a[i]); dp[0]=1; for(int i=1;i<=n;i++){ dp...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<iostream> #include<cstdio> #include<string> using namespace std; int pt[1000000], last[1000000]; int main(){ int num; cin >> num; pt[0]=1; for(int i=1,x;i<=num;i++){ cin>>x; pt[i]=pt[i-1]; if(last[x]&&last[x]!=i-1) pt[i]+=pt[last[x]]; last[x]=i; ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
n = int(input()) mod = 10**9+7 s = [0 for _ in range(2*10**5)] pos = [-1 for _ in range(2*10**5)] dp = [0 for _ in range(n + 1)] dp[0] = 1 dp[-1] = 1 for i in range(n): x = int(input()) if i == 0: dp[0] = 1 else: p = pos[x - 1] if p == i - 1: dp[i] = dp[i - 1] elif p > -1: dp[i] = dp[i - 1] + dp[p] ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
# B N = int(input()) C_list = [0]*N for i in range(N): C_list[i] = int(input()) - 1 LARGE = 10**9+7 T = 1 DP = [0]*(max(C_list) + 1) for i in range(N): c = C_list[i] if i > 0: if C_list[i] == C_list[i-1]: pass else: T, DP[c] = (T + DP[c]) % LARGE, (DP[c] + T) %...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
def resolve(): n = int(input()) c = [int(input()) for i in range(n)] lac = [-1]* 200010 dp = [0]*n dp[0] = 1 lac[c[0]] = 0 for i in range(1, n): if c[i] == c[i-1]: dp[i] = dp[i-1] elif lac[c[i]] == -1: dp[i] = dp[i-1] else: dp[i] =...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
N=int(input()) l=[] l_value=[] i=0 stack=-1 for i in range(N): if i < N: l.append(input()) list_simple=[] for i in l: if stack != i: list_simple.append(i) stack = i dp_dict=[0]*200001 dp=1 for i in list_simple: i=int(i) dp = dp_dict[i] +dp dp_dict[i] = dp print(dp%(10**9+7))
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#encoding: utf-8 n = int(input()) c = [] for i in range(n): c.append(int(input())) dp = 1 prev = [0] * (max(c) + 1) x = 0 for i in c: if x != i: dp += prev[i] prev[i] = dp x = i print(dp % (10 ** 9 + 7))
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
d={i:0for i in range(1<<18)};c=0;r=1 for _ in[0]*int(input()):m=int(input());r+=d[m]*(c!=m);d[m]=r;c=m print(r%(10**9+7))
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007LL int main(){ int N; cin >> N; vector<int> C(N); for(auto &i:C)cin >> i; vector<int> prev(222222,-1); vector<long long> dp(N,0); dp[0]=1; for(int i=0;i<N;i++){ int p=prev[C[i]]; prev[C[i]]=i; if(i==0)continue; if(p<0 || p==i-1)dp...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
from collections import defaultdict MOD = 10 ** 9 + 7 N = int(input()) c = [int(input()) for _ in range(N)] dp = [0 for _ in range(N + 1)] dp[0] = 1 d = defaultdict(lambda: -1) for i, cc in enumerate(c, 1): if c[i - 1] == c[i - 2]: dp[i] = dp[i - 1] elif d[cc] == -1: # 初めて出現した色 dp[i...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import java.util.*; public class Main { static final int MOD = 1000000007; public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] dp = new int[n + 1]; dp[0] = 1; HashMap<Integer, TreeSet<Integer>> map = new HashMap<>(); for (int i = 1; i <= n; i++) {...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
N = int(input()) K = [] for i in range(N): K.append(int(input())) L = [K[0]] for j in range(1,N): if K[j] != L[len(L)-1]: L.append(K[j]) dp = [0]*len(L) dp[0] = 1 used = [-1]*(max(L)+1) used[L[0]] = 0 for i in range(1,len(L)): if used[L[i]] == -1: dp[i] = dp[i-1] else: dp[i] = (dp[i-1]+dp[used[L[i]]...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.Input...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<iostream> using namespace std; const int N=2e5+1,D=1e9+7; int n,a[N],f[N],s[N]; int main() { cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; f[0]=1; for(int i=1;i<=n;i++) { f[i]=f[i-1]; if(a[i]!=a[i-1]) (f[i]+=s[a[i]])%=D,(s[a[i]]+=f[i-1])%=D; } cout<<f[n]; return 0; }
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
n = int(input()) c = [0] + [int(input()) for _ in range(n)] dp = [0] * (200001) num = 1 for i in range(n): if c[i]!=c[i+1]: num += dp[c[i+1]] dp[c[i+1]] = num print(num%(10**9+7))
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
/** * Created at 15:18 on 2019-08-26 */ import java.io.*; import java.util.*; public class Main { static FastScanner sc = new FastScanner(); static Output out = new Output(System.out); static final int[] dx = {0, 1, 0, -1}; static final int[] dy = {-1, 0, 1, 0}; static final long MOD = (long) (1e9 + 7)...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<iostream> #include<vector> using namespace std; const int N=2e5+5; const int mod=1e9+7; long long ar[N],dp[N],sum[N]; int main(){ int n,i,j,k; cin>>n; for(i=1;i<=n;i++){ cin>>ar[i]; } dp[0]=1; for(i=1;i<=n;i++){ if(ar[i]!=ar[i-1]){ sum[ar[i]]+=dp[i-1]; } if(sum[ar[i]]>=mod){ sum[ar[i]]-=mod...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import collections n=int(input()) c=[0]+[int(input()) for i in range(n)] d=collections.defaultdict(int) mod=10**9+7 dp=[1 for i in range(n+1)] for i in range(0,n): if c[i]==c[i+1]: dp[i+1]=dp[i] else: t=c[i+1] dp[i+1]=(dp[i]+d[t])%mod d[t]+=dp[i] d[t]%=mod print(dp[n])
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
N = int(input()) C = [int(input()) for _ in range(N)] l = sorted([(c, i) for i,c in enumerate(C)]) path = [[i+1] for i in range(N)] for i in range(len(l)-1): if l[i][0] == l[i+1][0] and l[i+1][1]-l[i][1] > 1: path[l[i][1]].append(l[i+1][1]) dp = [0 for _ in range(N)] dp[0] = 1 for i in range(N-1): f...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import java.util.*; class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long mod = 1000000007; int[] c = new int[n]; //c[i] -> i番目の色 for(int i = 0; i < n; i++){ c[i] = sc.nextInt() - 1; } ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import sys input = sys.stdin.readline n=int(input()) c=[] tmp=0 num=0 for i in range(n): m=int(input()) if m!=tmp: c.append(m) tmp=m num+=1 n=num from collections import Counter dp=Counter() ans=1 mod=10**9+7 for i in range(n): dp[c[i]]=(dp[c[i]]+ans)%mod ans=dp[c[i]] print(ans)
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include <bits/stdc++.h> using namespace std; const int mod = 1e9+7; const int N = 200005; int n, s[N]; int las[N], nex[N]; int ans = 0, dp[N]; int main() { scanf("%d", &n); for(int i = 1; i <= n; ++i) { scanf("%d", s+i); nex[las[s[i]]] = i; las[s[i]] = i; } dp[1] = 1; for(int i = 1; i < n; ++i) { dp[i + 1...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
from collections import defaultdict mod = 10 ** 9 + 7 n = int(input()) C = [int(input()) for i in range(n)] for i in range(n - 1, 0, -1): if C[i] == C[i - 1]: del C[i] #重複部分を圧縮したCを作成 D = defaultdict(int) ans = 1 for c in C: ans += D[c] D[c] = ans #print(D, ans) print(ans % ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include <iostream> #include <map> using namespace std; #define N 200001 #define DD 1000000007 int a[N]; int f = 1; map<int,int> m; int main() { int n,tot = 1; cin >> n; for(int i = 1;i <= n;i ++) cin >> a[i]; for(int i = 2;i <= n;i ++) { if(a[i] != a[i - 1]) a[++tot] = a[i]; } for(int i = 1;i <= tot;i ++...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
#include<bits/stdc++.h> typedef long long ll; using namespace std; const int M=200005,Mod=1e9+7; int n,A[M],B[M],m=0; ll pre[M],dp[M]; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&A[i]); for(int i=1;i<=n;i++) if(A[i]!=A[i-1])B[++m]=A[i]; n=m; dp[0]=1; for(int i=1;i<=n;i++){ dp[i]=(dp[i-1]+p...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import sys input = sys.stdin.readline N = int(input()) A = [int(input()) for _ in range(N)] mod = 10**9+7 dp = [0]*(N+1) last_ind = [-1]*(2*10**5+1) for i, a in enumerate(A): ind = last_ind[a] if ind == -1 or ind == i-1: dp[i+1] = dp[i] else: dp[i+1] = (dp[i] + dp[ind+1] + 1) % mod ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
from collections import defaultdict N=int(input()) C=[int(input()) for _ in range(N)] d = defaultdict(int) ans=1 for i,c in enumerate(C): if i>=1 and C[i-1]==C[i]: continue else: ans+=d[c] d[c]=ans ans%=10**9+7 d[c]%=10**9+7 print(ans)
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
from collections import Counter MOD = pow(10,9)+7 N = int(input()) C = [int(input()) for i in range(N)] DP = [1]*N counter = Counter() for i in range(N): c = C[i] if i==0: counter[c]+=1 elif i==1: if c!=C[i-1]: counter[c]+=1 else: DP[i] = DP[i-1] if c!=C[i-1]...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
N = int(input()) C = [int(input()) for i in range(N)] MOD = 10**9+7 A = [] for c in C: if A and A[-1]==c: continue A.append(c) last = [-1] * 200001 B = [-1] * len(A) for i,a in enumerate(A): B[i] = last[a] last[a] = i dp = [0] * (len(A)) dp[0] = 1 for i in range(1,len(A)): bk = 0 if B[i] < 0 else...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int input[] = new int[n + 2]; final int COLOR_MAX = 2_000_01; input[0] = COLOR_MAX; for(int i = 1; i <= n; i++){ inp...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. 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 possibl...
N=int(input()) C=[int(input()) for i in range(N)] a=[0]*N p={} m=10**9+7 for i,c in enumerate(C): a[i]=a[i-1] if c in p and p[c]<i-1: a[i]=(a[i]+a[p[c]]+1)%m p[c]=i print(a[-1]+1)