头像

Sichhingeben




离线:14小时前


最近来访(11)
用户头像
不高兴的兽奶
用户头像
ACdefly
用户头像
jiangbenshuo
用户头像
星星藏于星野
用户头像
李.嘉图
用户头像
aoczs
用户头像
菜菜-胶胶
用户头像
nightwish
用户头像
_叽嘻嘻_

活动打卡代码 AcWing 2816. 判断子序列

#include<iostream>
#include<cstring>

using namespace std;

const int N = 1e5 + 10;

int n,m;
int a[N],b[N];

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0;i < n;i ++) scanf("%d", &a[i]);
    for (int i = 0;i < m;i ++) scanf("%d", &b[i]);

    int i = 0,j = 0;
    while (i < n && j < m)
    {
        if (a[i] == b[j]) i ++;
        j ++;
    }

    if (i == n) puts("Yes");
    else puts("No");

    return 0;
}



题目链接 算法基础课 判断子序列2816

我遇到了Wrong Answer问题。

错误的代码:

#include<iostream>
#include<cstring>

using namespace std;

const int N = 1e5 + 10;

int n,m;
int a[N],b[N];

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0;i < n;i ++) scanf("%d", &a[i]);
    for (int i = 0;i < m;i ++) scanf("%d", &b[i]);

    int i = 0,j = 0;
    while (i < n && j < m)
    {
        if (a[i] = b[j]) i ++;
        j ++;
    }

    if (i == n) puts("Yes");
    else puts("No");

    return 0;
}

编译器报了Wrong Answer问题,代码就是跟着写的Y总的代码,调试可以,但是没法儿提交,检查代码也没啥毛病




#include<iostream>
#include<algorithm>

using namespace std;

const int N = 1e5 + 10;

int n,m,x;
int a[N],b[N];

int main()
{
    cin >> n >> m >> x;

    for (int i = 0;i < n;i ++) scanf("%d", &a[i]);
    for (int i = 0;i < m;i ++) scanf("%d", &b[i]);

    for (int i = 0,j = m - 1;i < n;i ++)
    {
        while (j >= 0 && a[i] + b[j] > x) j--;
        if (a[i] + b[j] == x) 
        {
            printf("%d %d\n", i, j);
            break;
        }
    }

    return 0;
}


活动打卡代码 AcWing 802. 区间和

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

typedef pair<int,int> PII;

const int N = 300010;

int n,m;
int a[N],s[N];

vector<int> alls;
vector<PII> add, query;

int find(int x) //离散 
{
    int l = 0,r = alls.size() - 1;
    while (l < r)
    {
        int mid = l + r >> 1;
        if (alls[mid] >= x) r = mid;
        else l = mid + 1;
    }

    return r + 1;

}

int main()
{
    cin >> n >> m;
    for (int i = 0;i < n;i ++)
    {
        int x,c;
        cin >> x >> c;
        add.push_back({x,c});

        alls.push_back(x);
    }

    for (int i = 0;i < m;i ++)
    {
        int l,r;
        cin >> l >> r;
        query.push_back({l,r});

        alls.push_back(l);
        alls.push_back(r);

    }

    //去重
    sort(alls.begin(),alls.end()); 
    alls.erase(unique(alls.begin(),alls.end()),alls.end());

    //处理插入 
    for (auto item : add)
    {
        int x = find(item.first);
        a[x] += item.second;
    }

    //预处理
    for (int i = 1;i <= alls.size();i ++) s[i] = s[i - 1] + a[i];

    //预处理前缀和
    for (auto item : query) 
    {
        int l = find(item.first),r = find(item.second);
        cout << s[r] - s[l - 1] << endl;
    }

    return 0;
}


活动打卡代码 AcWing 803. 区间合并

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

typedef pair<int,int> PII;

const int N = 100010;

int n;
vector<PII> segs;

