头像

青柠

霍格沃兹




离线:24天前


最近来访(9)
用户头像
余火
用户头像
SELFIE1
用户头像
watasky_r
用户头像
moreexcellent
用户头像
姚军
用户头像
skydegree
用户头像
PDX
用户头像
算法小菜狗
用户头像
zombotany

活动打卡代码 AcWing 3197. 相反数

青柠
9个月前
#include <bits/stdc++.h>

using namespace std;

map<int,int> mp;

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


活动打卡代码 AcWing 3208. Z字形扫描

青柠
9个月前
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

const int N = 600;

int n;
int a[N][N];

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

    int x = 1 , y = 1;
    bool f = true;
    while(x != n+1 || y != n+1 )
    {
        if(x <= n && y <= n)
        {
            cout << a[x][y] << ' ';
        }

        if(f) x -- ,y ++ ;
        else x ++ ,y -- ;

        if(y <= 0){
            y = 1;
            f = !f;
        }
        if(x <= 0){
            x = 1;
            f = !f;
        }
    }
    return 0;
}


活动打卡代码 AcWing 3207. 门禁系统

青柠
9个月前
#include <iostream>

using namespace std;

const int N = 1010;

int a[N];

int main()
{
    int n;
    cin >> n;
    for (int i = 0 ; i < n ; i ++ ){
        int x;
        cin >> x;
        a[x] ++ ;
        cout << a[x] << ' ';
    }
    cout << endl;

    return 0;
}


活动打卡代码 AcWing 3205. 最优配餐

青柠
9个月前
#include <bits/stdc++.h>
#define ll long long
#define x first
#define y second
#define IO ios::sync_with_stdio(false)

using namespace std;

typedef pair<int,int> pii;

const int N = 1010,INF = 0x3f3f3f3f;

map<pii,int> mp;
int n,m,k,d;
char g[N][N];
int dist[N][N];
int cus[N*N][3];

queue<pii> q;

int dx[]={0,0,1,-1},dy[]={1,-1,0,0};

void bfs()
{
    while(q.size())
    {
        auto t = q.front();
        q.pop();

        for (int i = 0 ; i < 4  ; i ++ )
        {
            int a = t.x + dx[i],b = t.y + dy[i];
            if(g[a][b] == '#' || a <= 0 || a > n || b <= 0 || b > n) continue;
            if(dist[a][b] <= dist[t.x][t.y] + 1) continue;
            dist[a][b] = dist[t.x][t.y] + 1;
            q.push({a,b});
        }
    }
}

int main()
{
    IO;

    memset(dist,0x3f,sizeof dist);

    cin >> n >> m >> k >> d;

    for (int i = 1 ; i <= n ; i ++ )
        for (int j = 1 ; j <= n ; j ++ )
            g[i][j] = '.';

    while(m -- )
    {
        int x,y,z;
        cin >> x >> y;
        dist[x][y] = 0;
        q.push({x,y});
    }
    for (int i = 0 ; i < k ; i ++ )
    {
        int x,y,z;
        cin >> x >> y >> z;
        cus[i][0] = x;
        cus[i][1] = y;
        cus[i][2] = z;
    }
    while(d -- )
    {
        int x,y;
        cin >> x >> y;
        g[x][y] = '#';
    }

    bfs();

    ll res = 0;
    for (int i = 0; i < k ; i ++ )
    {
        int x = cus[i][0],y=cus[i][1];
        res += 1ll * dist[x][y] * cus[i][2];  
    }
    cout << res << endl;
    return 0;
}


活动打卡代码 AcWing 3204. 字符串匹配

青柠
9个月前
#include <bits/stdc++.h>

using namespace std;

string change(string str,int ty)
{
    if(ty) return str;
    else {
        transform(str.begin(),str.end(),str.begin(),::tolower);
        return str;
    }
}

int main()
{
    string s;
    cin >> s;
    int ty;
    cin >> ty;
    s = change(s,ty);
    int n;
    cin >> n;
    while(n -- )
    {
        string ss;
        cin >> ss;
        string sss;
        sss = change(ss,ty);
        if(sss.find(s) != string::npos)
        {
            cout << ss << endl;
        }
    }
    return 0;
}


