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...
#include <bits/stdc++.h> using namespace std; typedef unsigned int ui; typedef long long ll; typedef unsigned long long ull; constexpr int MAXC = 200000, MOD = 1000000007; int dp[MAXC + 2]; int main() { int N; cin >> N; int prev = 1; for(int i = 1, prevc = -1; i <= N; i++) { int C; cin >> C; if(C != 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...
#include <cstdio> const int MAXN=200005; int main() { int n,x; int p[MAXN],ans[MAXN]; for (int i=0;i<MAXN;++i) { p[i]=MAXN-1; ans[i]=0; } scanf("%d",&n); for (int i=0;i<n;++i) { scanf("%d",&x); if (i==0) ans[i]=1; else ans[i]=ans[i-1]+ans[p[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<bits/stdc++.h> using namespace std; long long n,i,c[200009],d[200009],e[200009]; int main() { for(d[0]=1,scanf("%lld",&n); n--; ++i,scanf("%lld",c+i),c[i]==c[i-1]?--i:e[c[i]]=d[i]=(d[i-1]+d[i]+e[c[i]])%1000000007); printf("%lld\n",d[i]),exit(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()) mod = 10**9 + 7 c = [int(input()) for i in range(n)] dp = [0 for i in range(n)] dp[0] = 1 memo = {c[0]:0} for i in range(1,n): if c[i] in memo: if memo[c[i]] != i-1: dp[i] =(dp[i-1] + dp[memo[c[i]]])%mod memo[c[i]] = i 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...
n=int(input()) c=[int(input()) for _ in range(n)] mod=10**9+7 dp=[0]*(n+1) dp[0]=1 last=[-1]*(2*10**5+1) last[c[0]]=0 for i in range(1,n): if c[i]==c[i-1] or last[c[i]]==-1: dp[i]=dp[i-1] else: dp[i]=(dp[i-1]+dp[last[c[i]]])%mod last[c[i]]=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...
#include <cstdio> const int MOD=1e9+7; int cs[200005]; int dp[200005]={1}; void addmod(int& x,int y){ x=(x+y)%MOD; } int main(){ int N; scanf("%d",&N); for(int i=1;i<=N;i++){ scanf("%d",&cs[i]); } for(int i=1;i<=N;i++){ if(cs[i]!=cs[i-1]){ addmod(dp[cs[i]],dp[cs[i-1]]); } } printf(...
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 P 1000000007 #define M 200005 int n,i,c[M],si; long long dp[M],a[M]; int main() { scanf("%d",&n); for(i=1; i<=n; i++)scanf("%d",&c[i]); for(i=1; i<=n; i++)if(c[i]!=c[i-1])c[++si]=c[i]; for(dp[0]=i=1; i<=si; i++)a[c[i]]=(dp[i-1]+a[c[i]])%P,dp[i]=a[c[i]]; printf("...
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()) cc = [int(input()) for i in range(n)] c = [cc[0]] for i in range(1,n): if cc[i] != cc[i-1]: c.append(cc[i]) n = len(c) dp = [0 for i in range(n)] pos_dp = [-1 for i in range(2*10**5+1)] dp[0] = 1 pos_dp[c[0]] = 0 for i in range(1,n): if pos_dp[c[i]]!=-1: dp[i] = (dp[i-1] + dp[pos_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...
import static java.lang.System.*; import java.util.*; public class Main { public static final int MOD = 1000000007; public static void main(String[] args) { Scanner sc = new Scanner(in); int n = sc.nextInt(); int[] stone = new int[n]; for(int i=0; i<n; i++) stone[i] = sc.nextInt()-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...
n=int(input()) latest_dic = {} array = [] answers = [] for i in range(n): c = int(input()) array.append(c) for i,c in enumerate(array): if i==0: answers.append(1) latest_dic[c] = 1 else: cnt = answers[i-1] if (c in latest_dic) and (c!=array[i-1]): cnt += ...
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()) l=[0]*2*10**5#ペアを作れるか診断 k=[0]*2*10**5#ansを記録 ans=1 pre=-1 for _ in range(N): c=int(input())-1 if pre==c: continue l[c]+=1 ans+=k[c]*min(l[c]-1,1) k[c]=ans pre=c """ print(c+1) print(l[:5],k[:5]) print(ans) """ print(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 rep(i,n) for((i)=1;(i)<=(n);(i)++) using namespace std; const int mod=1e9+7; int n,i,dp[200005],sum[200005],a[200005]; int main(){ cin>>n; rep(i,n)cin>>a[i]; dp[0]=1; rep(i,n){ if(a[i]!=a[i-1]) (sum[a[i]]+=dp[i-1])%=mod; dp[i]=sum[a[i]]; } cout<<dp[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 sys mod = 10**9 + 7 N = int(sys.stdin.readline()) stones = [] for _ in range(N): stones.append(int(sys.stdin.readline())) left = {} dp = [0 for _ in range(N+5)] dp[0] = 1 for i in range(N): dp[i+1] = dp[i] # 同じ色が前回出現した位置 c = stones[i] if c in left and 0 <= left[c] < i - 1: 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.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int mod = 1000000007; int[] idx = new int[200000]; Arrays.fill(idx,-1); int before = -1; long[] dp = new long[n+1]; dp[0]=1; for(int i=0;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...
mod = 10**9+7 from collections import defaultdict N = int(input()) C = defaultdict(list) dp = [1] * N for i in range(N): col = int(input()) dp[i] = dp[i-1] if not len(C[col]) == 0: if not C[col][-1] + 1 == i: dp[i] = (dp[C[col][-1]] +dp[i])%mod C[col].append(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...
#include<bits/stdc++.h> #define ll long long #define rg register using namespace std; const int MAXN=2e5+10; const int Mod=1e9+7; int n,Col[MAXN],Last[MAXN],F[MAXN]; int main() { scanf("%d",&n),F[1]=1; for(rg int i=1;i<=n;i++) scanf("%d",&Col[i]); Last[Col[1]]=1; for(rg int i=2;i<=n;i++) F[i]=(F[i-1]+F[Last[Col[...
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 MOD = 1e9 + 7; const int MAXN = 2e5; int N; int C[MAXN + 10]; long long dp[MAXN + 10], ss[MAXN + 10]; int main() { scanf("%d", &N); for(int i = 0; i < N; i++) { scanf("%d", &C[i]); } dp[N] = 1; for(int i = N-1; i >= 0; i--) { int c = 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> using namespace std; long MOD=1e9+7,n,c,p=-1,dp[1<<18],N; vector<long>v; map<long,long>m; int main(){ cin>>n; for(int i=0;i<n;i++){ cin>>c; if(c!=p){ v.push_back(c); p=c; } } if(n==1){ cout<<1<<endl; return 0; } dp[0]=1; m[v[0]]=1; N=v.size(); 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<iostream> using namespace std; const int N=2e5+10,mod=1e9+7; int f[N],nxt[N],a[N],nw[N],n; void add(int &x,int y) {x+=y;if(x>=mod) x-=mod;} int main() { cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=n;i>=1;i--) nxt[i]=nw[a[i]],nw[a[i]]=i; f[1]=1; for(int i=1;i<n;i++) { add(f[i+1],f[i]); if(nxt...
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])
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 read = sys.stdin.read MOD = 10**9+7 n, *C = map(int, read().split()) dp = [0]*(n+1) dp[0] = 1 idx = [-1]*(2*10**5+1) for i, c in enumerate(C, 1): dp[i] = dp[i-1] if idx[c] != -1 and idx[c] < i-1: dp[i] += dp[idx[c]] dp[i] %= MOD idx[c] = 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 sys DBG = not True R = 1000000007 MAXN = 200000 n = int(input()) c = [] for i in range(n): c.append(int(input())) tot = 1 s = [0] * (MAXN+1) for i in range(n): if i > 0 and c[i] == c[i-1]: continue x = s[c[i]] tot = (tot + x) % R s[c[i]] = tot if DBG: print("i {} sci {} 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...
n = int(input()) C = [0] + [int(input()) for i in range(n)] p = 10 ** 9 + 7 DP = [0] * (n + 1) last = [0] * int(2e5 + 1) DP[0] = 1 for i in range(n): if C[i + 1] == C[i]: DP[i + 1] = DP[i] % p else: DP[i + 1] = (DP[i] + last[C[i + 1]]) % p last[C[i + 1]] = DP[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 N = int(input()) C = [int(sys.stdin.readline()) for _ in range(N)] MOD = 10**9 + 7 A = [] for c in C: if A and A[-1] == c: continue A.append(c) NA = len(A) last = [-1] * 200001 B = [-1] * 200001 for i, a in enumerate(A): B[i] = last[a] last[a] = i dp = [0] * NA dp[0] = 1 for i in ra...
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...
#define _CRT_SECURE_NO_WARNINGS #include<cstdio> #include<cstring> #define Mod 1000000007 using namespace std; int a[200010],g[200010],f[200010],n,as; int main() { scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&a[i]); int n_=n;n=1; for (int i=1;i<=n_;i++) if(a[i]!=a[n]) a[++n]=a[i]; f[0]=1; 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...
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.*; public class Main { private static int MOD = 1000000007; public static void main(String[] args) throws Exception { Main main = new Main(); main.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...
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; #define mod ((ll)(1e9)+7) #define MAX 200000 ll d[MAX+1], s[MAX+1]; int main(){ int n; cin >> n; vector<int> v; for(int i=0; i<n; i++){ int c; cin >> c; if(v.size()==0 || *(v.end()-1)!=c) v.push_back(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<iostream> #include<cstdio> typedef long long ll; const ll mod=1e9+7; const int N=2e5+5; ll sum[N],dp[N]; int a[N],n,b[N],m; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",a+i); m=1;b[1]=a[1]; for(int i=2;i<=n;i++) if(a[i]!=a[i-1]) b[++m]=a[i]; dp[0]=1; for(int i=1;i<=m;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(raw_input()) s = [int(raw_input()) for _ in range(n)] mod = int(1e9+7) previous_idx = {} dp = [1] * n ans = 1 for i in range(n): c = s[i] if c in previous_idx and previous_idx[c] != i-1: ans += dp[previous_idx[c]] previous_idx[c] = i dp[i] = ans % mod print(ans % 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 <stdio.h> #define MOD 1000000007 int n; int dp[220000] = {1}; int memo[220000]; int main(){ scanf("%d", &n); for(int i = 1; i <= n; ++i){ int c; scanf("%d", &c); if(memo[c] == 0 || memo[c] == i - 1){ dp[i] = dp[i - 1]; }else{ dp[i] = (dp[i - 1] + dp[memo[c]]) % MOD; } memo[c] = i; } 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...
#include<bits/stdc++.h> using namespace std; const int maxn=4e5,mod=1e9+7; int n,cnt,f[maxn],c[maxn],vis[maxn],pre[maxn]; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){scanf("%d",&c[i]);} for(int i=1;i<=n;i++){ if(c[i]!=c[i-1]){cnt++;} c[cnt]=c[i]; } for(int i=1;i<=cnt;i++){ pre[i]=vis[c[i]]; vis[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...
n=int(input()) c=[int(input()) for _ in [0]*n] l=[-1]*200001 dp=[1]*n l[c[0]]=0 mod=10**+9+7 for i in range(1,n): ci=c[i] if l[ci] not in [-1,i-1]: dp[i]=(dp[i-1]+dp[l[ci]])%mod else: dp[i]=dp[i-1] l[ci]=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...
import sys input = sys.stdin.readline N = int(input()) c = [] for _ in range(N): c.append(int(input())) dp = [0] * N dp[0] = 1 dpc = [0] * (max(c) + 1) dpc[c[0]] = 1 for i in range(1, N): dp[i] = dp[i - 1] if c[i] != c[i - 1]: dp[i] += dpc[c[i]] dpc[c[i]] += dp[i - 1] 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...
#include<bits/stdc++.h> #define MOD 1000000007 using namespace std; const int maxn=2e5+5; int a[maxn]; int n; int dp[maxn][2]; int sum[maxn]; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); dp[0][0]=1; for(int i=1;i<=n;i++){ dp[i][0]=(dp[i-1][0]+dp[i-1][1])%MOD; if(a[i]!=a[i-1]){ 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; #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; vector<int> stn(2e5 + 5, -1); dp[0] = 1; for (int i = 0; i < n; ++i) { if (i) dp[i] = dp[i - 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...
import sys input=sys.stdin.readline N=int(input()) C=[int(input())] for i in range(N-1): tmp=int(input()) if tmp != C[-1]: C.append(tmp) import collections table=collections.defaultdict(int) N=len(C) dp=[0]*(N+1) dp[N]=1 mod=10**9 + 7 for i,x in enumerate(C[::-1]): dp[N-1-i]=dp[N-i] + table[x] dp[N-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...
N = int(input()) MOD = 10 ** 9 + 7 memo = {} prev_num = 1 prev = None for _ in range(N): c = int(input()) if prev == c: continue tmp = prev_num if c in memo: tmp += memo[c] tmp %= MOD memo[c] = tmp prev_num = tmp prev = c print(prev_num % 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.io.*; import java.util.*; import java.math.*; // import java.awt.Point; public class Main { InputStream is; PrintWriter out; String INPUT = ""; static int mod = 1_000_000_007; // int mod = 998244353; long inf = Long.MAX_VALUE/2; void solve(){ int n = 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...
#include<stdio.h> int main() { long n, c, p[222222], dp[222222],i; scanf("%ld", &n); dp[1] = 1; for (i = 1; i <= n; i++) p[i] = -1; scanf("%ld", &c); p[c] = 1; for (i = 2; i <= n; i++) { scanf("%ld", &c); dp[i] = dp[i - 1]; if (p[c] != -1 && p[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...
#include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll pre,x,n,i,tot=0,c[200005],dp[200005],vis[200005],sum[200005]; const ll mod=1e9+7; int main() { pre=0; scanf("%lld",&n); for(i=1;i<=n;i++) { scanf("%lld",&x); if(x!=pre)c[++tot]=x; pre=x; } sum[0]=1; for(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...
import java.util.Arrays; import java.util.Scanner; public class Main { private static final long DEV_NUM = 1000000000 + 7; private static final int MAX_COLOR_NUM = 2 * 100000; int[] jumpIndex = null; long[] patterns = null; int N = 0; public void solve() { Scanner in = new Scanner(System.in); N = in.nextI...
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 N; int pat[200020]; int last[200020]; const int M = 1000000000 + 7; int main() { while (cin >> N) { pat[0] = 1; for (int i = 1; i <= N; ++i) { int c; cin >> c; pat[i] = pat[i-1]; if (last[c] > 0 && i - last[c] > 1) { pat[i] = (pat[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; typedef long long ll; const int N = 2e5+87, M=1e9+7; int c[N],la[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; ++i) cin >> c[i]; n = unique(c,c+n) - c; int*a=c-1,dp=1; 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...
n = int(input("")) mod = 1e9+7 maxn = 200000 x = [] f = [] sum = [0 for j in range(0,maxn+1)] def add(a,b): s = (a+b)%mod return s f.append(1) x.append(-1) for i in range(1,n+1): x.append(int(input())) if(x[i]==x[i-1]): f.append(f[i-1]) else : f.append(add(f[i-1],sum[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...
#include <bits/stdc++.h> using namespace std; #define M 200005 int n,A[M]; int dp[M],sum[M]; const int mod=1e9+7; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&A[i]); memset(sum,0,sizeof(sum)); dp[0]=1; for(int i=1;i<=n;i++){ dp[i]=dp[i-1]; if(A[i]!=A[i-1]){ dp[i]=(dp[i]+sum[A[i]])%mod; su...
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 sz=2e5+10, mod=1e9+7; int su[sz]; int main() { //freopen("input.txt", "r", stdin); int n, dp=1; cin>>n; int ar[n]; for(int a=0; a<n; a++) scanf("%d", &ar[a]); int yk=0; while(yk<n) { int x=ar[yk]; while(yk<n and ar[y...
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 = [1]*(N+1) colors = [1]*(2*10**5+1) S = [-1]*(2*10**5+1) Q = 10**9+7 for i in range(N): if i >= 1: if C[i] == C[i-1]: dp[i+1] = dp[i] continue if S[C[i]] == -1: dp[i+1] = dp[i] S[C[i]] = 1 if 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 subC = [-2] * (2 * 10 ** 5 + 10) cntC = [-1]*N for num, i in enumerate(C): cntC[num] = subC[i]+1 subC[i] = num # print(cntC) dp = [0] * (N + 10) dp[0] = 1 for i in range(N): if cntC[i] < 0 or cntC[i] == i: dp[i + 1] = dp[i] % MO...
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.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int [] ways = new int[200001]; ArrayList<Integer> stones = new ArrayList<>(); int current = sc.nextI...
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;const int N=2*1e5+10;typedef long long ll;const ll mod=1e9+7; ll coldp[N];ll dp[N];int c[N];int n; int main() { dp[0]=1; scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&c[i]); for(int i=1;i<=n;i++) { dp[i]=dp[i-1]; if((i-1)&&c[i-1]!=c[i]) (dp[i]+=col...
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)] v_c = [] MOD = 10**9+7 for i,v in enumerate(c): if i > 0 and v == c[i-1]: continue v_c.append(v) dp = [1]*(n+1) color_cnt = [-1]*(2*10**5+5) for i in range(1, len(v_c)+1): color = v_c[i-1] if color_cnt[color] > 0: 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...
mod=7+10**9 n=int(input()) c=['$']+[int(input()) for _ in [0]*n] data=[1]+[0]*n s={} for i in range(1,n+1): if c[i] in s.keys(): if c[i]!=c[i-1]: s[c[i]]+=data[i-1] s[c[i]]%=mod else: s[c[i]]=data[i-1] data[i]=s[c[i]]%mod print(data[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...
def main(): n = int(input()) c = [] mod = 10 ** 9 + 7 state = [0 for i in range(n+200000)] dp = [0 for i in range(n+1)] dp[0] = 1 for i in range(n): c_i = int(input()) if state[c_i] >= 1 and state[c_i] != i: dp[i+1] = (dp[state[c_i]] + dp[i]) % 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...
N=int(input()) dp=[1 for i in range(0,N)] data={i:-1 for i in range(1,2*10**5+1)} mod=10**9+7 c=int(input()) data[c]=0 for i in range(1,N): c=int(input()) if data[c]==i-1: dp[i]=dp[i-1] elif data[c]==-1: dp[i]=dp[i-1] else: dp[i]=(dp[i-1]+dp[data[c]])%mod data[c]=i print(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 <iostream> #include <cstdio> #define N 200005 using namespace std; int n, a, M=1e9+7, dp[N]={1}, pr[N]; int main() { cin >> n; for(int i=1; i<=n; i++) { scanf("%d", &a); dp[i] = dp[i-1]; if(pr[a]>0 && i-pr[a]>1) dp[i] = (dp[i] + dp[pr[a]]) % M; pr[a] = i; } cout << dp[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...
#include "bits/stdc++.h" using namespace std; int main() { long long N, MOD = 1000000007; static long long C[200000], DP[200010] = {}, X[200010] = {}; cin >> N; for (int i = 0; i < N; i++) cin >> C[i]; DP[0] = 1; X[C[0]] = 1; for (int i = 1; i < N; i++) { if (C[i] == 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...
import sys n=int(input()) c=[int(i) for i in sys.stdin] #連続した数を除去したlist d=[] #前の数を保存する変数 stack=0 for i in c: if i!=stack: d.append(i) stack=i #0で初期化 x=[0]*200001 sum=1 p=10**9+7 for i in range(0,len(d)): x[d[i]]= (x[d[i]]+sum)%p sum=x[d[i]]%p print(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...
MOD = 10**9 + 7 N = int(input()) C = [] last_idx = [0 for i in range(200001)] bef = [0 for i in range(N+1)] for i in range(1,N+1): c = int(input()) C.append(c) bef[i] = last_idx[c] last_idx[c] = i dp = [0 for i in range(N+1)] dp[1] = 1 if N == 1 or N == 2: print(1) exit() for i in range(2, N+1): if bef[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...
from collections import defaultdict M = 10**9 + 7 N = int(input()) C = [0]*N for i in range(N): C[i] = int(input()) P = [0]*N Q = [0]*N QS = defaultdict(int) Q[0] = QS[C[0]] = 1 for i in range(1,N): if C[i] == C[i-1]: Q[i] = 0 else: Q[i] = (P[i-1] + Q[i-1]) % M P[i] = QS[C[i]] QS[...
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: C.append(c) ans = 1 dic = defaultdict(lambda: 0) mod = 10 ** 9 + 7 for c in C: a = dic[c] dic[c] += ans ans = (ans + a) % mod print(ans % 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> #define MOD 1000000007 using namespace std; int main() { int n; cin >> n; long ans = 1; int prev = -1; unordered_map<int, int> counter; for (int i = 0; i < n; i++) { int c; cin >> c; if (c != prev) { ans += counter[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...
#include<iostream> #include<cstdio> using namespace std; const int N=200005,mod=1e9+7; int n,a[N],f[N],s[N]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); f[0]=1,s[a[1]]=1; for(int i=1;i<=n;i++) { // for(int j=1;j<=i;j++) // if(a[i]==a[j]&&a[j-1]!=a[i]) // f[i]=(f[i]+f[j-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...
MOD=1000000007 N=int(input()) d = {} pairs=[-1]*N for i in range(N): c = int(input()) if c in d and i-d[c]>1: pairs[d[c]] = i d[c]=i dp1,dp2=[0]*N,[0]*N prev=1 for src in range(N): dp2[src]=(prev+dp1[src])%MOD dst = pairs[src] if dst>0: dp1[dst] = (dp1[dst]+dp2[src])%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; const int N=2e5+10; const int p=1e9+7; int n, c[N]; int dp[N], t[N], nxt[N]; int main(){ scanf("%d", &n); for(int i=1; i<=n; ++i){ scanf("%d", &c[i]); if(t[c[i]])nxt[t[c[i]]]=i; t[c[i]]=i; } dp[1]=1; for(int i=1; i<=n; ++i){ if(nxt[i]>i+1)(dp[nxt[i]]+=dp[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 MN = 2e5+5, mod = 1e9+7; int dp[MN], lst[MN], N, i, x, y; int main(){ for(scanf("%d",&N),i=1;i<=N;i++){ scanf("%d",&x); dp[i] = dp[i-1]; if(lst[x]&&x!=y) dp[i]=(dp[i]+dp[lst[x]]+1)%mod; lst[x]=i; y=x; } printf("%d\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...
#include<bits/stdc++.h> #define maxn 205050 using namespace std; typedef long long ll; const ll M=1000000007; ll a[maxn],c[maxn],b[maxn],dp[maxn]; int n,m; int main(){ cin >> n; for (int i=1;i<=n;i++){ cin >> a[i]; if (a[i]!=a[i-1]) b[++m]=a[i]; } dp[0]=1; c[b[1]]=1; 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...
#include<cstdio> #include<algorithm> using namespace std; #define MAXN 200010 #define MO 1000000007 #define LL long long int n,sum[MAXN],ans=1; int main() { scanf("%d",&n); for(int i=1,col=0,c;i<=n;i++) { scanf("%d",&c); if(col==c) continue; int tmp=sum[c]; (sum[c]+=ans)%=MO; (ans+=tmp)%=MO; col=c; } 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> #define rep(i,x,y) for (int i=(x);i<=(y);i++) using namespace std; const int N=2e5+100,mod=1e9+7; int n,x,f[N],g[N],a[N]; void upd(int &x,int y){x+=y; x-=x>=mod?mod:0;} int main(){ scanf("%d",&n); rep (i,1,n) scanf("%d",&a[i]); n=unique(a+1,a+1+n)-a-1; f[0]=1; rep (i,1,n) upd(g[a[i]],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 <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; typedef long long int ll; ll mod=1e9+7; ll dp[200100]; ll last[200100]; int main(){ int n; cin >> n; vector<int> c(n); dp[0]=1; for(int i=0;i<n;i++){ cin >> c[i]; if(last[c[i]]&&last[c[i]]<i){ (dp[i+1]+=dp[las...
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)] L = [-1]*(2*10**5+10) dp = [-1]*(2*10**5+10) dp[0] = 1 for i in range(N): if L[C[i]] == -1: dp[i+1] = dp[i] elif C[i] == C[i-1]: dp[i+1] = dp[i] else: dp[i+1] = dp[i]+dp[L[C[i]]] dp[i+1] %= MOD 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...
N = int(input()) s = set() dic = {} memo = [0]*(N+1) sm = {} memo[0] = 1 P = 10**9 + 7 b = 0 for i in range(1,N+1): c = int(input()) if c not in s: s.add(c) dic[c] = [i] memo[i] = memo[i-1] else: memo[i] = memo[i-1] if b != c: if len(dic[c]) == 1: sm[c] = memo[dic[c][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 maxn 200010 #define modn 1000000007 int a[maxn],f[maxn],cnt[maxn],n,i; int main() { scanf("%d",&n); for (i=1;i<=n;i++) scanf("%d",&a[i]); f[0]=1; for (i=1;i<=n;i++) if (a[i]==a[i-1]) f[i]=f[i-1]; else { f[i]=(f[i-1]+cnt[a[i]])%modn; cnt[a[i]]=f[i]; } printf("%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...
n = int(input()) a = [] ans = [] c = {} for i in range(n): a.append(int(input())) ans.append(0) ans[0] = 1 c[a[0]] = 0 for i in range(1,n): if a[i] not in c: c[a[i]] = i if a[i] == a[i-1]: ans[i] = ans[i-1] else: ans[i] = (ans[i-1] + ans[c[a[i]]]) % 1000000007 c[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...
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Arrays; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.UncheckedIOException; import java.nio.charset.Charset; import java.util.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...
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.NoSuchElementException; import java.util.PrimitiveIterator; import java.util.function.IntBinaryOperator; import java.util.function.IntFunction; import java.util.function.IntPredicate; 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()) 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 = [0] * (ln + 1) dp[0] = 1 acc = [0] * (MAX + 1) for i, e in enumerate(cc, 1): dp[i] = dp[i-1] + acc[e] dp[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 java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.charset.Charset; import java.util.Arrays; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) throws Exception { bool...
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 import sys input=sys.stdin.readline sys.setrecursionlimit(10**9) mod=10**9+7 n=int(input()) C=[int(input()) for _ in range(n)] A=[0]*(2*10**5+1) #A[i]:色iが最後にあった場所(1-indexed) B=[1] for i,c in enumerate(C): #print(A[c]) tmp=B[-1] if A[c]!=i and A[c]!=0: tmp+=B[A[c]] A[c]=i+1 B.append(tmp%mo...
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,M=1e9+7; int n,a[N],dp[N],s[N]; int main(){ scanf("%d",&n); for (int i=1;i<=n;i++)scanf("%d",&a[i]); int m=1; for (int i=2;i<=n;i++) if (a[i]!=a[i-1])a[++m]=a[i]; dp[0]=1; for (int i=1;i<=m;i++){ dp[i]=(s[a[i]]+dp[i-1])%M; (s[a[i]]+=dp[i-1])%=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...
#include <iostream> using namespace std; using ll = long long; const ll M = 1e9 + 7; ll n, a[200005], b[200005], d[200005], s[200005]; int main() { scanf("%lld", &n); s[0] = d[0] = 1; for(ll i = 1; i <= n; i++) { scanf("%lld", &a[i]); if(b[a[i]] > 0 && b[a[i]] < i - 1) { d[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 ll MOD = 1e9+7; int main() { int n; cin >> n; vector<ll> c(n); for(auto &i:c)cin >> i; c.erase(unique(c.begin(),c.end()),c.end()); //for(auto &i:c)cout<<i<<endl; vector<int> tmp(200001,0); tmp[c[0]]=1; for(int i = 1;i<c.size();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 maxn = 2e5 + 20, mod = 1e9 + 7; long long n, c[maxn], dp[maxn], p[maxn]; int main () { cin >> n; for (int i = 1; i <= n; i++) cin >> c[i]; dp[1] = 1; p[c[1]] += 1; for (int i = 2; i <= n; i++) { if (c[i] != c[i - 1]) p[c[i]] = (p[c[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...
N = int(input()) A = [int(input()) for _ in range(N)] MOD = 10**9 + 7 dp = [0] * N prev_i = [-1] * (2 * 10**5 + 1) dp[0] = 1 prev_i[A[0]] = 0 for i in range(1, N): a = A[i] if prev_i[a] >= 0: if prev_i[a] == i - 1: dp[i] = dp[i - 1] else: dp[i] = dp[i - 1] + dp[prev_i[a]] 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...
import sys mod = 10**9 + 7 N = int(sys.stdin.readline()) stones = [] for _ in range(N): stones.append(int(sys.stdin.readline())) left = [-1 for _ in range(2*10**5+1)] dp = [0 for _ in range(N+5)] dp[0] = 1 for i in range(N): dp[i+1] = dp[i] # 同じ色が前回出現した位置 c = stones[i] if 0 <= left[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...
MOD = 10**9 + 7 N = int(input()) C = [int(input()) for i in range(N)] dp = [0] * (N+1) dp_c = [0] * (2*10**5+1) pred_ci = 0 dp[0] = 1 for i, ci in enumerate(C, 1): if pred_ci == ci: dp[i] += dp[i-1] continue dp[i] = (dp[i-1] + dp_c[ci]) % MOD dp_c[ci] = dp[i] pred_ci = ci 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...
#include<algorithm> #include<cstdlib> #include<cstdio> #include<cmath> #define N 200020 #define mod 1000000007 using namespace std; int n,m; int a[N]; long long dp[N],tot[N]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=n;i++)if(i==1||a[i]!=a[i-1])a[++m]=a[i]; dp[0]=1; for(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...
# 入力 N = int(input()) C = [int(input()) for _ in range(N)] # 現時点で最も左の、色C_iの石が存在する負のインデックス(0は存在しないことを表す) k = [0 for _ in range((2 * 10**5) + 1)] # 動的計画法により解を求める dp = [0 for _ in range(N)] k[C[-1]] = -1 dp[-1] = 1 for i in range(2, N + 1): if k[C[-i]] == 0 or k[C[-i]] == -i + 1: dp[-i] = dp[-i + 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...
#include <bits/stdc++.h> using namespace std; long long dp[200004]; int c[200004]; int main(){ int n; const int mod=1e9+7; scanf("%d",&n); for(int i=0;i<n;++i) scanf("%d",&c[i]); dp[c[n-1]]=1; for(int i=n-1;i>=0;--i){ if(i && c[i-1]==c[i]) continue; dp[c[i-1]]=(dp[c[i-1]]+dp[c[i]])%mod; } printf("%lld\...
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 #define mod 1000000007 #define for1(i, n) for(int i = 1; i <= (n);i++) using namespace std; int input() { int r; scanf("%lld", &r); return r; } int n, c[234567], dp[234567], c1[234567]; signed main() { n = input(); for1(i, n)c[i] = input(); dp[0] = 1; for1(i, n) { 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 <iostream> using namespace std; int c[200009]; long long p[200009], d[200009]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; d[0] = 1; for (int i = 1; i <= n; i++) { cin >> c[i]; if (c[i] == c[i - 1]) { d[i] = d[i - 1]; continue; } p[c[i]] = (p[c[i]] + 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...
N=int(input()) MOD=10**9+7 memo={} #prev_numがその時点での組み合わせ数 prev_num=1 prev=None for _ in range(N): c=int(input()) if c==prev: continue tmp=prev_num if c in memo: tmp+=memo[c] tmp%=MOD #memo[c]にそれまでの組み合わせの数が入ることで、 #次にcが出てきた時にどれぐらい通りが増えるかが分かる memo[c]=tmp prev_num=tmp prev=c print(prev_num)
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 from itertools import groupby read = sys.stdin.read N, *C = map(int, read().split()) C = [i for i, _ in groupby(C)] mod = 10 ** 9 + 7 cnt = defaultdict(int) dp = 1 for i in C: prev = dp dp = (cnt[i] + dp) % mod cnt[i] = (cnt[i] + prev) % mod 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 <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<(n);i++) #define ll long long int main(){ int N,C; cin>>N; int X[200010]={0}; ll Y[200010]={0}; rep(i,N){ cin>>C; if(X[C]&&(X[C]!=i)) Y[i-1]=X[C]; X[C]=i+1; } for(int i=2;i<200010;i++){ if(Y[i]==0) Y[i]=Y[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.*; public class Main { static final int MOD = 1000000007; public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); HashMap<Integer, Integer> map = new HashMap<>(); ArrayList<Integer> list = new ArrayList<>(); int prev = 0; int idx = 0; int 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...
N = int(input()) C = [int(input()) for _ in range(N)] MOD = 10 ** 9 + 7 recent = [-1 for _ in range(max(C))] hist = [] ans = 1 for i in range(N): c = C[i] - 1 if recent[c] >= 0 and recent[c] != i-1: ans = (ans + hist[recent[c]]) % MOD recent[c] = i hist.append(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...
from collections import defaultdict mod = 10**9+7 n = int(input()) c = [0] d = defaultdict(list) r = defaultdict(int) idx = 1 for i in range(n): tmp = int(input()) if c[-1] != tmp: if d[tmp]: r[idx] = d[tmp][-1] d[tmp].append(idx) c.append(tmp) idx += 1 n = len(c) 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.*; public class Main { int n; int[] a; long MOD = (long)1e9 + 7; Map<Integer, LinkedList<Integer>> i2ps = new HashMap<>(); Map<Integer, Long> sum = new HashMap<>(); void run() { Scanner sc = new Scanner(System.in); LinkedList<Integer> as = new LinkedList<>(...
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 last[200010]; int dp[200010]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; dp[0]=1; for(int i=1;i<=n;i++){ int x; cin>>x; dp[i]=dp[i-1]; if(last[x]!=i-1&&last[x]){ dp[i]+=dp[last[x]]; if(dp[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> #define f(i,n) for(int i=0;i<(n);i++) using namespace std; int x[200000],y[200000],dp[200000]; signed main(){ int n; cin>>n; f(i,200000) y[i]=-1; dp[0]=1; f(i,n){ cin>>x[i]; if(y[x[i]-1]>=0&&i-y[x[i]-1]>1)dp[i]+=dp[y[x[i]-1]]; y[x[i]-1]=i; if(i) dp[i]+=dp[i-1]; dp[i]%=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...
#include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #define fo(i,j,k) for(int i=j;i<=k;++i) #define fd(i,j,k) for(int i=j;i>=k;--i) using namespace std; const int N=2e5+10,mo=1e9+7; int wz[N],a[N],f[N]; int main() { int n; scanf("%d",&n); f[0]=1; fo(i,1,n){ int x; scanf("%d",&x); f[i]=f[i...