头像

爱吃奶糖_




离线:15天前


最近来访(2)
用户头像
锕嗄阿5吖锕啊
用户头像
Angels_of_Death

活动打卡代码 AcWing 3762. 二进制矩阵

#include<bits/stdc++.h>
using namespace std;
const int N = 110, M = 3e4 + 10;
char g[N][N];
int res[M], idx = 0;
int T, n, m;
void show(int x, int y, int d)
{
    if(d == 1)
    {
        printf("%d %d %d %d %d %d\n", x, y, x - 1, y, x, y + 1);
        printf("%d %d %d %d %d %d\n", x, y, x - 1, y, x - 1, y + 1);
        printf("%d %d %d %d %d %d\n", x, y, x, y + 1, x - 1, y + 1);
    }
    else if(d == 2) 
    {
        printf("%d %d %d %d %d %d\n", x, y, x + 1, y, x, y + 1);
        printf("%d %d %d %d %d %d\n", x, y, x, y + 1, x + 1, y + 1);
        printf("%d %d %d %d %d %d\n", x, y, x + 1, y, x + 1, y + 1);
    }
    else if(d == 3) 
    {
        printf("%d %d %d %d %d %d\n", x, y, x + 1, y, x, y - 1);
        printf("%d %d %d %d %d %d\n", x, y, x + 1, y, x + 1, y - 1);
        printf("%d %d %d %d %d %d\n", x, y, x + 1, y - 1, x, y - 1);
    }
    else    
    {
        printf("%d %d %d %d %d %d\n", x, y, x - 1, y, x, y - 1);
        printf("%d %d %d %d %d %d\n", x, y, x - 1, y - 1, x, y - 1);
        printf("%d %d %d %d %d %d\n", x, y, x - 1, y, x - 1, y - 1);
    }
}
void solve()
{
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            if(g[i][j] == '0')  continue;
            if(i < n && j < m)  show(i, j, 2);
            else if(i == n && j < m)    show(i, j, 1);
            else if(i < n && j == m)    show(i, j, 3);
            else    show(i, j, 4);
        }
    }
}
int main()
{
    scanf("%d", &T);
    while(T--)
    {
        int cnt = 0;
        scanf("%d%d", &n, &m);
        getchar();
        for(int i = 1; i <= n; i++)    
        {
            for(int j = 1; j <= m; j++)    
            {
                scanf("%c", &g[i][j]);
                if(g[i][j] == '1')  cnt += 3;
            }
            getchar();
        }
        printf("%d\n", cnt);
        solve();
    }
    return 0;
}


活动打卡代码 AcWing 3761. 唯一最小数

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int vis[N], idx[N];
int T, n, x;
int main()
{
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        memset(vis, 0, (n + 1) * 4);
        for(int i = 1; i <= n; i++)     
        {
            scanf("%d", &x);
            vis[x]++;
            idx[x] = i;
        }
        bool flag = false;
        for(int i = 1; i <= n; i++)
        {
            if(vis[i] == 1)
            {
                flag = true;
                printf("%d\n", idx[i]);
                break;
            }
        }
        if(!flag)   puts("-1");
    }
    return 0;
}


活动打卡代码 AcWing 143. 最大异或对

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
int nums[N];
struct node
{
    node(){
        flag = 0;
        memset(son, 0, sizeof son);
    }
    bool flag;
    node* son[2];
};
node* root;
void insert(int x)
{
    node* p = root;
    for(int i = 31; i >= 0; i--)
    {
        int t = x >> i & 1;
        if(!p->son[t])  p->son[t] = new node();
        p = p->son[t];
    }
    p->flag = true;
}
int solve(int x)
{
    node* p = root;
    int ans = 0;
    for(int i = 31; i >= 0; i--)
    {
        int t = x >> i & 1;
        if(p->son[!t])
        {
            p = p->son[!t];
            ans += 1 << i;
        }
        else    p = p->son[t];
    }
    return ans;
}
int main()
{
    root = new node();
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)  
    {
        scanf("%d", &nums[i]);
        insert(nums[i]);
    }
    int ans = 0;
    for(int i = 0; i < n; i++)
    {
        ans = max(ans, solve(nums[i]));
    }
    cout << ans << endl;
    return 0;
}


活动打卡代码 AcWing 835. Trie字符串统计

