STL unordered_map模数冲突

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;
}
/* Created by Pujx on 2024/3/16. */ #pragma GCC optimize(2, 3, "Ofast", "inline") #include <bits/stdc++.h> using namespace std; #define endl '\n' //#define int long long //#define double long double using i64 = long long; using ui64 = unsigned long long; using i128 = __int128; #define inf (int)0x3f3f3f3f3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define yn(x) cout << (x ? "yes" : "no") << endl #define Yn(x) cout << (x ? "Yes" : "No") << endl #define YN(x) cout << (x ? "YES" : "NO") << endl #define mem(x, i) memset(x, i, sizeof(x)) #define cinarr(a, n) for (int i = 1; i <= n; i++) cin >> a[i] #define cinstl(a) for (auto& x : a) cin >> x; #define coutarr(a, n) for (int i = 1; i <= n; i++) cout << a[i] << " \n"[i == n] #define coutstl(a) for (const auto& x : a) cout << x << ' '; cout << endl #define all(x) (x).begin(), (x).end() #define md(x) (((x) % mod + mod) % mod) #define ls (s << 1) #define rs (s << 1 | 1) #define ft first #define se second #define pii pair<int, int> #ifdef DEBUG #include "debug.h" #else #define dbg(...) void(0) #endif const int N = 2e5 + 5; //const int M = 1e5 + 5; const int mod = 998244353; //const int mod = 1e9 + 7; //template <typename T> T ksm(T a, i64 b) { T ans = 1; for (; b; a = 1ll * a * a, b >>= 1) if (b & 1) ans = 1ll * ans * a; return ans; } //template <typename T> T ksm(T a, i64 b, T m = mod) { T ans = 1; for (; b; a = 1ll * a * a % m, b >>= 1) if (b & 1) ans = 1ll * ans * a % m; return ans; } int a[N]; int n, m, t, k, q; void work() { cin >> n; map<string, int> mp; auto read = [&] () -> string { string s, t; for (int i = 1; i <= 8; i++) cin >> t, s += t; return s; }; while (n--) cout << ++mp[read()] << endl; } signed main() { #ifdef LOCAL freopen("C:\\Users\\admin\\CLionProjects\\Practice\\data.in", "r", stdin); freopen("C:\\Users\\admin\\CLionProjects\\Practice\\data.out", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int Case = 1; //cin >> Case; while (Case--) work(); return 0; } /* _____ _ _ _ __ __ | _ \ | | | | | | \ \ / / | |_| | | | | | | | \ \/ / | ___/ | | | | _ | | } { | | | |_| | | |_| | / /\ \ |_| \_____/ \_____/ /_/ \_\ */
最新发布
12-08
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hesorchen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值