标签:
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2340 Accepted Submission(s): 731
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 1000010; 5 LL dp[maxn],delta; 6 int d[maxn],pre[maxn],len[maxn],n,q; 7 bool vis[maxn]; 8 int main() { 9 while(~scanf("%d",&n),n) { 10 memset(len,0,sizeof len); 11 memset(vis,false,sizeof vis); 12 memset(pre,-1,sizeof pre); 13 for(int i = 0; i < n; ++i) { 14 scanf("%d",d+i); 15 len[i - pre[d[i]]]++; 16 pre[d[i]] = i; 17 } 18 for(int i = n-1; i >= 0; --i) len[i] += len[i+1]; 19 dp[0] = 0; 20 dp[1] = n; 21 delta = 1; 22 vis[d[n-1]] = true; 23 for(int i = 2; i <= n; ++i) { 24 dp[i] = dp[i-1] + len[i] - delta; 25 if(!vis[d[n-i]]) { 26 delta++; 27 vis[d[n-i]] = true; 28 } 29 } 30 scanf("%d\n",&q); 31 while(q--) { 32 scanf("%d",&n); 33 printf("%I64d\n",dp[n]); 34 } 35 } 36 return 0; 37 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4779273.html