#include<bits/stdc++.h>
using namespace std;
struct node
{
    node()
    {
        cnt = 0;
        memset(son, 0, sizeof son);
    }
    int cnt;
    node* son[26];
};
node* root;
void insert(string s)
{
    int n = s.size();
    node* p = root;
    for(int i = 0; i < n; i++)
    {
        int u = s[i] - 'a';
        if(!p->son[u])  p->son[u] = new node();
        p = p->son[u];
    }   
    p->cnt++;
}
int query(string s)
{
    int n = s.size();
    node* p = root;
    for(int i = 0; i < n; i++)
    {
        int u = s[i] - 'a';
        if(!p->son[u])      return 0;
        p = p->son[u];
    }
    return p->cnt;
}
int main()
{
    root = new node();
    int n;
    cin >> n;
    char op[2];
    string s;
    while(n--)
    {
        scanf("%s", op);
        cin >> s;
        if(*op == 'I')  insert(s);
        else    printf("%d\n", query(s));
    }
    return 0;
}



题解:

按照每个店铺id为第一关键字,时间t为第二关键字升序排序。
把每个店铺看做一个集合label,集合中元素为该店铺的订单时间。(以下叙述均称每一个店铺为集合)
对于每一个集合,按照订单时间先后顺序扫描,扫描过程中记录该店铺的实时状态(是否在缓存中),并记录当前的优先级。
遇到一个新集合时,此时应该判断上一个集合是否满足条件,判断末状态即可,更新答案。

细节:在遇到新集合时,上一个集合的最后一个订单时间可能不是T,那么随着时间流逝,优先级应该随着T减少,此刻应该注意判断。


时间复杂度: $O(M * log M + M)$

C++ 代码

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
int cnt[N];
struct message
{
    int t, id;
    bool operator < (const message& m)
    {
        return id < m.id || id == m.id && t < m.t;
    }
}m[N];
int main()
{
    int n, M, T;
    scanf("%d%d%d", &n, &M, &T);
    for(int i = 0; i < M; i++)  scanf("%d%d", &m[i].t, &m[i].id);
    sort(m, m + M);
    int cur = 2, ans = 0;
    bool flag = false;

    for(int i = 1; i <= M; i++)
    {
        if(m[i].id != m[i-1].id)
        {
            if(flag && cur - (T - m[i-1].t) > 3)    ans++;      //注意判断末订单时间
            flag = false;   //恢复初始值,为当前集合服务
            cur = 2;
        }
        else
        {
            int diff = m[i].t - m[i-1].t - 1;
            if(diff == -1)   diff = 0;
            cur = max(0, cur - diff);
            if(cur <= 3)    flag = false;
            cur += 2;
            if(cur > 5)     flag = true;
        }
    }
    printf("%d\n", ans);
    return 0;
}



题解:

按照每个店铺id为第一关键字,时间t为第二关键字升序排序。
把每个店铺看做一个集合label,集合中元素为该店铺的订单时间。(以下叙述均称每一个店铺为集合)
对于每一个集合,按照订单时间先后顺序扫描,扫描过程中记录该店铺的实时状态(是否在缓存中),并记录当前的优先级。
遇到一个新集合时,此时应该判断上一个集合是否满足条件,判断末状态即可,更新答案。

细节:在遇到新集合时,上一个集合的最后一个订单时间可能不是T,那么随着时间流逝,优先级应该随着T减少,此刻应该注意判断。


时间复杂度:$O(M * logM + M)$

C++ 代码

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
int cnt[N];
struct message
{
    int t, id;
    bool operator < (const message& m)
    {
        return id < m.id || id == m.id && t < m.t;
    }
}m[N];
int main()
{
    int n, M, T;
    scanf("%d%d%d", &n, &M, &T);
    for(int i = 0; i < M; i++)  scanf("%d%d", &m[i].t, &m[i].id);
    sort(m, m + M);
    int cur = 2, ans = 0;
    bool flag = false;

    for(int i = 1; i <= M; i++)
    {
        if(m[i].id != m[i-1].id)
        {
            if(flag && cur - (T - m[i-1].t) > 3)    ans++;      //注意判断末订单时间
            flag = false;   //恢复初始值,为当前集合服务
            cur = 2;
        }
        else
        {
            int diff = m[i].t - m[i-1].t - 1;
            if(diff == -1)   diff = 0;
            cur = max(0, cur - diff);
            if(cur <= 3)    flag = false;
            cur += 2;
            if(cur > 5)     flag = true;
        }
    }
    printf("%d\n", ans);
    return 0;
}


