CodeForces 977F. Consecutive Subsequence
unordered_map用Hashtable实现,操作均摊常数时间复杂度,但是可能存在模数冲突,会被卡到On导致TLE,最好手写一个hash映射,防止被卡。当然,时间允许的话可以用map的稳定logn时间复杂度。
AC代码:
struct node { int siz,idx; }; inline int gethash(int x)//手写映射 { return x*1331+1333331; } unordered_map<int, node> mp; // num size idx int ans[200010], ct; int a[200010]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d",&a[i]); mp[gethash(a[i])].siz = mp[gethash(a[i]-1)].siz + 1; mp[gethash(a[i])].idx = i; } int pos, maxx = -1,pos2; for (unordered_map<int, node>::iterator it = mp.begin(); it != mp.end(); it++) if (it->second.siz > maxx) { maxx = it->second.siz; pos2=it->second.idx; } pos =a[pos2]-maxx+1; printf("%d\n",maxx); for(int i =1; i<=n; i++) if(a[i]==pos) { printf("%d ",i); pos++; } return 0; }