10.1-10.4题解
Love_Ti_fjx · · 个人记录
::::info[P17539]
link
通过观察本题,可以非常快速发现这题应该使用贪心的思路。
首先由于要求一个区间内
然后运用前缀和来快速统计该区间内的
而每一次操作可以向左和向右拓展,所以可以一次处理两个待处理区间。
所以答案需要向上取整。
#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
此题是构造题,由于
然后构造数组
所以先尝试模
#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。
讲解一下赛时思路。
刚开始想到的是
第一个测试点直接打暴力,然后通过观察 checker 可以知道无解判定是当
然后就是随机生成的第三个测试点,由于大部分都是无解点,所以直接输出
然后就是观察第四个测试点的样例,可以发现当
最后的证明是:
从而构造这样的
#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
通过阅读本题题面可以得出:
格子
关于第
取
所以答案为:从
#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;
}
::::