活动打卡代码 AcWing 1241. 外卖店优先级

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
int cnt[N];
struct message
{
    int t, id;
    bool operator < (const message& m)
    {
        return id < m.id || id == m.id && t < m.t;
    }
}m[N];
int main()
{
    int n, M, T;
    scanf("%d%d%d", &n, &M, &T);
    for(int i = 0; i < M; i++)  scanf("%d%d", &m[i].t, &m[i].id);
    sort(m, m + M);
    int cur = 2, ans = 0;
    bool flag = false;

    for(int i = 1; i <= M; i++)
    {
        if(m[i].id != m[i-1].id)
        {
            if(flag && cur - (T - m[i-1].t) > 3)    ans++;
            flag = false;
            cur = 2;
        }
        else
        {
            int diff = m[i].t - m[i-1].t - 1;
            if(diff == -1)   diff = 0;
            cur = max(0, cur - diff);
            if(cur <= 3)    flag = false;
            cur += 2;
            if(cur > 5)     flag = true;
        }
    }
    printf("%d\n", ans);
    return 0;
}


活动打卡代码 AcWing 1231. 航班时间

#include<iostream>
#include<string>
using namespace std;

struct time
{
    int h1, m1, s1;
    int h2, m2, s2;
    int d;
}t[2];
void input()
{
    for(int i = 0; i < 2; i++)
    {
        t[i].d = 0;
        scanf("%d:%d:%d %d:%d:%d (+%d)",&t[i].h1,&t[i].m1,&t[i].s1,&t[i].h2,&t[i].m2,&t[i].s2,&t[i].d);
    }
}
void output(int x)
{
    int h = x / 3600;
    x -= h * 3600;
    int m = x / 60;
    x -= m * 60;
    printf("%02d:%02d:%02d\n", h, m, x);
}
void solve()
{
    int sum = 0;
    for(int i = 0; i < 2; i++)
    {
        int tt = (t[i].h2 + t[i].d * 24 - t[i].h1) * 3600 + (t[i].m2 - t[i].m1) * 60 + (t[i].s2 - t[i].s1);
        sum += tt;
    }
    output(sum / 2);
}
int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        input();
        solve();
    }
    return 0;
}


活动打卡代码 AcWing 1229. 日期问题

#include<iostream>
#include<string>
#include<unordered_map>
#include<vector>
#include<algorithm>
using namespace std;
struct time
{
    int y, m, d;
}t[6];
int dd[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int ddd[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
string get(int y, int b, int c)
{
    string s;
    s += to_string(y);
    s += '-';
    if(b < 10)  s += "0";
    s += to_string(b);
    s += '-';
    if(c < 10)  s += "0";
    s += to_string(c);
    return s;
}
bool check(int u)
{
    int y = t[u].y, m = t[u].m, d = t[u].d;
    if(y < 1960 || y > 2059 || m <= 0 || m > 12 || d <= 0 || d > 31)    return false;
    if(y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
    {
        return d <= ddd[m-1];
    }
    else    return d <= dd[m-1];
}
int main()
{
    int a, b, c;
    scanf("%d/%d/%d", &a, &b, &c);
    vector<string> res;
    unordered_map<string, bool> hash;
    int idx = 0;
    t[idx++] = {1900 + a, b, c};
    t[idx++] = {2000 + a, b, c};
    t[idx++] = {1900 + c, a, b};
    t[idx++] = {2000 + c, a, b};
    t[idx++] = {1900 + c, b, a};
    t[idx++] = {2000 + c, b, a};

    for(int i = 0; i < 6; i++)
    {
        if(check(i))
        {
            string s = get(t[i].y, t[i].m, t[i].d);
            if(hash[s])     continue;
            res.push_back(s);
            hash[s] = true;
        }
    }
    sort(res.begin(), res.end());
    for(auto& s : res)  cout << s << endl;
    return 0;
}


活动打卡代码 AcWing 1219. 移动距离

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int w, n, m;
    cin >> w >> n >> m;
    int x1 = (n - 1) / w, x2 = (m - 1) / w;
    int y1 = (n - 1) % w, y2 = (m - 1) % w;
    if(x1 % 2 == 1)     y1 = w - y1 - 1;
    if(x2 % 2 == 1)     y2 = w - y2 - 1;
    int ans = abs(x1 - x2) + abs(y1 - y2);
    cout << ans << endl;
    return 0;
}