9.10比赛

· · 个人记录

A:CF934B A Prosperous Lot

思路

因为 8 有两个圈所以先用 8 如果发现要的圈是奇数个就在结尾输出一个 6 ,不能是 0 因为 0 不是正整数

时间复杂度: O(k)

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
int k,a[]={1,0,0,0,1,0,1,0,2,1};
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>k;
    if(k>=38) cout<<-1;
    else if(k%2==0) for(int i=1;i<=k/2;i++) cout<<'8';
    else{
        for(int i=1;i<=k/2;i++) cout<<'8';
        cout<<'6';
    }
    return 0;
}

B:CF2007B Index and Maximum Value

思路

题意:给定 n 个元素,有 m 次操作,求每次操作后的最大值

时间复杂度 : $O(n*m) - 情况一 : $maxi<L$ ,则 $[L,R]$ 内没有数要修改,直接输出 $maxi

时间复杂度 : O(n+m)

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int n,m,a[N],sub[N];
void solve(){
    int maxi=-1e18;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        maxi=max(maxi,a[i]);
    }
    while(m--){
        char c;
        int x,y;
        cin>>c>>x>>y;
        if(x<=maxi&&maxi<=y){
            if(c=='-') maxi--;
            else maxi++;
        }
        cout<<maxi<<" ";
    }
    cout<<'\n';
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int T;
    cin>>T;
    while(T--) solve();
    return 0;
}

C:CF2092B Lady Bug

思路

$2.$ 将 $a$ 看做 $0$ 行,将 $b$ 看做 $1$ 行 $3.$ 对于 $a$ 中的 $1$, 其所在的行和所在的列的和在操作后不改变奇偶性,反过来的 $b$ 中的一也是同理 $4.$ 统计 $a$ 和 $b$ 中所有的 $1$ ,并区分为两类 $t0$ 表示行加列是偶数的 $1$ 的数量 , $t1$ 表示行加列是奇数的 $1$ 的数量 $5.$ 所有的 $t0$ 的 $1$ 都在 $b$ 中占据奇数列,而 $t1$ 的 $1$ 都在 $b$ 中占据偶数列 $6.$ 显然 $b$ 中的奇数列位置 $odd=(n+1)/2$ ,偶数列 $even=n/2 7.$ 当且仅当` t0<=odd && t1<=even `时输出 $Yes

时间复杂度: O(n)

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int n,m;
string a,b;
void solve(){
    int t0=0,t1=0,sum=0;
    cin>>n>>a>>b;
    for(int i=0;i<n;i++){
        int jia=i+1;
        if(a[i]=='1'){
            sum=jia;
            if(sum%2==0) t0++;
            else t1++;
        }
        if(b[i]=='1'){
            sum=jia+1;
            if(sum%2==0) t0++;
            else t1++;
        }
    }
    int odd=(n+1)/2,even=n/2;
    if(t0<=odd&&t1<=even) cout<<"Yes\n";
    else cout<<"No\n";
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int T;
    cin>>T;
    while(T--) solve();
    return 0;
}

D:P8197 [传智杯 #4 决赛] 排排队

思路

1.$ 将原始 $a$ , $b$ 数组 $copy$ 一份 $A , B 2.$ 将 $A$ 和 $B$ 排序时候依次枚举比较 , 若 $A!=B$ , 输出 $NO $4.$ 交换次数至多是 $n*(n-1)/2$ , 一定不超过 $n^2 时间复杂度:$O(n^2)

代码

#include<bits/stdc++.h>
using namespace std;
#define int long long
int a[1005],b[1005];
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T;
    cin>>T;
    while(T--){
        map<int,int>mp;
        int n;
        cin>>n;
        for(int i=1;i<=n;i++) cin>>a[i],mp[a[i]]++;
        int f=0;
        for(int i=1;i<=n;i++){
            cin>>b[i];
            if(mp[b[i]]<=0){
                cout<<"NO\n";
                f=1;
                break;
            }
            mp[b[i]]--;
        }
        if(f) continue;
        cout<<"YES\n";
        for(int i=1;i<=n;i++){
            if(a[i]==b[i])continue;
            for(int j=i+1;j<=n;j++){
                if(a[j]!=b[i]) continue;
                int last=a[i];
                a[i]=a[j];
                for(int k=i+1;k<=j;k++){
                    cout<<j-k+i+1<<" "<<j-k+i<<'\n';
                    int t=a[k];
                    a[k]=last;
                    last=t;
                }
            }
        }
        cout<<"0 0\n";
    }
    return 0;
}