头像

Anlans


访客:1153

离线:15小时前


活动打卡代码 AcWing 791. 高精度加法

Anlans
1个月前
#include<iostream>
#include<vector>
using namespace std ;
const int N=1e5+10;
vector<int> add(vector<int>&a,vector<int>&b)
{
    if(a.size()<b.size())return add(b,a);
    vector<int>c;
    int t=0;
    for(int i=0;i<a.size();i++)
    {
        t+=a[i];
        if(i<b.size())t+=b[i];
        c.push_back(t%10);
        t=t/10;

    }
    if(t)c.push_back(t);
    return c;
    }
int main()
{
    string a,b;
    cin>>a>>b;
    vector<int>A,B;
    for(int i=a.size()-1;i>=0;i--)A.push_back(a[i]-'0');
    for(int i=b.size()-1;i>=0;i--)B.push_back(b[i]-'0');
    auto C=add(A,B);
    for(int i=C.size()-1;i>=0;i--)cout<<C[i];
    cout<<endl;
    return 0;
}  


活动打卡代码 AcWing 790. 数的三次方根

Anlans
1个月前
#include<iostream>
using namespace std ;
int main()
{
    double n;
    scanf("%lf",&n);
    double l=-1e4,r=1e4;
    while(r-l>1e-8)
    {
        double mid=(l+r)/2;
        if(mid*mid*mid>=n)r=mid;
       else  l=mid;
    }
    printf("%lf",l);
    return 0;
}


活动打卡代码 AcWing 789. 数的范围

Anlans
1个月前
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,q,x,a[N];
int main()
{
    scanf("%d%d",&n,&q);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    while(q--)
    {
        scanf("%d",&x);
        int l=0,r=n-1;
        while(l<r)
        {
            int mid=l+r>>1;
        if(a[mid]<x)l=mid+1;
        else r=mid;
        }

    if(a[l]!=x)
    {
        printf("-1 -1\n");
        continue;
    }
    int l1=l,r1=n;
    while(l1+1<r1)
    {
        int mid=l1+r1>>1;
        if(a[mid]<=x)l1=mid;
        else r1=mid;
    }
    printf("%d %d\n",l,l1);
    }
    return 0;
}


活动打卡代码 AcWing 788. 逆序对的数量

Anlans
1个月前
#include<iostream>

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;
int n;
int q[N], tmp[N];

LL merge_sort(int l, int r){
    if(l >= r) return 0;

    int mid = l + r >> 1;

    LL res = merge_sort(l, mid) + merge_sort(mid + 1, r);

    int i = l, j = mid + 1, k = 0;
    while(i <= mid && j <= r){
        if(q[i] <= q[j]) tmp[k++] = q[i++];
        else{
            res += mid - i + 1;
            tmp[k++] = q[j++];
        }
    }
    while(i <= mid) tmp[k++] = q[i++];
    while(j <= r) tmp[k++] = q[j++];

    for(int i = l, j = 0; i <= r; i++, j++) q[i] = tmp[j];

    return res;
}

int main(){
    cin >> n;

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

    cout << merge_sort(0, n - 1) << endl;

    return 0;
}



活动打卡代码 AcWing 787. 归并排序

Anlans
1个月前
#include<iostream>
#include<cstdio>
using namespace std ;
const int N=1e5+10;
int a[N],temp[N];
void merge_sort(int q[],int l,int r)
{
    if(l>=r)return ;
    int mid=l+r>>1;
    merge_sort(q,l,mid),merge_sort(q,mid+1,r);
    int k=0,i=l,j=mid+1;
    while(i<=mid&&j<=r)
    if(q[i]<q[j])temp[k++]=q[i++];
    else temp[k++]=q[j++];
    while(i<=mid)temp[k++]=q[i++];
    while(j<=r)temp[k++]=q[j++];
    for(int i=l,j=0;i<=r;i++,j++)q[i]=temp[j];
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    merge_sort(a,0,n-1);
    for(int i=0;i<n;i++)
    {
        printf("%d ",a[i]);
    }
    return 0;
}



活动打卡代码 AcWing 786. 第k个数

Anlans
1个月前
#include<bits/stdc++.h>
using namespace std ;
const int N=1e5+10;
int a[N],n,m,i;
int main()
{
    cin>>n>>m;
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    sort(a+1,a+1+n);
    cout<<a[m]<<endl;
    return 0;
}


活动打卡代码 AcWing 785. 快速排序

Anlans
1个月前
#include<bits/stdc++.h>//STL
using namespace std;
const int N=1e7+10;
int a[N],n,m,i,j;
int main()
{
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)
{
cout<<a[i]<<" ";
}
return 0;
}
/*
#include <iostream>
using namespace std;
const int N = 1000010;
int q[N];
void quick_sort(int q[], int l, int r)
{
    if (l >= r) return;
    int i = l - 1, j = r + 1, x = q[l + r >> 1];
    while (i < j)
    {
        do i ++ ; while (q[i] < x);
        do j -- ; while (q[j] > x);
        if (i < j) swap(q[i], q[j]);
    }
    quick_sort(q, l, j);
    quick_sort(q, j + 1, r);
}

int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i ++ ) scanf("%d", &q[i]);
    quick_sort(q, 0, n - 1);
    for (int i = 0; i < n; i ++ ) printf("%d ", q[i]);
    return 0;
}
*/



Anlans
9个月前

哇上线新功能啦,好棒



活动打卡代码 AcWing 2. 01背包问题

Anlans
9个月前
/*
滚动数组解法
*/
#include<iostream>
using namespace std;

const int N = 1010;

int n, m;
int v[N], w[N];
int f[N];

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

    for(int i=1; i<=n; i++) cin>>v[i]>>w[i];

    //f[0][0~m] = 0; 考虑前0个物品都是0,初始化省略

    for(int i=1; i<=n; i++)
        for(int j=m; j>=v[i]; j--)
            f[j] = max( f[j], f[j-v[i]] + w[i] );

    cout<<f[m]<<endl;
}



Anlans
9个月前
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

const int N = 100010, M = 200010, INF = 0x3f3f3f3f;

int n, m;
int p[N];

struct Edge
{
    int a, b, w;

    bool operator< (const Edge &W)const//重载比较符合表示以权值大小排序
    {
        return w < W.w;
    }
}edges[M];

int find(int x)
{
    if(p[x] != x) p[x] = find(p[x]);
    return p[x];
}

int kruskal()
{
    sort(edges, edges+m);
    for(int i=1; i<=n; i++) p[i] = i;

    int res = 0, cnt = 0;
    for(int i=0; i<m; i++)
    {
        int a = edges[i].a, b = edges[i].b, w = edges[i].w;

        a=find(a), b=find(b);//让其等于各自祖宗结点
        if(a!=b)//判断两者是否连通,若不连通则
        {
            p[a] = b;//两个集合合并
            res += w;//res加的是,最小生成树边的权重之和
            cnt++;//当前加入多少边
        }
    }

    if(cnt < n-1) return INF;//判断一共加了多少条边,若是cnt小于n-1则说明不连通
    return res;
}

int main()
{
    scanf("%d%d", &n, &m);

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

    int t = kruskal();

    if(t == INF) puts("impossible");
    else printf("%d\n", t);

}