头像

hhhcj




离线:4小时前


最近来访(3)
用户头像
蔚蓝_6
用户头像
dgxdn1
用户头像
ZhengQC

活动打卡代码 AcWing 3416. 时间显示

hhhcj
4天前
#include<iostream>
#include<cstring>
using namespace std;
typedef unsigned long long ULL;
int main()
{
    ULL t;
    cin >> t;
    int hour, min, sec;
    hour = t / 1000 / 3600 % 24;
    min = t / 1000 / 60 % 60;
    sec = t / 1000 % 60;
    printf("%02d:%02d:%02d", hour, min, sec);
    return 0;
}


活动打卡代码 AcWing 826. 单链表

hhhcj
7天前
#include<iostream>
#include<cstring>
using namespace std;
const int N = 100010;
int h, e[N], ne[N], idx;
void addToHead(int x)
{
    e[idx] = x, ne[idx] = h, h = idx ++;
}
void delK(int k)
{
    ne[k] = ne[ne[k]];
}
void removeHead()
{
    h = ne[h];
}
void addK(int k, int x)
{
    e[idx] = x, ne[idx] = ne[k], ne[k] = idx ++;
}
int main()
{
    int m;
    cin >> m;
    h = -1;
    char ch;
    while(m--)
    {
        cin >> ch;
        if(ch == 'H')
        {
            int x;
            cin >> x;
            addToHead(x);
        }
        else if (ch == 'D')
        {
            int k;
            cin >> k;
            if(k == 0)removeHead();
            else delK(k - 1);
        }
        else
        {
            int k, x;
            cin >> k >> x;
            addK(k - 1, x);
        }
    }
    for(int i = h; i != -1; i = ne[i])
        cout << e[i] << " ";
    return 0;
}


活动打卡代码 AcWing 1233. 全球变暖

hhhcj
8天前
#include<iostream>
#include<cstring>
using namespace std;
const int N =1010;
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
char g[N][N];
int cnt, n;
bool flag, st[N][N];
void dfs(int x, int y, int &total, int &bound)
{
    total++;
    st[x][y] = true;
    bool boundFlag = false;
    for(int i = 0; i < 4; i++)
    {
        int xx = x + dx[i], yy = y + dy[i];
        if(xx >= 0 && xx < n && yy >= 0 && yy < n && g[xx][yy] == '.')
        {
            boundFlag = true;
        }
        if(xx >= 0 && xx < n && yy >= 0 && yy < n && g[xx][yy] == '#' && !st[xx][yy])
        {
            flag = true;
            dfs(xx, yy, total, bound);
        }
    }
    if(boundFlag)bound++;
    return;
}
int main()
{
    cin >> n;
    for(int i = 0; i < n; i++)cin >> g[i];
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            if(!st[i][j] && g[i][j] == '#')
            {
                int total = 0, bound = 0;
                dfs(i, j, total, bound);
                if(total == bound)cnt++;
            }
    cout << cnt << endl;
    return 0;
}


活动打卡代码 AcWing 1096. 地牢大师

hhhcj
8天前
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 110;
const int dx[] = {-1, 0, 1, 0, 0, 0}, dy[] = {0, 1, 0, -1, 0, 0}, dz[] = {0, 0, 0, 0, -1, 1};
struct node
{
    int x, y, z;
    bool operator == (const node &n1)const
    {
        return x == n1.x && y == n1.y && z == n1.z;
    }
};
char g[N][N][N];
node s, e;
int d[N][N][N], l, r, c;
int bfs()
{
    queue<node> q;
    q.push(s);
    d[s.z][s.x][s.y] = 0;
    while(!q.empty())
    {
        node t = q.front();
        q.pop();
        if(t == e)return d[t.z][t.x][t.y];
        for(int i = 0 ; i < 6; i++)
        {
            int x = t.x + dx[i], y = t.y + dy[i], z = t.z + dz[i];
            if(x >= 0 && x < r && y >= 0 && y < c && z >=0 && z < l && g[z][x][y] != '#' && d[z][x][y] == -1)
            {
                d[z][x][y] = d[t.z][t.x][t.y] + 1;
                //cout << x <<" "<<y <<" "<<z <<" "<<endl;
                q.push({x, y, z});
            }
        }
    }
    return -1;
}

int main()
{
    while(scanf("%d%d%d", &l, &r, &c) && l && r && c)
    {
        memset(d, -1, sizeof d);
        for(int k = 0; k < l; k++)
            for(int i = 0; i < r; i++)
                scanf("%s", g[k][i]);
        for(int k = 0; k < l; k++)
            for(int i = 0 ; i < r; i++)
                for(int j = 0; j < c; j++)
                    if(g[k][i][j] == 'S')s = {i, j, k};
                    else if(g[k][i][j] == 'E')e = {i, j, k};
        int ans = bfs();
        if(ans != -1)printf("Escaped in %d minute(s).\n", ans);
        else puts("Trapped!");
    }
    return 0;
}



