memset的易错点

memset的作用一般就是用来初始化数组例如用来将a数组全部归零
memset(a,0,sizeof a)或者全部为无穷大memset(a,0x3f,sizeof a),
**这个虽然好用但很容易错,而且一般都不会往这里想,血的记忆 **

数据卡memset

如果memset的第三个参数是是 sizeof a的话就有可能被卡,因为如果有1e4个测评数据,a数组的大小为1e6
那就要1e10次操作,大概率是无了,*但是这种情况有,但很少,这个也很好处理,引入n这个参数就行了
a的类型为int,一个int 为四个字节所有可以用
memset(a,0,4
(n+5))**来替代,+5是避免边界问题

字节数填错

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#define fir(i, a, b) for (int i = a; i <= b; i++)
typedef long long LL;
#define met(x, y) memset(x, 0, y)
//#define read(x) scanf("%d",&x)
using namespace std;
typedef pair<int, int> PII;
const int N = 2e5 + 10;
int a[N];
int x[N], y[N];
int cnt[N];
int l[N], r[N];
vector<int> cx, cy;
int read()
{
    char ch = getchar();
    int x = 0, w = 1;
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = (x << 1) + (x << 3) + ch - '0';
        ch = getchar();
    }
    return x * w;
}
int main()
{
    // freopen("test.txt", "r", stdin);
    int t = read();
    while (t--)
    {
        int n = read();
        met(cnt, n);
        met(l, n);
        met(r, n);
        cx.clear(), cy.clear();
        
        bool fl = true;
        for (int i = 1; i <= n; i++)
            a[i] = read();
        for (int i = 1; i <= n; i++)
        {
            cnt[a[i]]++;
            if (l[a[i]] == 0)
                l[a[i]] = i;
            else
                r[a[i]] = i;
            if (cnt[a[i]] > 2)
                fl = false;
        }
        if (cnt[1] >= 2)
            fl = false;
        if (!fl)
        {
            puts("NO");
            continue;
        }
        if (cnt[1] == 1)
            x[l[1]] = 1, y[l[1]] = 1;
            if(cnt[1]==0)cx.push_back(1);
        for (int i = 2; i <= n; i++)
        {
            if (cnt[i] == 1)
                x[l[i]] = i, y[l[i]] = i;
            else if (cnt[i] == 0)
                cx.push_back(i);
            else
                cy.push_back(i);
        }
        //cout<<cx.size();
        if (cx.size() != cy.size())
            fl = false;
        if (!fl)
        {
            puts("NO");
            continue;
        }
        for (int i = 0; i < cx.size(); i++)
            if (cx[i] > cy[i])
                fl = false;
        if (!fl)
        {
            
            puts("NO");
            continue;
        }
        for(int i=0;i<cy.size();i++)
        {
            x[l[cy[i]]]=cy[i],y[r[cy[i]]]=cy[i],y[l[cy[i]]]=cx[i],
            x[r[cy[i]]]=cx[i];
        }
        puts("YES");
        for(int i=1;i<=n;i++)cout<<x[i]<<" ";
        puts("");
        for(int i=1;i<=n;i++)cout<<y[i]<<" ";
        puts("");
    }
    // freopen("CON", "r", stdin);
    // system("pause");
    return 0;
}

题目地址这个题目就是因为memset我错了四发,一直没看出,一直调没搞出来,最后才突然想起来
上面写的的是**#define met(x, y) memset(x, 0, y)
很明显少了4*,就是我改的时候忘记了,我的模板写的是
#define met(x,y) memset(x,y,sizeof x)**

布尔类型的字节数为1

所以就不用乘4了,如果同时有int和bool时更容易错。

不能用来全部归1

刚开始使用的话可能会用出memset(a,1,sizeof a)原本是想全部为1但实际上a[0]=16843009
因为16843009=1+256+65536+16777216= 2 0 + 2 8 + 2 16 + 2 24 2^0+2^8+2^{16}+2^{24} 20+28+216+224.
但是可以全部归-1.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值