10.1-10.4题解

· · 个人记录

::::info[P17539]

link

通过观察本题,可以非常快速发现这题应该使用贪心的思路。

首先由于要求一个区间内 0 和 1 的数量要相等,那么此区间的长度肯定不能为奇数,那么无解判定已经十分明了了。

然后运用前缀和来快速统计该区间内的 01 数量是否相同,不同则需要处理。

而每一次操作可以向左和向右拓展,所以可以一次处理两个待处理区间。

所以答案需要向上取整。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=2e6+10;
int n,m,ans;
int a[N];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        char c;
        cin>>c;
        if(c=='1') a[i]=1;
        else a[i]=-1;
        a[i]+=a[i-1];
    }
    while(m--){
        int l,r;
        cin>>l>>r;
        if((r-l+1)%2){cout<<-1;return 0;}
        if(a[r]-a[l-1]!=0) ans++;
    }
    cout<<(ans+1)/2;
    return 0;
}

::::

::::info[P17555]

link

此题是构造题,由于 n 一定为奇数,那么我们运用等差数列。要让 a 数组形成一个公差为 3 的等差数列,那不难想到把 a_i 设为 3(i-1)即可。

然后构造数组 b,我们可以通过三个集合最终并集要形成整个 3n 来思考,b 可以选择模 3 后余 1 或 2。

所以先尝试模 1,把 b_i 设为 3(i-1)+1,然后检验发现 S_3 内刚好所有的数值模 3 都余 2,从而得出构造方案。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
int n,k;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>k;
    cout<<"Yes\n";
    for(int i=0;i<n;i++) cout<<3*i<<" ";
    cout<<'\n';
    for(int i=0;i<n;i++) cout<<3*i+1<<" ";
    return 0;
}

::::

::::info[CF1790E]

link

模拟赛T1。

讲解一下赛时思路。

刚开始想到的是 40pts-50pts 的做法。

第一个测试点直接打暴力,然后通过观察 checker 可以知道无解判定是当 x 为奇数的时候,从而通过第二个测试点。

然后就是随机生成的第三个测试点,由于大部分都是无解点,所以直接输出 -1 有较大几率拿取这 10pts,但是也有被卡掉的几率。

然后就是观察第四个测试点的样例,可以发现当 x 为 2 的幂时,a = x/2 然后 b = x/2*3 是一定成立的,从而让我在此上面做了文章,得出最终正解则是这样的公式。

最后的证明是:

a + b = (a \oplus b) + 2(a and b)

从而构造这样的 a 和 b 是正确的。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int T;
    cin>>T;
    while(T--){
        int x;
        cin>>x;
        if(x&1){cout<<-1<<'\n';continue;}
        int a=x/2;
        int b=a*3;
        if((a^b)==x) cout<<a<<" "<<b<<'\n';
        else cout<<-1<<'\n';
    }
    return 0;
}

::::

::::info[P17521]

link

其实通过阅读本题不难发现应该是一个 DP,但是官方解是贪心,于是我选择用贪心再来写了一遍。

由于运用的是贪心,那我们肯定需要求出最优方案,最优方案即为所有能量的绝对值之和。

可以发现,我们是否能取到最优方案与其负数个数的奇偶性有关。

1.总数为奇数

2.总数为偶数。

与奇数的情况相反。

从而得出正解。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=1e6+10;
int n;
int a[N];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int T;
    cin>>T;
    while(T--){
        cin>>n;
        int minn=1e18,sum=0,cnt=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            sum+=abs(a[i]);
            minn=min(minn,abs(a[i]));
            if(a[i]<0) cnt++;
        }
        if(n%2==1){
            if(cnt%2==1) cout<<sum-minn*2<<'\n';
            else cout<<sum<<'\n';
        }
        else{
            if(cnt%2==0) cout<<sum-minn*2<<'\n';
            else cout<<sum<<'\n';
        }
    }
    return 0;
}

::::

::::info[P17585]

link

通过阅读本题题面可以得出:

格子 (i,j) 有花的话当且仅当 j \mid i。

关于第 x 列对称,要求 j \mid i 且 j \mid (2x - i)。

取 i = j 得到 j \mid 2x,反过来对称成立,所以为充要条件。

所以答案为:从 1 开始连续整除 2x 的最大整数。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int T;
    cin>>T;
    while(T--){
        int x;
        cin>>x;
        x*=2;
        int i=2;
        while(x%i==0) i++;
        cout<<i-1<<'\n';
    }
    return 0;
}

::::