A.
直接判断输出
#include <bits/stdc++.h>
using namespace std;
void solve(){
int a,b,c;cin>>a>>b>>c;
if(a+b==c)cout<<'+'<<'\n';
else if(a-b==c)cout<<"-"<<'\n';
}
signed main(){
int hey_left=1;
cin>>hey_left;
while(hey_left--){
solve();
}
}
B.
把偶数全部放前面
再维护偶数和与奇数和
遍历所有数,是偶数就+到偶数和,是奇数就加到奇数和,若某一个时刻奇数和大等于偶数和了,就是no
#include <bits/stdc++.h>
using namespace std;
void solve(){
int n;cin>>n;
vector<int>a(n+1);
for(int i=1;i<=n;i++)cin>>a[i];
vector<int>ou,ji;
for(int i=1;i<=n;i++){
if(a[i]%2==0)ou.push_back(a[i]);
else ji.push_back(a[i]);
}
for(int i=0;i<ji.size();i++){
ou.push_back(ji[i]);
}
int sum_ou=0,sum_ji=0;
bool f=0;
for(int i=0;i<ou.size();i++){
if(ou[i]%2==0)sum_ou+=ou[i];
else sum_ji+=ou[i];
if(sum_ji>=sum_ou){
f=1;break;
}
}
if(f)cout<<"NO"<<'\n';
else cout<<"YES"<<'\n';
}
signed main(){
int hey_left=1;
cin>>hey_left;
while(hey_left--){
solve();
}
}
C.
直接按题意模拟
需要注意的是,mp[]=0相当于没标记
这里换成1和2即可
#include <bits/stdc++.h>
using namespace std;
void solve(){
int n;cin>>n;
string s;cin>>s;
map<char,int>mp;
mp[s[0]]=1;
// cout<<"s[0]="<<s[0]<<' '<<"mp[]="<<mp[s[0]]<<'\n';
bool f=0;
for(int i=1;i<s.size();i++){
if(s[i]==s[i-1]){
f=1;
break;
}
if(mp[s[i]]){
if(mp[s[i-1]]==mp[s[i]]){
f=1;
break;
}
}else {
if(mp[s[i-1]]==1)mp[s[i]]=2;
else if(mp[s[i-1]]==2)mp[s[i]]=1;
}
//cout<<"s[i]="<<s[i]<<' '<<"mp[]="<<mp[s[i]]<<'\n';
}
if(f)cout<<"NO"<<'\n';
else cout<<"YES"<<'\n';
}
signed main(){
int hey_left=1;
cin>>hey_left;
while(hey_left--){
solve();
}
}
D.
记录下前缀和与后缀和,再加上改变的和
判奇偶性
#include <bits/stdc++.h>
using namespace std;
void solve(){
int n,q;cin>>n>>q;
vector<int>a(n+1),pre(n+5),post(n+5);
for(int i=1;i<=n;i++){
cin>>a[i];
pre[i]=pre[i-1]+a[i];
}
for(int i=n;i>=1;i--)post[i]=post[i+1]+a[i];
while(q--){
int l,r,k;cin>>l>>r>>k;
int sum=0;
sum+=pre[l-1]+post[r+1]+(r-l+1)*k;
if(sum&1){
cout<<"YES"<<'\n';
}else cout<<"NO"<<'\n';
}
}
signed main(){
int hey_left=1;
cin>>hey_left;
while(hey_left--){
solve();
}
}
E.
二分特殊石头所在的区间
注意要特判边界,详情见代码
#include <bits/stdc++.h>
using namespace std;
void solve(){
int n;cin>>n;
vector<int>a(n+1),pre(n+1);
for(int i=1;i<=n;i++){
cin>>a[i];
pre[i]=pre[i-1]+a[i];
}
int l=1,r=n,mid;
while(l<=r){
mid=(l+r)/2;
cout<<"? "<<(mid-l+1)<<' ';
for(int i=l;i<=mid;i++){
cout<<i;
if(i==mid)cout<<endl;
else cout<<' ';
}
int x;cin>>x;
if(x==pre[mid]-pre[l-1])l=mid+1;
else {
cout<<"? "<<1<<' '<<mid<<endl;
cin>>x;
if(x==a[mid])r=mid-1;
else {
cout<<"! "<<mid<<endl;
return ;
}
}
}
cout<<"! "<<r<<endl;
}
signed main(){
int hey_left=1;
cin>>hey_left;
while(hey_left--){
solve();
}
}
标签:11,pre,int,hey,859,--,solve,left
From: https://www.cnblogs.com/wwww-/p/17976476