void merge(vector<PII> &segs)
{
    vector<PII> res;

    sort(segs.begin(),segs.end());

    int st = -2e9,ed = -2e9;
    for (auto seg : segs)
        if (ed < seg.first)
        {
            if (st != -2e9) res.push_back({st,ed});
            st = seg.first, ed = seg.second;
        }
        else ed = max(ed,seg.second);

    if (st != -2e9) res.push_back({st,ed});

    segs = res;
}

int main()
{
    int n;
    cin >> n;

    for (int i = 0;i <= n;i ++)
    {
        int l,r;
        cin >> l >> r;
        segs.push_back({l,r});
    }

    merge(segs);

    cout << segs.size() << endl;

    return 0;

}



活动打卡代码 AcWing 4268. 性感素数

#include<iostream>

using namespace std;

bool is_prime(int x)
{
    if (x < 2) return false;
    for (int i = 2;i <= x / i;i ++)
        if (x % i == 0)
            return false;
    return true;
}

int main()
{
    int n;
    cin >> n;

    for (int i = n - 6;i <= n + 6;i += 12)
        if (is_prime(i) && is_prime(n))
        {
            cout << "Yes" << endl;
            cout << i << endl;
            return 0;
        }

    for (int i = n - 1;;i ++)
        if (is_prime(i) && is_prime(i - 6) || is_prime(i + 6))
        {
            cout << "No" << endl;
            cout << i << endl;
            return 0;
        }

    return 0;
}



#include<iostream>

using namespace std;

int lowbit(int x)
{
    return x & -x;
}

int main()
{
    int n;
    cin >> n;
    while (n --)
    {
        int x;
        cin >> x;

        int res = 0;
        while (x) x -= lowbit(x),res ++;

        cout << res << ' ';
    }

    return 0;

}



#include<iostream>

using namespace std;

const int N = 1e5 + 10;

int n;
int s[N],a[N];

int main()
{
    cin >> n;
    for (int i = 0;i < n;i ++) cin >> a[i];

    int res = 0;
    for (int i = 0,j = 0;i < n;i ++)
    {
        s[a[i]] ++;
        while (s[a[i]] > 1)
        {
            s[a[j]] --;
            j ++;
        }

        res = max(res,i - j + 1);
    }

    cout << res << endl;

    return 0;
}



活动打卡代码 AcWing 794. 高精度除法

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

vector<int> div(vector<int> &A,int b,int &r)
{
    vector<int> C;
    for (int i = A.size() - 1;i >= 0;i --)
    {
        r = r * 10 + A[i];
        C.push_back(r / b);
        r %= b;
    }

    reverse(C.begin(),C.end());
    while (C.size() > 1 && C.back() == 0) C.pop_back(); //最高位为0时,则去掉前导0

    return C;
}

int main()
{
    string a;
    int b;
    cin >> a >> b;

    vector<int> A;
    for (int i = a.size() - 1;i >= 0;i --) A.push_back(a[i] - '0');

    int r;
    auto C = div(A,b,r);

    for (int i = C.size() - 1;i >= 0;i --) printf("%d",C[i]);
    cout << endl << r << endl;

    return 0;
}


活动打卡代码 AcWing 793. 高精度乘法

#include<iostream>
#include<vector>

using namespace std;

vector<int> mul(vector<int> &A,int b)
{
    vector<int> C;

    int t = 0;

    for (int i = 0;i < A.size() || t;i ++) //循环条件是i 没有循环完,要么是t 不为0
    {
        if (i < A.size()) t += A[i] * b;
        C.push_back(t % 10);
        t /= 10;
    }

    return C;

}

int main()
{
    string a;
    int b;
    cin >> a >> b;

    vector<int> A;
    for (int i = a.size() - 1;i >= 0;i --) A.push_back(a[i] - '0');

    auto C = mul(A,b);

    for (int i = C.size() - 1;i >= 0;i --) printf("%d",C[i]);

    return 0;
}