头像

徐州煤炭工人




离线:2小时前


分享 背包合集

01背包

#include<iostream>
using namespace std;

const int N = 1005;
int w[N],v[N],f[N];
int main(){
    int n,m;
    cin>>n>>m;
    for(int i = 1; i <= n; ++i){ 
        int v,w;
        cin>>v>>w;
        for(int j = m; j >= v; j--){
        f[j]=max(f[j],f[j-v]+w);
        }
    }    
    cout<<f[m];
    return 0;    
}

完全背包

#include<iostream>
using namespace std;

const int N = 1005;
int w[N],v[N],dp[N];
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        int v, w;
        cin >> v >> w;
        for(int j=v;j<=m;j++){
            dp[j]=max(dp[j],dp[j-v]+w);    
        }
    }
    cout<<dp[m];
    return 0;    
}

多重背包1

#include<iostream>
using namespace std;

const int N = 1010;

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

int main(){
    int v,w,s;
    cin >> n >> m;
    for(int i = 1; i <= n; i ++ ){

        cin >> v >> w>>s;
        for(int i=1;i<=s;i++){
        for(int j = m; j >= v; j -- ){
                dp[j] = max(dp[j], dp[j - v] + w);
            }
        }
    }
    cout << dp[m] << endl;
}


分享 快读

inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}


活动打卡代码 AcWing 831. KMP字符串

#include <iostream>

using namespace std;

const int N = 100010, M = 1000010;

int n, m;
int ne[N];
char s[M], p[N];

int main()
{
    cin >> n >> p + 1 >> m >> s + 1;

    for (int i = 2, j = 0; i <= n; i ++ )
    {
        while (j && p[i] != p[j + 1]) j = ne[j];
        if (p[i] == p[j + 1]) j ++ ;
        ne[i] = j;
    }

    for (int i = 1, j = 0; i <= m; i ++ )
    {
        while (j && s[i] != p[j + 1]) j = ne[j];
        if (s[i] == p[j + 1]) j ++ ;
        if (j == n)
        {
            printf("%d ", i - n);
            j = ne[j];
        }
    }

    return 0;
}





string replaceSpaces(string &str) {
        size_t t;
        while((t=str.find(' '))!=string::npos){
            str.replace(t,1,"%20");
        }
        return str;
    }
string replaceSpaces(string &str) {
        string res;
        for (auto x : str)
            if (x == ' ')
                res += "%20";
            else
                res += x;
        return res;
    }



#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 <bits/stdc++.h>
const int N=1e6+10;
int a[N],b[N];
int main()
{
    int n,m,x;
    scanf("%d%d%d",&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(b[j]+a[i]==x)
            printf("%d %d\n",i,j);
    }
    return 0;
}




for (int i = 0, j = 0; i < n; i ++ )
{
    while (j < i && check(i, j)) j ++ ;



}
(1) 一个序列,维护一段区间
(2) 两个序列,维护某种次序


活动打卡代码 AcWing 798. 差分矩阵

#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 1e3 + 40;
int a[maxn][maxn], b[maxn][maxn];

inline void insert(int x1, int y1, int x2, int y2, int c) {
    b[x1][y1] += c;
    b[x2 + 1][y1] -= c;
    b[x1][y2 + 1] -= c;
    b[x2 + 1][y2 + 1] += c;
}

int main(void) {
    int n, m, q;
    scanf("%d%d%d", &n, &m, &q);
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            scanf("%d", &a[i][j]);

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            insert(i, j, i, j, a[i][j]);

    for(int i = 1; i <= q; i++) {
        int x1, y1, x2, y2, c;
        scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &c);
        insert(x1, y1, x2, y2, c);
    }

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            b[i][j] += b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            if(j == m) printf("%d\n", b[i][j]);
            else printf("%d ", b[i][j]);




    return 0;
}



活动打卡代码 AcWing 797. 差分

#include<iostream>
#include<vector>
using namespace std;
const int N=1e5+10;
int a[N],b[N];
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) b[i]=a[i]-a[i-1];
     while(m--){
        int l,r,c;
        scanf("%d%d%d",&l,&r,&c);
        b[l]+=c;
        b[r+1]-=c;
    }
    for(int i=1;i<=n;i++){
        b[i]=b[i-1]+b[i];//将差分改为原数组
        cout<<b[i]<<' ';
    }
    return 0;
}



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

#include<iostream>
#include<iomanip>
using namespace std;
double n,l,r,mid;
bool flag;
double q(double a){return a*a*a;}
int main(){
    cin>>n;
    l=-100,r=100;
    while(r-l>=1e-7){
        mid=(l+r)/2;
        if(q(mid)>=n) r=mid;
        else l=mid;
    }
    cout<<fixed<<setprecision(6)<<l;
    return 0;
}

牛顿迭代法

#include<iostream>
using namespace std;

double cubic(double num){
    double t = num;
    for (int i = 0; i < 100; i++) 
        t = (2 * t * t * t + num) / (3 * t * t);

    return t;
}

int main()
{
    double n; scanf("%lf",&n);

    printf("%.6lf", cubic(n));

    return 0;
}