HDU 4455 Substrings(預處理+dp)
題目大意:給你n個數字,然後m次查詢,每次給你一個x,讓你求出來1到x,2到x+1。。。不同數的和。
需要各種預處理,處理出來所有的間隔之間有多少相同的數字,處理出來最後一個被去掉的間隔有多少個不重複的數字。
dp[i] = dp[i-1]-S+T.S代表最後被略去的那個區間的不同的數,T代表新區間擴張之後每個區間增加的不同的數的和。
Substrings
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1983 Accepted Submission(s): 608
Problem Description
XXX has an array of length n. XXX wants to know that, for a given w, what is the sum of the distinct elements’ number in all substrings of length w. For example, the array is { 1 1 2 3 4 4 5 } When w = 3, there are five substrings of length 3. They are (1,1,2),(1,2,3),(2,3,4),(3,4,4),(4,4,5)
The distinct elements’ number of those five substrings are 2,3,3,2,2.
So the sum of the distinct elements’ number should be 2+3+3+2+2 = 12
The distinct elements’ number of those five substrings are 2,3,3,2,2.
So the sum of the distinct elements’ number should be 2+3+3+2+2 = 12
Input
There are several test cases.
Each test case starts with a positive integer n, the array length. The next line consists of n integers a1,a2…an, representing the elements of the array.
Then there is a line with an integer Q, the number of queries. At last Q lines follow, each contains one integer w, the substring length of query. The input data ends with n = 0 For all cases, 0<w<=n<=106, 0<=Q<=104, 0<= a1,a2…an <=106
Each test case starts with a positive integer n, the array length. The next line consists of n integers a1,a2…an, representing the elements of the array.
Then there is a line with an integer Q, the number of queries. At last Q lines follow, each contains one integer w, the substring length of query. The input data ends with n = 0 For all cases, 0<w<=n<=106, 0<=Q<=104, 0<= a1,a2…an <=106
Output
For each test case, your program should output exactly Q lines, the sum of the distinct number in all substrings of length w for each query.
Sample Input
7
1 1 2 3 4 4 5
3
1
2
3
0
Sample Output
7
10
12
Source
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <time.h>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
///#define LL long long
#define LL __int64
#define INF 0x3f3f3f
#define PI 3.1415926535898
#define mod 1000000007
using namespace std;
const int maxn = 1001000;
int num[maxn];
int vis[maxn];
int f[maxn];
int p[maxn];
LL dp[maxn];
int main()
{
int n;
while(~scanf("%d", &n) && n)
{
memset(vis, 0, sizeof(vis));
memset(p, 0, sizeof(p));
for(int i = 1; i <= n; i++)
{
scanf("%d", &num[i]);
p[i-vis[num[i]]]++;
vis[num[i]] = i;
}
memset(vis, 0, sizeof(vis));
f[1] = 1;
vis[num[n]] = 1;
for(int i = 2; i <= n; i++)
{
if(vis[num[n-i+1]])
{
f[i] = f[i-1];
continue;
}
vis[num[n-i+1]] = 1;
f[i] = f[i-1]+1;
}
dp[1] = n;
int sum = n;
for(int i = 2; i <= n; i++)
{
dp[i] = dp[i-1]-f[i-1];
sum -= p[i-1];
dp[i] += sum;
}
int m;
scanf("%d",&m);
for(int i = 1; i <= m; i++)
{
int x;
scanf("%d",&x);
printf("%I64d\n",dp[x]);
}
}
return 0;
}
相關文章
- HDU3944 DP? (LUCAS定理+階乘預處理)
- HDU 2478 Slides(瞎搞,預處理)IDE
- hdu5435 數位dp(大數的處理)
- HDU 5317 RGCDQ (素因子分解+預處理)GC
- HDU 5439 Aggregated Counting(找規律+預處理)
- HDU4675 GCD of Sequence(預處理階乘逆元+推公式)GC公式
- 列舉子集+預處理最佳化dp+貪心視角轉化成可做dp
- HDU 4055 Number String:字首和優化dp【增長趨勢——處理重複選數】優化
- HDU 3853 LOOPS(概率dp)OOP
- 影像預處理
- 預處理指令
- 預處理命令
- HDU 4669 Mutiples on a circle (DP , 統計)
- HDU 4542 小明系列故事——未知剩餘系 (DFS 反素數 篩子預處理)
- 【基礎dp】HDU 1260 Tickets
- hdu 3401 單調佇列+DP佇列
- hdu4288 離線處理線段樹
- hdu 1753 Java 高精度處理 大明A+BJava
- 資料預處理
- 影像預處理方法
- HDU5139 Formula (找規律+離線處理)ORM
- HDU5200 Trees (離線處理)
- HDU 6415 Rikka with Nash Equilibrium (DP)UI
- HDU 3853 LOOPS:期望dp【網格型】OOP
- 【dp】HDU - 1069 Monkey and BananaNaN
- hdu4374單調佇列+dp佇列
- HDU 5119 Happy Matt Friends(DP)APP
- HDU 5326 Work (基礎樹形dp)
- hdu 4123 樹形DP+RMQMQ
- 資料預處理 demo
- 預設型DP
- 預設型 DP
- HDU6415:Rikka with Nash Equilibrium(dp)UI
- 【基礎dp】HDU 1176 免費餡餅
- HDU 5067 Harry And Dig Machine(狀壓dp)Mac
- 【scikit-learn基礎】--『預處理』之 缺失值處理
- nlp 中文資料預處理
- 機器學習一:資料預處理機器學習