Codeforces Round #479 (Div. 3) F. Consecutive Subsequence
Tag: DP
topic link
analyze
Although it is not difficult dp, I thought it was difficult to put it in the position of question F. I still thought it was complicated and it took a long time to get it out, and I also encountered a std::unordered_map
pit. There is a special card for a group of data on the CF, unordered_map
and then it is tle.
Because it unordered_map
is hash storage, the same hash value will be placed in a bucket, but the complexity of searching in the bucket is \(O(n)\) . The following optimization method was found on codeforces
unordered_map<int,int>mp;
mp.reserve(1024);
mp.max_load_factor(0.25);
That way it won't be troll.
See the following two links for specific principles:
http://codeforces.com/blog/entry/21853
http://en.cppreference.com/w/cpp/container/unordered_map/reserve
code
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <map>
#include <unordered_map>
using namespace std;
const int maxn=200050;
int a[maxn];
unordered_map<int,int> dp;
int main(){
dp.reserve(maxn);
dp.max_load_factor(0.25);
int n;
scanf("%d", &n);
int M=0,e=0;
for(int i = 0; i < n; ++i){
scanf("%d", a+i);
int& u=dp[a[i]];
if(dp.count(a[i]-1)) u=max(u,dp[a[i]-1]+1);
else u=1;
if(u>M){
M=u;
e=a[i];
}
}
int b=e-M+1;
cout << M << endl;
for(int i = 0; i < n; ++i){
if(a[i]==b){
printf("%d ", i+1);
b++;
}
}
return 0;
}