牛客OI周赛7-普及组

牛客OI周赛7-普及组

A 救救猫咪 \sqrt{}

因为 n 比较小, 直接 O ( n 2 ) O(n^2) O(n2)

#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
int n;
int const N = 1100;
pair<int, int> q[N];

int main(){
    cin>>n;
    for(int i=1; i<=n; i++){
        int x, y;
        cin>>x>>y;
        q[i]={x, y};
    }
    for(int i=1; i<=n; i++){
        int res=0;
        for(int j=1; j<=n; j++){
            if(i==j) continue;
            if(q[j].x>q[i].x&&q[j].y>q[i].y) res ++;
        }
        cout<<res<<'\n';
    }
    return 0;
} 

B 救救兔子 × \times ×

第一次想的离散化, 有点麻烦就没写。 二分做简单点, 脑子晚上不太好使

#include<bits/stdc++.h>
using namespace std;

int n;
int const N = 1e5+10;
int a[N];

int main(){
    ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
    cin>>n;
    for(int i=1; i<=n; i++) cin>>a[i];
    sort(a+1,a+n+1);
    a[0]=2e9;
    a[n+1]=2e9;

    int t;
    cin>>t;
    while(t--){
        int x;
        cin>>x;
        int pos=lower_bound(a+1, a+n+1, x)-a;
        if(abs(a[pos]-x) < abs(a[pos-1]-x)){
            cout<<a[pos]<<'\n';
        }
        else{
            cout<<a[pos-1]<<'\n';
        }
    }

    return 0;
} 

C 救救企鹅 \sqrt{}

KMP, 这题的数据比较弱, 有的解法不合格, 题目要求转化后的字符不允许再转换

解题思路同 kuangbin 专题的一道题, 即 kmp 匹配成功后, j=0 重新匹配

#include<bits/stdc++.h>
using namespace std;

string str, a, b;
int const N = 1e6+10;
int ne[N];

int main(){
    ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
    cin>>str>>a>>b;
    int lena=a.size(); a=' '+a;
    int len=str.size(); str=' '+str;
    for(int i=2, j=0; i<=lena; i++){
        while(j&&a[i]!=a[j+1]) j=ne[j];
        if(a[i]==a[j+1]) j++;
        ne[i]=j;
    }
    for(int i=1, j=0; i<=len; i++){
        while(j&&str[i]!=a[j+1]) j=ne[j];
        if(str[i]==a[j+1]) j++;
        if(j==lena){
           // cout<<i-lena+1<<'\n';
            str.replace(i-lena+1, lena, b);
            len=str.size();
           // cout<<str<<'\n';
            j=0;
        }
    }
    cout<<str.substr(1);
    return 0;
}

D 数糖纸 × \times ×

抽象一下 :

    就是求最长不重复子串

做法 :

#include<bits/stdc++.h>
using namespace std;

int n;
int const N = 1e6+10;
int a[N];
map<int, int> q;

int main(){
    ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
	cin>>n;
    for(int i=1; i<=n; i++){
        cin>>a[i];
    }
    int res=0;
    for(int i=1, j=1; i<=n; i++){
        int &t = q[a[i]];
        t ++;
        while(t>1){
            q[a[j]]--;
            j++;
        }
        // cout<<j<<' '<<i<<'\n';
        res=max(res, i-j+1);
    }
    cout<<res;
    
	return 0;
} 

类似双指针(尺取法), a i a_i ai 比较大就是 O ( n l o g n ) O(nlogn) O(nlogn) , 因为要开一个 m a p map map , 否则 O ( n ) O(n) O(n)

总结

  • 练练离散化

  • 积累做题经验

  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值