牛客周赛 Round 36(A~F)

文章涉及C++编程中的几个竞赛题目,包括异或和计算、字符串中大写字母对答案的贡献、回文子串检测、队列使用中的注意事项以及树状数组在解决查询问题的应用。
摘要由CSDN通过智能技术生成

A

签到直接 / 1000 /1000 /1000输出即可

#include <bits/stdc++.h>

#define int long long
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define fep(i, a, b) for(int i = (a); i >= (b); --i)
#define _for(i, a, b) for(int i=(a); i<(b); ++i)
#define pii pair<int, int>
#define pdd pair<double,double>
#define ll long long
#define db double
#define endl '\n'
#define fs first
#define sc second
#define pb push_back
#define vi vector<int>

using namespace std;


void solve() {
    int n;
    cin>>n;
    cout<<n/1000<<'\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
//	freopen("C:\\Users\\24283\\CLionProjects\\untitled2\\1.in", "r", stdin);
    solve();
    return 0;
}

B

B题主要是一个异或和的概念
异或和是一些数连续异或。
直接按题意模拟即可。


#include <bits/stdc++.h>

#define int long long
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define fep(i, a, b) for(int i = (a); i >= (b); --i)
#define _for(i, a, b) for(int i=(a); i<(b); ++i)
#define pii pair<int, int>
#define pdd pair<double,double>
#define ll long long
#define db double
#define endl '\n'
#define fs first
#define sc second
#define pb push_back
#define vi vector<int>

using namespace std;
int a[110][110];

void solve() {
    int n,m,x;
    cin>>n>>m>>x;
    int res=0;
    rep(i,1,n){
        rep(j,1,m){
            cin>>a[i][j];
            res+=a[i][j];
        }
    }
    if(res!=x){
        cout<<"wrong answer\n";
        return;
    }
    //判断行得和
    int hy=0,ly=0;
//    rep(i,1,m)  hy^=a[1][i];
//    rep(i,1,n)  ly^=a[i][1];
    rep(i,1,n){
        int ans=0;
        rep(j,1,m){
            ans^=a[i][j];
        }
        hy+=ans;
    }

    rep(i,1,m){
        int ans=0;
        rep(j,1,n){
            ans^=a[j][i];
        }
        ly+=ans;
    }
    if(ly!=hy){
        cout<<"wrong answer\n";
        return;
    }
    cout<<"accepted\n";
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
// 	freopen("C:\\Users\\24283\\CLionProjects\\untitled2\\1.in", "r", stdin);
    solve();
    return 0;
}

C

贪心、构造
考虑对答案的贡献。
如果一个数是小写字母对答案是没有贡献的后面只要出现一个大写字母就会对答案贡献1。
i i i是大写字母,如果 i + 1 i+1 i+1是大写字母对答案会贡献1
如果 i + 1 i+1 i+1是小写字母对答案

#include <bits/stdc++.h>

#define int long long
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define fep(i, a, b) for(int i = (a); i >= (b); --i)
#define _for(i, a, b) for(int i=(a); i<(b); ++i)
#define pii pair<int, int>
#define pdd pair<double,double>
#define ll long long
#define db double
#define endl '\n'
#define fs first
#define sc second
#define pb push_back
#define vi vector<int>

using namespace std;
int a[110][110];

void solve() {
    string s;
    cin>>s;
    int ans=0;
    rep(i,0,s.size()-2){
        if(islower(s[i])&&isupper(s[i+1])){
            ans++;
            i++;
            continue;
        }
        if(isupper(s[i])&& isupper(s[i+1])){
            ans++;
            i++;
            continue;
        }
    }
    cout<<ans<<'\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
// 	freopen("C:\\Users\\24283\\CLionProjects\\untitled2\\1.in", "r", stdin);
    solve();
    return 0;
}

D

