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...
if __name__ == '__main__': import sys input = sys.stdin.readline n = int(input()) color_dict = {} ans = 1 front_c = 0 for _ in range(n): c = input().rstrip() if c != front_c: try: ans += color_dict[c] color_dict[c] = 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...
N=int(input()) L=[] for i in range(N): n=int(input()) L.append(n) dp=[0 for i in range(N)] dp[0]=1 mod=10**9+7 D=dict() D[L[0]]=1 for i in range(1,N): if L[i]==L[i-1]: dp[i]=dp[i-1] elif L[i] not in D: D[L[i]]=dp[i-1] dp[i]=dp[i-1] else: dp[i]=dp[i-1]+D[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...
N = input() C = [input()-1 for _ in range(N)] P = [-1]*(2*10**5) T = [0]*N M = 10**9+7 T[0] = 1 P[C[0]] = 0 for i in range(1,N): T[i] = T[i-1] p = P[C[i]] if p>=0 and i-p>=2: T[i] += T[p] T[i] %= M P[C[i]] = i print T[-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)] c.append(-1) dic = [-1 for _i in range(10**5 *2 +1)] dp = [0 for _i in range(n+1)] dp[n] = 1 mod = 10**9 +7 for i in range(n): if c[i-1] != c[i]: if dic[c[i]] > 0: dp[i] = (dp[i-1] + dic[c[i]])%mod else: dp[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...
#include<bits/stdc++.h> using namespace std; const int N = 234567, mod = 1e9 + 7; int n, a[N], dp[N], pre[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; dp[0] = 1; for (int i = 1; i <= n; ++i) { cin >> a[i]; dp[i] = dp[i - 1]; if (pre[a[i]] && pre[a[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...
MOD = 10**9+7 N = int(input()) C = [int(input()) for i in range(N)] dp = [0 for i in range(N)] dp[0] = 1 color = {} for c in C: color[c] = 0 color[C[0]] = 1 for i in range(1,N): dp[i] = dp[i-1] if C[i-1] != C[i]: dp[i] = (dp[i] + color[C[i]])%MOD color[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...
#include<cstdio> #define RI register int #define CI const int& using namespace std; const int N=200005,mod=1e9+7; int n,x,pre[N],f[N]; inline void inc(int& x,CI y) { if ((x+=y)>=mod) x-=mod; } int main() { RI i,j; for (scanf("%d",&n),f[0]=i=1;i<=n;pre[x]=i++) if (scanf("%d",&x),f[i]=f[i-1],pre[x]&&pre[x]!=i-1) inc(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...
mod=10**9+7 import sys input=sys.stdin.readline n=int(input()) c=[int(input())for i in range(n)] last=[-1]*max(c) last[c[0]-1]=0 dp=[0]*n dp[0]=1 for i in range(1,n): dp[i]=dp[i-1] if c[i-1]!=c[i] and last[c[i]-1]!=-1: dp[i]+=dp[last[c[i]-1]] dp[i]%=mod last[c[i]-1]=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())-1 for i in range(N)] mod=10**9+7 pre_dp=[0]*(max(C)+1) dp=[0]*(N+1) dp[0]=1 dp[1]=1 pre_dp[C[0]]=1 for i in range(1,N): if C[i]!=C[i-1]: dp[i+1]=(dp[i]+pre_dp[C[i]])%mod pre_dp[C[i]]=(pre_dp[C[i]]+dp[i])%mod else: dp[i+1]=dp[i] 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...
import java.io.*; import java.util.*; public class Main { static void solve() { int n = ni(); int[] c = na(n); long[] dp = new long[n+1]; Map<Integer, TreeSet<Integer>> map = new HashMap<>(); dp[0] = 1; map.put(c[0], new TreeSet<Integer>()); map.get(c[0]).add(0); for(int i=1;i<n;i++) { if(c[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...
#B問題 N = int(input()) C = [int(input()) for i in range(N)] mod = 10**9+7 D = [] for i in range(N-1): if C[i] != C[i+1]: D.append(C[i]) D.append(C[-1]) M = len(D) dp = [0 for i in range(M+1)] dp[0] = 1 count = [0 for i in range(2*10**5+1)] for i in range(1,M+1): dp[i] = dp[i-1] dp[i]+=count[D[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 _ in range(n)] m = 10**9+7 dp = [-1] * n dp[0] = 1 last_dic = {} for i in range(n): if c[i] in last_dic.keys() and i - last_dic[c[i]] >= 2: dp[i] = (dp[i-1] + dp[last_dic[c[i]]]) % m elif i: dp[i] = dp[i-1] last_dic[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...
import collections MOD = int(1e9 + 7) n = int(input()) dic = {} f = [0] * (n + 1) f[0] = 1 for i in range(n): x = int(input()) if x not in dic: f[i + 1] = f[i] elif dic[x] == i - 1: f[i + 1] = f[i] else: f[i + 1] = (f[i] + f[dic[x] + 1]) % MOD dic[x] = i print(f[-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...
#include <bits/stdc++.h> using namespace std; using ll=long long; const ll MOD=1000000007; int main(){ int n; cin>>n; vector<ll> dp(n,0),num(200001,0); int log; cin>>log; dp[0]=1; for(int i=1;i<n;i++){ int x; cin>>x; if(x!=log){ dp[i]=dp[i-1]+num[log]; dp[i]%=MOD; num[log]=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...
#include <bits/stdc++.h> constexpr long long mod{1000000007}; int main() { int N; scanf("%d", &N); std::vector<int> C(N); for (auto& e: C) { scanf("%d", &e); e--; } std::vector<long long> dp(N + 1); std::vector<int> pre(200000, -1); dp[0] = 1; for (int i{}; i < N; i++) { if (pre[C[i]] >= 0 && pre[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> typedef long long ll; using namespace std; const ll mod=1e9+7,N=2e5+5; ll c[N],dp[N],sum[N]; int main(){ cin.tie(NULL); cout.tie(NULL); ios_base:: sync_with_stdio(false); ll n;cin>>n; for(ll i=1;i<=n;i++)cin>>c[i]; sum[c[1]]=dp[1]=1; for(ll i=2;i<=n;i++){ if(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 java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual soluti...
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()) Ca = [int(input())-1 for i in range(N)] C = [] p = -1 for c in Ca: if p != c: C.append(c) p = c #print(C) N = len(C) if N<=2: print(1) exit() L = [-1]*(2*10**5) dp = [0]*N dp[0] = 1 L[C[0]] = 0 for i, c in enumerate(C[1:], 1): if L[c] == -1: L[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 java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { BufferedReader in = new BufferedReader(new InputStreamReader(System.in), 1000000); StringTokenizer tok = new StringTokenizer(""); String next() throws IOExcep...
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 = [] mod = 10**9+7 for i in range(n): c = int(input()) if i==0: C.append(c) elif C[-1]!=c: C.append(c) n = len(C) dp = [0]*n idx = {} for i in range(n): if i==0: idx[C[i]] = i dp[i] = 1 else: if C[i] not in idx: dp[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...
#include <bits/stdc++.h> using namespace std; typedef long long ll; int ans,last,x,n,c[200019]; int main() { cin >> n; ans=1; last=-1; for(int i=0; i<n; i++){c[i]=0;} for(int i=0; i<n; i++){ cin >> x; if(x==last){continue;} last=x; ans = (1LL*ans+c[x])%1000000007; c[x]=ans; } 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 N=200010,mo=1000000007; int A[N],Sum[N],dp[N]; int main(){ int n,i,h=0; scanf("%d",&n); for (i=1;i<=n;i++){ scanf("%d",&A[i]); if (A[i]!=A[h]){ A[++h]=A[i]; } } dp[0]=1; for (i=1;i<=h;i++){ dp[i]=(dp[i-1]+Sum[A[i]])%mo; Sum[A[i]]=(Sum[A[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...
n = int(input()) c = [int(input()) for _ in range(n)] mod = 10 ** 9 + 7 MAX = 2 * 10 ** 5 cc = [] prev = 0 for e in c: if e != prev: cc.append(e) prev = e ln = len(cc) dp_prev = 1 acc = [0] * (MAX + 1) for i, e in enumerate(cc, 1): dp_now = dp_prev + acc[e] dp_now %= mod acc[e] += 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...
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Scanner; public class Main { public static class Interval implements Comparable<Interval> { public int start; public int end;...
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 mod = 1_000_000_007; int N = sc.nextInt(); int[] index = new int[200000]; Arrays.fill(index, -1); int before = -1; long[] dp = new long[...
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.util.Map.Entry; class Main { static int mod = (int) (Math.pow(10,9)+7); // static int mod = (int) 998244353; static List<ArrayList<Integer>> list= new ArrayList<ArrayList<Integer>>(); static int a_dist[]; public static void main(String[] args) { ...
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()) ans_list = [ 1 for i in range(n) ] color_list = [ -1 for i in range(2*(10**5))] for i in range(n): color = int(input()) - 1 if color_list[color] not in [-1,i-1]: ans_list[i] = ans_list[i-1] + ans_list[color_list[color]] else: ans_list[i] = ans_list[i-1] ans_list[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.PrintWriter; import java.util.Arrays; import java.io.IOException; import java.io.BufferedReader; import java.io.Reader; import java.io.InputStreamReader; import java.util.TreeSet; import java.io.InputStream; /** * Buil...
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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.Deque; import java.util.HashMap; import java.util.Map; public class Main { public void solve(BufferedReader stdin, PrintWriter stdout) throws Number...
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 N = 2e5+4; const int mod = 1e9+7; int a[N]; int dp[N]; int roll[N]; int n; signed main() { cin>>n; for(int i=1;i<=n;i++) { scanf("%lld",&a[i]); } dp[0] = 1; roll[a[1]] = 1; for(int i=1;i<=n;i++) { dp[i] = roll[a[i]]; if(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> using namespace std; const int M = 2e5 + 5; const int P = 1e9 + 7; int a[M], w[M], dp[M]; inline int Mod(int x) { return x >= P ? x - P : x; } int main() { int n; cin >> n; for (int i = 1; i <= n; ++i) scanf("%d", a + i); dp[n] = w[a[n]] = 1; for (int i = n - 1; i > 0; --i) { if ...
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 MOD (1000000007) using namespace std; const int N=2e5+10; int f[N], g[N]; int num[N], p; int main() { ios::sync_with_stdio(false), cin.tie(0); int n; cin>>n; int v; for(int i=0; i<n; i++) { cin>>v; if(num[p]!=v) num[++p]=v; } f[0]=1; for(...
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 dp = [0] * (n + 1) dp[0] = 1 prev = [-1] * (10 ** 6) for i, x in enumerate(c): dp[i + 1] = dp[i] if 0 <= prev[x] < i: dp[i + 1] += dp[prev[x]] dp[i + 1] %= MOD prev[x] = i + 1 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...
import sys input = sys.stdin.readline mod = 10**9 +7 N = int(input()) C = [input() for _ in range(N)] dic ={} lis = [1] before =C.pop(0) dic[before]=[0] for i,s in enumerate(C): lis.append(lis[-1]) if s in dic.keys(): if s != before: lis[-1] += lis[dic[s][-1]+1] #for j in dic[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> using namespace std; #define ll long long #define mod 1000000007 #define maxn 200005 int c[maxn], pre[maxn], dp[maxn]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) scanf("%d", c + i); n = unique(c, c + n + 1) - c; dp[1] = 1; pre[c[1]] = 1; for (int i = 2; 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<iostream> using namespace std; long N; long C[200020]; long A[200020]; long X[200020]; int main(){ cin >> N; for (int i=1;i<=N;i++)cin >> C[i]; for (int i=0;i<200020;i++)X[i]=-1; A[0]=1; for (int i=1;i<=N;i++){ if (X[C[i]]!=-1 && X[C[i]]!=i-1){A[i]=A[X[C[i]]]+A[i-1];} else {A[i]=A[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...
#include <bits/stdc++.h> using namespace std; #define N 200100 const int mod=1e9+7; int n,c[N],L[N],now[N],dp[N]; int main() { scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&c[i]); n=unique(c+1,c+n+1)-c-1; for (int i=1;i<=n;i++) L[i]=now[c[i]],now[c[i]]=i; dp[0]=1; for (int i=1;i<=n;i++) { dp[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...
import java.util.*; import java.io.*; public class Main { static final int MOD = 1000000007; public static void main (String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int[] dp = new int[n + 1]; dp[0] = 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 P 1000000007 using namespace std; int f[220000],sum[220000]; int a[220000]; int n; int main(){ scanf("%d",&n); f[0]=1; for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=n;i++){ f[i]=f[i-1]; if(a[i]!=a[i-1])(f[i]+=sum[a[i]])%=P; if(a[i]!=a[i-1])(sum[a[i]]=f[i])%=P; } prin...
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 if N <= 2: print(1) exit() dp = [1,1] last_idx = [-1]*(2*10**5 + 1) last_idx[C[0]] = 0 last_idx[C[1]] = 1 for i in range(2, N): if C[i-1] == C[i] or last_idx[C[i]] == -1: dp.append(dp[-1] % MOD) else: dp.append((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<cstdio> #include<algorithm> using namespace std; int n, w[201000], P[201000]; long long D[201000], Mod = 1000000007, C[201000]; int main() { int i; scanf("%d", &n); D[0] = 1; for (i = 1; i <= n; i++) { scanf("%d", &w[i]); D[i] = D[i - 1]; if (w[i] != w[i - 1]) { D[i] = (D[i] + C[w[i]]) % Mod; 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<cstdio> using namespace std; int n,x,las,ans,c[200013]; int main(){ scanf("%d",&n); ans=1;las=-1; for(register int i=0;i<n;++i){ scanf("%d",&x); if(x==las)continue; las=x; ans=(1LL*ans+c[x])%1000000007; c[x]=ans; } printf("%d",ans); 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 sys input = sys.stdin.readline N=int(input()) mod=10**9+7 C=[int(input()) for i in range(N)] M=max(C)+2 dp=[0]*(N+1) co=[0]*M co[C[0]]=1 for i in range(1,N+1): if i<N and C[i-1]==C[i]: continue dp[i]+=co[C[i-1]] if i<N: co[C[i]]+=dp[i] co[C[i]]%=mod #print(dp,co) print(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 <map> using namespace std; #define N 200001 #define DD 1000000007 int a[N]; long long f = 1; map<int,long long> 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...
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)] import copy cc = copy.deepcopy(c) color_sum = {i: 0 for i in range(10 ** 6)} dp = [1 for _ in range(n + 1)] color_sum[cc[0]] = 1 for i in range(2, n + 1): dp[i] = dp[i - 1] if cc[i - 2] == cc[i - 1]: continue dp[i] += color_sum[...
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 i in range(N): c = int(input()) if len(C) == 0 or C[-1] != c-1: C.append(c-1) lis = [] last = [-1] * (2 * (10**5)) for i in range(len(C)): if i == 0: lis.append(1) elif last[C[i]] == -1 or last[C[i]] == i-1: lis.append(lis[-1]) 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...
n = int(input()) C = [int(input()) for i in range(n)] nc = 2 * 10 ** 5 + 3 mod = 10**9+7 A = [0] * n B = [-1] * nc A[-1] = 1 B[C[-1]] = n-1 # print(B) for i in range(n-2, -1, -1): if B[C[i]] != -1 and B[C[i]] != i+1: A[i] = (A[i+1] + A[B[C[i]]]) % mod else: A[i] = A[i+1] B[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 <iostream> #include <vector> int main(){ int n; std::cin >> n; std::vector<int> co(200000,0),dp(n,0),da(n); int c; std::cin >> c; da[0]=c; dp[0]=1; co[c-1]=1; for(int i=1;i<n;i++){ std::cin >> c; da[i]=c; dp[i]=dp[i-1]; if(da[i]!=da[i-1]){ dp[i]=(co[c-1]+dp[i])%1000000007; co[c-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...
#include <bits/stdc++.h> #define fo(i,a,b) for(int i=a;i<=b;++i) #define fod(i,a,b) for(int i=a;i>=b;--i) #define N 300005 #define LL long long #define mo 1000000007 using namespace std; LL f[N]; int lst[N],a[N],n; int main() { cin>>n; fo(i,1,n) scanf("%d",&a[i]); f[0]=1; fo(i,1,n) { f[i]=f[i-1]; if(lst[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...
#include<bits/stdc++.h> using namespace std; const int N=2e5+7,mod=1e9+7; int n,ans,c[N],f[N],lst[N]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&c[i]); if(c[i]==c[i-1])i--,n--; } f[0]=1; for(int i=1;i<=n;i++) { f[i]=f[i-1]; if(lst[c[i]])f[i]=(f[i]+f[lst[c[i]]])%mod; lst[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 <bits/stdc++.h> using namespace std; #define modp 1000000007 #define Maxn 200007 int n,a[Maxn],f[Maxn],s[Maxn]; int main() { scanf("%d",&n); int m=1; for (int i=1;i<=n;i++) scanf("%d",&a[i]); for (int i=2;i<=n;i++) if (a[i]!=a[m]) a[++m]=a[i]; n=m; f[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...
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]] SUM = 1 DP_dict = {} for c in C: if c in DP_dict.keys(): SUM += DP_dict[c] DP_dict[c] = SUM print(SUM % (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...
N = int(input()) L = [-1] * (2 * 10 ** 5 + 1) mod = 10 ** 9 + 7 dp = [0] * (N + 1) dp[0] = 1 for i in range(N): dp[i+1] += dp[i] color = int(input()) if L[color] != -1 and L[color] != i: dp[i+1] += dp[L[color]] L[color] = i+1 dp[i+1] %= 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...
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual soluti...
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.util.NoSuchElementException; public class Main { private static FastScanner sc = new FastScanner(); private static long MOD = 1000000007; public static void main(String[] args) { int N = sc.nextInt(); int[] C = new int[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; int f[200001],c[200001],pos[200001],nxt[200001]; int main() { int n,i; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&c[i]); if(pos[c[i]]) nxt[pos[c[i]]]=i; pos[c[i]]=i; } f[1]=1; for(i=1;i<=n;i++) { if(nxt[i]>i+1) f[nxt[i]]=(f[nxt[i]]+f[i])%100000000...
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 long long nmax=5e6+3; const long long mod=1e9+7; long long d[nmax],nr[nmax],k,x,n,v[nmax]; int main() { ios::sync_with_stdio(false); cin>>n; d[0]=1; for(long long i=1;i<=n;++i) { cin>>x; if(x==v[k]) continue; v[++k]=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...
#include <cstdio> int N; int mod = 1000000007; int cs[200008]; int not_first[200008]; long long recent[200008]; int main() { long long cur = 1; scanf("%d",&N); for(int i=0;i<N;i++) { scanf("%d", &cs[i]); if(not_first[cs[i]]) { if(cs[i-1] != cs[i]) { cur += recent[cs[i]]; cur %= mod; } recent[cs...
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.PrintWriter; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] rec=new int[200000]; Arrays.fill(rec,n); long[] dp=new long[n+1]; long mod=1000000007; dp[0]=1; for(int i=1; i<=n; i++){ int c=sc.ne...
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 MAXN=200005,MOD=1000000007; int n,A[MAXN],last[MAXN]; int dp[MAXN]; int main() { 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]]!=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...
#include <bits/stdc++.h> using namespace std; const int MAXN = 200005; const int MOD = 1000000007; int n, c[MAXN], a[MAXN], dp[MAXN]; int main(){ scanf("%d", &n); for(int i = 0; i < n; i++) scanf("%d", &c[i]); for(int i = n; i >= 0; i--){ if(i == n)dp[i] = 1; else{ if(i + 1 == n || c[i + 1] != c[i])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 <cstdio> // #include <vector> typedef long long ll; const int tt=1e9+7; int N,A[200005],F[200005],Ans=1; // std::vector<int> E[200005]; 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]) Ans=F[A[i]]=(Ans+F[A[i]])%tt; return printf("%d",Ans),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 = [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]] dp[i] %= mod 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 = [input() for i in range(N)] MOD = 1e9 + 7 C2 = [] re = "" L = {} LL = [-1] * N for i in C: if i != re: if i in L: LL[L[i]] = len(C2) L[i] = len(C2) else: L[i] = len(C2) C2.append(i) re = i DP = [-1] * (len(C2) + 1) DP[-1] = 1 for i in range(len(C2) - 1, -1, -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 itertools import groupby n = int(input()) C = [key for key, _ in groupby([int(input()) for _ in range(n)])] N = len(C) MOD = 10 ** 9 + 7 dp = [0 for _ in range(N)] dp[0] = 1 T = {} T[C[0]] = 0 for i in range(1, N): if i == 0 or C[i - 1] == C[i] or C[i] not in T: dp[i] = dp[i - 1] else: j = T[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...
from collections import defaultdict N = int(input()) tmp = [int(input()) for _ in range(N)] mod = 10 ** 9 + 7 C = [] for c in tmp: if not C: C.append(c) continue if C[-1] != c: C.append(c) N = len(C) last = defaultdict(int) ans = 1 for c in C: ans += last[c] ans %= mod last...
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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; public class Main { static final long MOD = 1000000007; static final int MAX = 200001; public static void mai...
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 = [input() for i in range(N)] dp = [1 for i in range(N)] last = {} for i in range(N) : if i == 0 : last[L[i]] = i else : if L[i] not in last : dp[i] = dp[i-1] elif L[i] == L[i-1] : dp[i] = dp[i-1] else : j = last[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...
//package com.company; // Always comment out package when submitting. import java.io.*; import java.util.*; public class Main { public static class Task { private static int mod = 1_000_000_007; public void solve(Scanner sc, PrintWriter pw) throws IOException { int n = sc.nextInt(); ...
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()) mod = 10 ** 9 + 7 c = [] for i in range(n): c.append(int(input())) d = {} dp = [[1,0]] p = -1 for ci in c: if p != ci: cnt = 0 if ci in d: cnt = sum(dp[d[ci]]) % mod dp.append([sum(dp[-1]) % mod, cnt]) d[ci]...
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 mod 1000000007 #define N 200005 using namespace std; int num[N],a[N],pre[N],bfr[N]; int main() { int n; cin >> n ; bfr[0]=1; int cnt=0; for(int i=1;i<=n;i++) {scanf("%d",&num[i]); if(num[i]!=num[i-1]||i==1) a[++cnt]=num[i];} for(int i=1;i<=cnt;i++) { int x; if(pre[a[i]]) x=bfr[...
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 = 200010, P = 1000000007; int n, a[maxn], pre[maxn], f[maxn]; int main() { scanf("%d", &n); for (int i = f[0] = 1; i <= n; i++) { scanf("%d", &a[i]), f[i] = f[i - 1]; if (pre[a[i]] && pre[a[i]] < i - 1) (f[i] += f[pre[a[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...
import java.util.*; import java.io.*; import java.math.*; public class Main { static class Graph0n { private ArrayList<Node0n> dt = new ArrayList<>(); Graph0n(int sz){for(int i=0; i<sz; i++){Node0n node1 = new Node0n();dt.add(node1);}} public void add(int vn, int val){ dt.get(vn).add(va...
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> typedef long long LL; const int maxn = 2e5 + 5; const LL mod = 1e9+7; LL last[maxn]; LL dp[maxn]; int s[maxn]; int main(){ int n; scanf("%d",&n); dp[0] = 1; for(int i = 1; i <= n; i++) scanf("%d",&s[i]); for(int i = 1; i <= n; i++){ dp[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...
import sys N = int(input()) C = [0] n = N for i in range(1, n + 1): c = int(sys.stdin.readline()) if c == C[-1]: N -= 1 else: C.append(c) dp = [0] * (N + 2) dp[0] = 1 dp[1] = 1 cumsum = [0] * (2 * (10 ** 5) + 2) cumsum[C[1]] = 1 for i in range(2, N+1): dp[i] = dp[i - 1] + cumsum[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<cstdio> #include<algorithm> #include<cstring> #define ll long long using namespace std; const ll MOD=1e9+7; int n,a[200005]; ll f[200005],g[200005]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); if(a[i]==a[i-1])i--,n--; } f[0]=1; for(int i=1;i<=n;i++) { f[i]=f[i-1]+g[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...
#include<bits/stdc++.h> #define int long long using namespace std; const int Mod=1e9+7,maxn=2e5+10; int n,a[maxn],f[maxn],s[maxn],cnt; signed main() { scanf("%lld",&n); for(register int i=1,x;i<=n;i++) { scanf("%lld",&x); if(x!=a[cnt]) a[++cnt]=x; } f[0]=1; for(register int i=1;i<=cnt;i++) { f[i]=(f[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 = [] prev = None for i in range(n): tmp = int(input()) if tmp==prev: n -= 1 continue else: C.append(tmp) prev = tmp dp = [0]*n dp[0] = 1 table = {C[0]:1} for i in range(n-1): if C[i+1] not in table: table[C[i+1]]= 0 dp[i+1] = dp[i] + table[C[i+1]] table[C[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...
def int_raw(): return int(raw_input()) def ss_raw(): return raw_input().split() def ints_raw(): return map(int,ss_raw()) INF = 1<<29 DIV = 10**9+7 def make2d_arr(w,h,val =INF): return [[val]*w for i in xrange(h)] def floyd_warshall(costs): N = len(costs) for k in xrange(N): for 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...
N = int(input()) C = [int(input()) for _ in range(N)] mod = 10**9+7 newC = [C[0]] for c in C: if newC[-1] == c: continue newC.append(c) N = len(newC) L = [0]*(N+1) L[0] = 1 idx = [-1]*(max(newC)+1) for i in range(N): if idx[newC[i]] >= 0: L[i+1] += L[idx[newC[i]]] L[i+1] += 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 mod=1e9+7; int n,a[330001],Next[330001],go[330001]; long long dp[330001],sum[330001]; int main() { cin>>n; for(int i=1;i<=n;i++)cin>>a[i]; n=unique(a+1,a+1+n)-a-1; dp[0]=1; for(int i=1;i<=n;i++) { dp[i]=(sum[a[i]]+dp[i-1])%mod; sum[a[i]]=(sum[a[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...
#!/usr/bin/env python2 from __future__ import division, print_function import itertools import os import sys from atexit import register from cStringIO import StringIO range = xrange sys.stdout = StringIO() register(lambda: os.write(1, sys.stdout.getvalue())) input = StringIO(os.read(0, os.fstat(0).st_size)).readlin...
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 = 2e5 + 5; const int mod = 1e9 + 7; int a[maxn], p[maxn], dp[maxn]; 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 (p[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...
n,*c=map(int,open(0).readlines()) l=[-1]*2**18 b=[] for i in range(n): b.append(l[c[i]]) l[c[i]]=i dp=[1]+[0]*n for i in range(1,n): dp[i]=dp[i-1] if c[i]!=c[i-1]and b[i]>-1: dp[i]=(dp[i]+dp[b[i]])%(10**9+7) print(dp[-2])
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)] dp = [0]*(N+1) lis = [-1] * (2 * 10**5 + 1) dp[0] = 1 lis[C[0]] = 0 for i,c in enumerate(C[1:],1): prev_i = lis[c] dp[i] = dp[i-1] if prev_i != i-1: dp[i] += dp[prev_i] dp[i] %= MOD lis[c] = i answer = dp[N-1] print(an...
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 { Scanner sc = new Scanner(System.in); public static void main(String[] args) { Main m = new Main(); m.run(); } void run() { int n = sc.nextInt(); int[] c = new int[n]; long mod = 1_000_000_007; int cmax = 2 * 100_000; long dp[] = new long[cmax+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 d = defaultdict(int) r = 1 p = "" for _ in range(int(input())): c = input() if c != p: r = (r + d[c]) % (10**9 + 7) d[c] = r p = c print(r)
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 N = int(input()) C = [0] for line in sys.stdin: c = int(line) if C[-1] == c: continue else: C.append(c) ans = 1 dic = {} for c in C: if c in dic.keys(): a = dic[c] dic[c] += ans ans += a else: dic[c] = ans 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...
N = int(input()) C = [int(input()) for _ in range(N)] dp = [0 for _ in range(N)] dp[0] = 1 MOD = 10**9 + 7 r = [-1 for _ in range(N)] prev = [-1 for _ in range(max(C) + 1)] for i in range(N): if prev[C[i]] != -1 and prev[C[i]] + 1 != i: r[prev[C[i]]] = i prev[C[i]] = i for i in range(N - 1): if...
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[200005] , f[200005]; int c[200005]; int n; const int mod = 1e9 + 7; int main() { scanf("%d",&n) ; for(int i = 1;i <= n;i++){ scanf("%d",&a[i]) ; if(a[i] == a[i - 1]) {i--;n--;} } f[0] = c[a[1]] = 1; 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()) lst=[0] mod=10**9+7 for i in range(N): a=int(input()) if lst[-1]==a: continue else: lst.append(a) length=len(lst) L=[0]*(2*10**5+1);DP=[0]*(length-1) DP.insert(0,1) for i in range(1,length): if L[lst[i]]==0: DP[i]=DP[i-1]%mod L[lst[i]]=i else: j=L[lst[i]] DP[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...
#include <bits/stdc++.h> using namespace std; typedef long long ll; int last,x,n,c[200019]; ll ans; int main() { 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...
import java.io.*; import java.time.Year; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.IntStream; import static java.lang.Math.*; import static java.lang.String.format; public class Main { public static void main(String[] args) { solve(...
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()) cl = [] for i in range(n): cl.append(int(input())) ans = [1 for i in range(n)] mod = 10**9+7 stones = dict() stones[cl[-1]] = n-1 for i in range(n-1): cur = cl[n-i-2] if cur in stones and stones[cur] != n-i-1: ans[n-i-2] = ans[n-i-1] + ans[stones[cur]] ans[n-i-2] %=...
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()) MOD = 10**9+7 dp = [0]*(N+1) dp[0] = 1 dic = defaultdict(int) p = -1 for i in range(N): c = int(input()) if c==p: dp[i+1] = dp[i] continue dic[c] += dp[i] dp[i+1] = dic[c]%MOD p = c # print(dp) 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 def input(): return sys.stdin.readline().rstrip() def main(): n=int(input()) mod=10**9+7 stone_last_pos=[-1]*(2*10**5+1) dp=[0]*(n+1) dp[0]=1 for i in range(1,n+1): c=int(input()) place=stone_last_pos[c] if place==i-1 or place==-1: dp[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...
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; #define MAXN 200000 #define MOD 1000000007 int n; int C[MAXN+5],dp[MAXN+5]; int pre[MAXN+5]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&C[i]); n=unique(C+1,C+n+1)-C-1; dp[0]=1; //printf("%d",n); 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...
from sys import stdin from itertools import repeat def main(): n = int(stdin.readline()) dp = [1] * (n + 1) a = map(int, stdin, repeat(10, n)) d = {} for i, x in enumerate(a): dp[i+1] = dp[i] if x in d and d[x] < i: dp[i+1] += dp[d[x]] if dp[i+1] >= 1000000007...
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.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; im...
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; constexpr int M = 1000000007; int main() { int n; cin >> n; int c[n]; for (int i = 0; i < n; i++) cin >> c[i]; long long sum[200100] = {}; long long ans = 1; sum[c[0]] = 1; for (int i = 1; i < n; i++) { if (c[i-1] == c[i]) continue; int t...