比赛
C
Compress Words
字符串哈希 或者 kmp
M
Unique Bid Auction
就模拟
这个题让我对memset的使用有了更深的了解
如果用 sizeof(x)来规定重置数组的大小会造成tle
对于大量无用的数组初始化
所以更好的规定是 根据数据的字节大小
如 * 处的
#include<cstring>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int N = 1000000;
ll k, b, c, d, sum, t;
int a[N],x[N];
int n;
void work()
{
scanf("%d", &n);
memset(x, 0, (n+1)*4);// *
for(int i = 1; i <= n; ++i)
{
scanf("%d", &a[i])
x[a[i]]++;
}
int mi = 1000000, id;
for(int i = 1; i <= n; ++i)
{
if(a[i] < mi && x[a[i]] == 1 )
{
mi = a[i];
id = i;
}
//x[a[i]] = 0;
//cout << x[a[i]] <<" ";
}
//cout << endl;
if(mi == 1000000) cout << -1 << endl;
else cout << id << endl;
}
int main()
{
cin >> t;
while(t--)
work();
return 0;
}
这篇文章探讨了在比赛中如何通过合理使用memset避免数组初始化冗余,特别是在处理字符串哈希和KMP问题时。作者通过实例展示了如何根据数据字节大小规定数组大小,从而提升代码效率,避免了Time Limit Exceeded (TLE)错误。
492

被折叠的 条评论
为什么被折叠?