昨天调了好久
之前一直没有注意入队出队的标记的问题
b f s 一定要在入队的时候标记 bfs一定要在入队的时候标记 bfs一定要在入队的时候标记,如果一个点在出队的时候标记可能会出现下面情况
262612440b17542695a202b5c7c8f12.jpg

#include <bits/stdc++.h>

#define int long long
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define fep(i, a, b) for(int i = (a); i >= (b); --i)
#define _for(i, a, b) for(int i=(a); i<(b); ++i)
#define pii pair<int, int>
#define pdd pair<double,double>
#define ll long long
#define db double
#define endl '\n'
#define fs first
#define sc second
#define pb push_back
#define vi vector<int>

using namespace std;
int vis[1010][1010],d[1010][1010];
char mp[1010][1010];
int n,m;
int dx[]={0,0,0,1,-1};
int dy[]={0,1,-1,0,0};

void solve()
{
    cin>>n>>m;
    rep(i,1,n){
        cin>>(mp[i]+1);
    }
    queue<pii>q;
    q.push({1,1});
    d[1][1]=0;
    vis[1][1]=1;
    while(q.size()){
        auto u=q.front();
        q.pop();
//        vis[u.first][u.second]=1;
        if(u.first==n&&u.second==m){
            cout<<d[n][m]<<'\n';
            return;
        }
//         vis[u.first][u.second]=1;
        rep(i,1,4){
            int xx=u.first+dx[i];
            int yy=u.second+dy[i];
            if(xx<1||xx>n||yy<1||yy>m||vis[xx][yy])  continue;
            if(mp[xx][yy]==mp[u.first][u.second]) continue;
            q.push({xx,yy});
            vis[xx][yy]=1;
            d[xx][yy]=d[u.first][u.second]+1;
        }
    }
    cout<<-1<<'\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
//	freopen("C:\\Users\\24283\\CLionProjects\\untitled2\\1.in", "r", stdin);
    solve();
    return 0;
}

E

瞎搞的题目只需要构造3 * 3的矩阵其他随机就行

#include <bits/stdc++.h>

#define int long long
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define fep(i, a, b) for(int i = (a); i >= (b); --i)
#define _for(i, a, b) for(int i=(a); i<(b); ++i)
#define pii pair<int, int>
#define pdd pair<double,double>
#define ll long long
#define db double
#define endl '\n'
#define fs first
#define sc second
#define pb push_back
#define vi vector<int>

using namespace std;
int mp[1010][1010];
int n,m;

const unsigned zseed=time(0);
mt19937_64 zgen{zseed};
struct UI{
    //随机数的分布器
    //产生随机数的区间是[a,b]
    uniform_int_distribution<int>u;
    //随机数的生发器
    mt19937_64& gen{zgen};
    int get()
    {
        return u(gen);
    }
    UI(int a=0,int b=1):u(a,b){};
};