活动打卡代码 AcWing 3203. 画图

青柠
9个月前
#include <bits/stdc++.h>

using namespace std;

const int N = 110;

int c[N][N];

int main()
{
    int n;
    cin >> n;
    while(n -- )
    {
        int x1,y1,x2,y2;
        cin >> x1 >> y1 >> x2 >> y2;
        for (int i = x1 ; i < x2 ; i ++ )
        {
            for (int j = y1 ; j < y2 ; j ++ )
            {
                c[i][j] ++ ;
            }
        }
    }
    int ans = 0;
    for (int i = 0 ; i < N ; i ++ )
    {
        for (int j = 0 ; j < N ; j ++ )
        {
            if(c[i][j]) ans ++ ;
        }
    }
    cout << ans << endl;
    return 0;
}


活动打卡代码 AcWing 3202. 相邻数对

青柠
9个月前
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 10010;

int a[N];

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


活动打卡代码 AcWing 3196. I’m stuck!

青柠
9个月前
#include <iostream>

using namespace std;

const int N = 100;

int r,c;
char g[N][N];
bool st1[N][N],st2[N][N];

int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};

bool check(int x,int y,int k)
{
    char c = g[x][y];
    if(c == 'S' || c == '+' || c == 'T') return true;
    if(c == '|' && (k == 0 || k == 2)) return true;
    if(c == '-' && (k == 1 || k == 3)) return true;
    if(c == '.' && k == 2) return true;
    return false;
}

void dfs1(int x,int y)
{
    st1[x][y] = true;
    for (int i = 0 ; i < 4 ; i ++ )
    {
        int a = x + dx[i],b = y + dy[i];
        if(a <= 0 || a > r || b <= 0 || b > c || g[a][b] == '#' || st1[a][b]) continue;
        if(check(x,y,i)) dfs1(a,b);
    }
}

void dfs2(int x,int y)
{
    st2[x][y] = true;
    for (int i = 0 ; i < 4 ; i ++ )
    {
        int a = x + dx[i],b = y + dy[i];
        if(a <= 0 || a > r || b <= 0 || b > c || g[a][b] == '#' || st2[a][b]) continue;
        if(check(a,b,i^2)) dfs2(a,b);
    }
}

int main()
{
    cin >> r >> c;
    for (int i = 1 ; i <= r ; i ++ )
        for (int j = 1 ; j <= c ; j ++ )
            cin >> g[i][j];

    int x,y;        
    for (int i = 1; i <= r ; i ++ )
        for (int j = 1 ; j <= c ; j ++ )
        {
            if(g[i][j] == 'S') dfs1(i,j);
            if(g[i][j] == 'T') dfs2(i,j),x=i,y=j;
        }

    int res = 0 ;
    for (int i = 1 ; i <= r ; i ++ )
        for (int j = 1 ; j <= c ; j ++ )
            if(st1[i][j] && !st2[i][j]) res ++ ;

    if(!st1[x][y]) puts("I'm stuck!");    
    else cout << res << endl;

    return 0;
}


活动打卡代码 AcWing 3195. 有趣的数

青柠
9个月前
#include <iostream>

using namespace std;

typedef long long ll;

const int N = 1010,MOD = 1e9+7;

int n;
int c[N][N];

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

    for (int i = 0 ; i <= n ; i ++ )
        for (int j = 0 ; j <= i ; j ++ )
            if(!j) c[i][j] = 1;
            else c[i][j] = (c[i-1][j] + c[i-1][j-1]) % MOD;

    int res = 0;
    for (int k = 2 ; k <= n - 2 ; k ++ )
    {
        res = (res + (ll)c[n-1][k] * (k-1) * (n - k - 1) % MOD)%MOD;
    }

    printf("%d\n",res);

    return 0;
}


活动打卡代码 AcWing 3194. 最大的矩形

青柠
9个月前
#include <iostream>

using namespace std;

const int N = 1010;

int h[N];

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

    for (int i = 0 ; i < n ; i ++ )
    {
        int mi = h[i];
        for (int j = i; j >= 0 ; j -- )
        {
            mi = min(mi,h[j]);
            res = max(res,mi*(i-j+1));
        }
    }
    cout << res << endl;
    return 0;
}