hhhcj
8天前
#include<iostream>
#include<cmath>
using namespace std;
const int N = 100010;
int a[N], n, maxd;
int main()
{
    cin >> n;
    long long maxSum = -999999999;
    for(int i = 1; i <= n; i++)cin >> a[i];
    for(int d = 0; d < log(n) * 1.0 / log(2); d++)
    {
        long long sum = 0;
        for(int j = pow(2, d); j <= n && j < pow(2, d+1); j++)
            sum += a[j];
        if(sum > maxSum)maxd = d, maxSum = sum;
    }
    cout << maxd + 1 << endl;
    return 0;
}


活动打卡代码 AcWing 1224. 交换瓶子

hhhcj
8天前
#include<iostream>
using namespace std;
const int N = 10010;
int b[N], n;
bool st[N];
int main()
{
    int cnt = 0;
    cin >> n;
    for(int i = 1; i <= n; i++)cin >> b[i];
    for(int i = 1; i <= n; i++)
        if(!st[i])
        {
            cnt++;
            for(int j = i; !st[j]; j = b[j])
                st[j] = true;
        }
    cout << n - cnt << endl;
}


活动打卡代码 AcWing 1224. 交换瓶子

hhhcj
8天前
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 10010;
int a[N], n;
int ans = 0;
int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    for(int i = 1; i <= n; i++)
        if(a[i] != i)
        {
            for(int j = i + 1; j <= n; j++)
            {
                if(a[j] == i)
                    swap(a[i], a[j]);
            }
            ans++;
        }
    cout << ans << endl;
    return 0;
}
#include<iostream>
using namespace std;
const int N = 10010;
int b[N], n;
bool st[N];
int main()
{
    int cnt = 0;
    cin >> n;
    for(int i = 1; i <= n; i++)cin >> b[i];
    for(int i = 1; i <= n; i++)
        if(!st[i])
        {
            cnt++;
            for(int j = i; !st[j]; j = b[j])
                st[j] = true;
        }
    cout << n - cnt << endl;
}


活动打卡代码 AcWing 1113. 红与黑

hhhcj
8天前
#include<iostream>
#include<cstring>
using namespace std;
const int N = 30;
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
char g[N][N];
int cnt, w, h;
void dfs(int x, int y)
{
    for(int i = 0; i < 4; i++)
    {
        int xx = x + dx[i], yy = y + dy[i];
        if(xx >= 0 && xx < h && yy >= 0 && yy < w && g[xx][yy] == '.')
        {
            cnt++;
            g[xx][yy] = '#';
            dfs(xx, yy);
        }
    }
    return;
}

int main()
{
    while(scanf("%d%d", &w, &h) && w && h)
    {
        int sx, sy;
        cnt = 0;
        for(int i = 0 ; i < h; i++)
            scanf("%s", g[i]);
        for(int i = 0 ; i < h; i++)
            for(int j = 0; j < w; j++)
                if(g[i][j] == '@')sx = i, sy = j;
        dfs(sx, sy);
        printf("%d\n", cnt + 1);
    }
    return 0;
}



hhhcj
8天前
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 210;
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
typedef pair<int, int> PII;
char g[N][N];
PII s, e;
int d[N][N], r, c;
int bfs()
{
    queue<PII> q;
    q.push(s);
    d[s.first][s.second] = 0;
    while(!q.empty())
    {
        PII t = q.front();
        q.pop();
        if(t == e)return d[t.first][t.second];
        for(int i = 0 ; i < 4; i++)
        {
            int x = t.first + dx[i], y = t.second + dy[i];
            if(x >= 0 && x < r && y >= 0 && y < c && g[x][y] != '#' && d[x][y] == -1)
            {
                d[x][y] = d[t.first][t.second] + 1;
                q.push({x, y});
            }
        }
    }
    return -1;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        memset(d, -1, sizeof d);
        scanf("%d%d", &r, &c);
        for(int i = 0 ; i < r; i++)
            scanf("%s", g[i]);
        for(int i = 0 ; i < r; i++)
            for(int j = 0; j < c; j++)
                if(g[i][j] == 'S')s = PII(i, j);
                else if(g[i][j] == 'E')e = PII(i, j);

        int ans = bfs();
        if(ans != -1)printf("%d\n", ans);
        else puts("oop!");
    }
    return 0;
}


活动打卡代码 AcWing 1238. 日志统计

hhhcj
9天前
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 100010;
typedef pair<int, int> PII;
PII a[N];
int cnt[N], q[N], maxid = -1;
bool flag[N];
int main()
{
    int n, d, k;
    scanf("%d%d%d", &n, &d, &k);
    for (int i = 0; i < n; i++)
    {
        scanf("%d%d", &a[i].first, &a[i].second);
        maxid = max(maxid, a[i].second);
    }
    sort(a, a + n);
    int hh = 0, tt = -1;
    for (int i = 0; i < n; i++)
    {
        while (hh <= tt && a[i].first >= a[q[hh]].first + d)
        {
            cnt[a[q[hh]].second]--;
            hh++;
        }
        cnt[a[i].second]++;
        if (cnt[a[i].second] >= k && !flag[a[i].second])
            flag[a[i].second] = 1;
        q[++tt] = i;
    }
    for(int i = 1; i <= maxid; i++)
        if(flag[i])printf("%d\n", i);
    return 0;
}