void solve() {
    UI u{0,26};
    int n,m;
    cin>>n>>m;
    mp[1][1]=mp[1][2]=3;
    mp[2][1]=mp[3][1]=2;
    mp[2][2]=mp[2][3]=mp[3][2]=1;
    rep(i,1,n){
        rep(j,1,m){
            if(!mp[i][j]){
                mp[i][j]=u.get();
            }
        }
    }
    rep(i,1,n){
        rep(j,1,m){
            cout<<(char)(mp[i][j]+'a');
        }
        cout<<'\n';
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
// 	freopen("C:\\Users\\24283\\CLionProjects\\untitled2\\1.in", "r", stdin);
    solve();
    return 0;
}

F

不包含长度不小于 2 的回文子串这个性质比较重要
先来看一下长度为2的回文串有哪些
aa
aba
abba
abcba
可以观察到一些性质
就是长度大于3的回文串会包含长度为2或3的回文串
所以好串的含义就是不包含长度为3的回文串
接着考虑假设第一个是r
那个第二个只能是e/d第三个就只能是d/e
这就启发我们修改成好串的情况只有3 * 2种
然后我们只需要在查询的时候比较一下6种那种是最小的
如何快速查询的这个可以用前缀和处理
但是设计到修改操作,所以这一需要一点点数据结构去维护
可以用树状数组去维护,线段树也是可以的.

#include <bits/stdc++.h>

#define int long long
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define fep(i, a, b) for(int i = (a); i >= (b); --i)
#define _for(i, a, b) for(int i=(a); i<(b); ++i)
#define pii pair<int, int>
#define pdd pair<double,double>
#define ll long long
#define db double
#define endl '\n'
#define fs first
#define sc second
#define pb push_back
#define vi vector<int>

using namespace std;
const int maxn = 2e5 + 10;
int tr[6][maxn];
int n,q;
string s;
string c[6]={"red","erd","dre","rde","edr","der"};
void add(int op,int x, int d){
    for(;x<=2e5;x+=x&-x)   tr[op][x]+=d;
}

int query(int op,int x){
    int res=0;
    for(;x;x-=x&-x) res+=tr[op][x];
    return res;
}

int ask(int op,int l,int r){
    return query(op,r)- query(op,l-1);
}

void solve() {
    cin>>n>>q;
    cin>>s;
    s=" "+s;
//    rep(i,1,n){
//        cout<<s[i];
//    }
//    cout<<'\n';
//    rep(i,1,n){
//        cout<<c[0][(i-1)%3];
//    }
//    cout<<'\n';
    //初始化树状数组
    rep(j,0,5){
        rep(i,1,n){
            int wei=i%3;
            int x=(s[i]!=c[j][wei]);
//            cout<<i<<' '<<x<<'\n';
            add(j,i,x);
        }
    }
    //q次操作
    while(q--){
        int op;
        cin>>op;
        if(op==1){//修改
            int x;
            char chr,lst;
            cin>>x>>chr;
            lst=s[x];
            //维护树状数组
            rep(i,0,5){
                int wei=x%3;
                if(s[x]!=c[i][wei]){
                    add(i,x,-1);
                }
                //修改
                s[x]=chr;
                if(s[x]!=c[i][wei]){
                    add(i,x,1);
                }
                s[x]=lst;
            }
            s[x]=chr;
        }else{//查询
            int res=1e9;
            int l,r;
            cin>>l>>r;
            rep(i,0,5){
                res=min(res,ask(i,l,r));
            }
            cout<<res<<'\n';
        }
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
// 	freopen("C:\\Users\\24283\\CLionProjects\\untitled2\\1.in", "r", stdin);
    solve();
    return 0;
}
  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
牛客 a卷2022年第四季度的华为题目中,要求考生设计一种高效的数据结构,能够支持以下几种操作: 1. 添加一个元素 2. 删除一个元素 3. 查找是否存在某个元素 4. 返回元素的总数 该数据结构要求满足空间复杂度较小、时间复杂度较低、能够快速地进行查找和修改等多种操作。 想要编写这样一种数据结构,我们可以参考许多已有的经典算法与数据结构,如二叉树、哈希表、红黑树等,通过综合利用它们的优点来实现这个问题的解决。 例如,我们可以通过哈希表来存储所有元素的值,并在每个哈希链表的元素中再使用红黑树来进行排序与查找。这样,我们既能够轻松地进行元素的添加和删除操作,也能够在查找较大数据范围和数量时保持较高的速度与效率。同时,由于使用了多个数据结构来协同完成这个问题,我们也能够在空间复杂度上适度地进行优化。 当然,在具体设计这个数据结构的过程中,我们还需要考虑一些实践中的细节问题,例如如何避免哈希冲突、如何处理数据丢失与被删除元素所占用的空间等问题,这都需要相应的算法与流程来进行处理。 总体来看,设计这种支持多种操作的高效数据结构,需要我们具备丰富的算法知识和编程实践能力,同时需要我们在具体处理问题时能够将多种算法和数据结构进行有效地结合。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值