头像

子沐


访客:3967

离线:2天前


活动打卡代码 AcWing 1055. 股票买卖 II

子沐
4个月前
#include <iostream>

using namespace std;
const int N = 1e5+10;
int a[N], n;

int main()
{
    cin >> n;
    for(int i = 0; i < n;i ++)
        cin >> a[i];
    int ans = 0;
    for(int i=0;i+1<n;i++)
    {
        if(a[i+1] > a[i]) ans += -a[i] + a[i+1];
    }
    cout << ans << endl;
    return 0;
}



活动打卡代码 AcWing 795. 前缀和

子沐
4个月前
#include <iostream>

using namespace std;
const int N = 1e5+10;
int a[N], n, m, sum[N];

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


    while(m--)
    {
        int l, r;
        cin >> l >> r;
        cout << sum[r] - sum[l-1] << endl;   
    }

    return 0;
}




子沐
4个月前
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 1005;
int f[N], n, a[N];

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


    int ans = -1;
    for(int i=0;i<n;i++){
        f[i] = 1;
        for(int j=0;j<i;j++)
            if(a[j]<a[i] && f[j]+1 > f[i])
                f[i] = f[j]+1;
        ans = max(ans, f[i]);
    }


    cout<<ans<<endl;

    return 0;
}



活动打卡代码 AcWing 1208. 翻硬币

子沐
4个月前
#include <iostream>
#include <cstring>
using namespace std;
const int N = 105;
char init[N], aim[N];

void turn(int x)
{
    if(init[x] == '*') init[x] = 'o';
    else init[x] = '*';

}

int main()
{
    cin >> init >> aim;
    int n = strlen(init);
    int cnt = 0;
    for(int i=0;i<n-1;i++)
    {
        if(init[i] != aim[i]) {
            turn(i);
            turn(i+1); 
            cnt ++;
        }
    }
    cout << cnt;


    return 0;
}




子沐
4个月前
#include <cstdio>
using namespace std;
const int N = 15;
int a[N], n, idx;
bool st[N];
void dfs(int u)
{
    if(u == n) {
        for(int i=0;i<idx;i++)
            printf("%d ", a[i]);
        puts("");
    }

    for(int i=1;i<=n;i++)
        if(!st[i])
        {
            st[i] = true;
            a[idx ++] = i;
            dfs(u+1);
            st[i] = false;
            a[idx -- ] = i;
        }
}

int main()
{
    scanf("%d", &n);
    dfs(0);
    return 0;
}



活动打卡代码 AcWing 717. 简单斐波那契

子沐
4个月前
#include <iostream>

using namespace std;

int main()
{
    int a = 0, b = 1;
    int n;
    cin >> n;
    for(int i=0;i<n;i++){
        int fn = a + b;
        cout << a << " ";
        a = b;
        b = fn;
    }
    return 0;
}




子沐
4个月前
#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    for(int i=0;i<1<<n;i++)
    {
        for(int k = 0;k<n;k++)
        {
            if(i >> k & 1) cout << k+1 << " ";
        }
        cout << endl;
    }

    return 0;
}



活动打卡代码 AcWing 95. 费解的开关

子沐
4个月前
#include <iostream>
#include <cstring>
using namespace std;
int n;
int a[5][5], backup[5][5];

void turn(int x, int y)
{
    int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
    a[x][y] = 1 - a[x][y];
    for(int i=0;i<4;i++)
    {
        int m = x + dx[i], n = y + dy[i];
        if(m < 0 || m >= 5 || n < 0 || n >=5) continue;
        a[m][n] = 1 - a[m][n];
    }

}

int main()
{
    cin >> n;
    while(n--)
    {
        int res = 10;
        for(int i=0;i<5;i++)
            for(int j=0;j<5;j++)
            {
                char x;
                cin >> x;
                if(x == '0') a[i][j] = 0;
                else a[i][j] = 1;
            }
        memcpy(backup, a, sizeof a);
        for(int i = 0; i <= 31; i ++)
        {
            int cnt = 0;
            for(int j = 0; j <=4; j++)
            {
                if(i >> j & 1){ 
                    turn(0, j);
                    cnt ++;
                }
            }
            for(int j = 1; j<5;j++)
                for(int k =0;k<5;k++)
                {
                    if(a[j-1][k] == 0){
                        turn(j, k);
                        cnt ++;
                    }
                }

            bool s = true;
            for(int t =0;t<5;t++)
                if(a[4][t] == 0){
                    s = false;
                    break;
                }

            if(s) res = min(res, cnt);
            memcpy(a, backup, sizeof backup);

        }

        if(res > 6) res = -1;

        cout << res << endl;


    }
    return 0;
}


活动打卡代码 AcWing 1209. 带分数

子沐
4个月前
#include <iostream>
#include <cstring>
using namespace std;
const int N = 20;
bool st[N], backup[N];

int n, ans = 0;
bool check(int a, int c)
{
    int b = n* c - a * c;
    if(!a || !b || !c) return false;
    memcpy(backup, st, sizeof st);
    while(b)
    {
        int x = b % 10;
        b /= 10;
        if(!x || backup[x]) return false;
        backup[x] = true;
    }
    for(int i=1;i<=9;i++)
        if(!backup[i]) return false;
    return true;
}
void dfs_c(int u, int a, int c)
{
    if(u == n) return;

    if(check(a, c)) ans ++;

    for(int i=1;i<=9;i++)
    {
        if(!st[i])
        {
            st[i] = true;
            dfs_c(u+1, a, c * 10 + i);
            st[i] = false;
        }
    }
}
void dfs_a(int u, int a)
{
    if(a >= n) return ;
    if(a) dfs_c(u, a, 0);

    for(int i = 1; i<= 9;i ++ )
        if(!st[i])
        {
            st[i] = true;
            dfs_a(u+1, a * 10 + i);
            st[i] = false;
        }
}
int main()
{
    cin >> n;

    dfs_a(0, 0);

    cout << ans << endl;
    return 0;
}



子沐
4个月前
#include <iostream>
#include <vector>
using namespace std;
int n, m;
const int N = 30;
vector<int> path;

void dfs(int u, int s)
{
    if(u == m+1) {
        for(auto x : path)
            cout << x << " ";
        cout << endl;
    }

    for(int i = s; i <= n; i++ )
    {
        path.push_back(i);
        dfs(u+1, i+1);
        path.pop_back();

    }
}

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

    dfs(1, 1);

    return 0;

}