Codeforces Round 924 (Div. 2)

本文讨论了C++中的两个技术问题,一是通过双指针和条件判断检查数字是否可通过交换得到相同值,二是利用排序和去重技术确定满足特定条件的序列组合数。

A.

赛时乱模拟写过

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+9;
int a[N];
void lan(){
    ll a,b;
    cin>>a>>b;
    ll ta=a;
    ll tb=b;
    ll ans=a*b;
    if(a&1 && b&1){
        cout<<"No"<<'\n';
        return;
    }
    if(a&1){
        b/=2;
        a*=2;
        if(b==ta && a==tb){
            cout<<"No"<<'\n';
            return;
        }
        if(a*b==ans){
            cout<<"Yes"<<'\n';
            return;
        }
    }else if(b&1){
        a/=2;
        b*=2;
        if(a==tb && b==ta){
            cout<<"No"<<'\n';
            return;
        }
        if(a*b==ans){
            cout<<"Yes"<<'\n';
            return;
        }
    }
    if(!(a&1) && !(b&1)){
        cout<<"Yes"<<'\n';
        return;
    }
    cout<<"No"<<'\n';
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int q;
    cin>>q;
    while(q--){
        lan();
    }
    return 0;
}

B.

假设要到2->5,如果有2个2必然只有一个能加到5,因此排序去重,只要左右差值小于n-1(最少也要加1)必然有一种排序方式构成相同数字,找最大数量,双指针找

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+9;
void lan(){
    int n;
    cin>>n;
    vector<int> v;
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        v.push_back(x);
    }
    //排序去重
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    int ans=1;
    //极差在n-1范围内就存在某种排序方式使得排列加上后得到相同数字
    for(int i=0,j=0;i<v.size();i++){
        while(j+1<v.size() && v[j+1]-v[i]<=n-1){
            j++;
        }
        ans=max(ans,j-i+1);
    }
    cout<<ans<<'\n';
    //静态数组写法    
    //int m=unique(a+1,a+1+n)-a-1;去重的数组长度
    //for(int i=1,j=1;i<=m;i++){
    //    while(a[j]<a[i]+1-n){
    //        j++;
    //    }
    //    ans=max(ans,i-j+1);
    //}
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int q;
    cin>>q;
    while(q--){
        lan();
    }
    return 0;
}

C.

1.(1~k)可以知道(2k-2)*t+x=n=>n-x%(2k-2)==0;

2.(k-1~2)可以知道(2k*2)*t+k+k-x=n=>n+x-2%(2k-2)==0;

枚举n-x(1~k),n+x-2(k-1~2)的因子

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+9;
int a[N];
void lan(){
    int n,x;
    cin>>n>>x;
    set<ll> st;
    for(int i=1;i<=(n-x)/i;i++){
        if((n-x)%i==0){
            if(!(i&1)){
                st.insert(i);
            }
            if(!(((n-x)/i)&1)){
                st.insert((n-x)/i);
            }
        }
    }
    for(int i=1;i<=(n+x-2)/i;i++){
        if((n+x-2)%i==0){
            if(!(i&1)){
                st.insert(i);
            }
            if(!(((n+x-2)/i)&1)){
                st.insert((n+x-2)/i);
            }
        }
    }
    ll ans=0;
    for(auto i: st){
        if((i+2)/2>=x){
            ans++;
        }
    }
    cout<<ans<<'\n';
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int q;
    cin>>q;
    while(q--){
        lan();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值