题意
题解
代码
#include
using namespace std;
void solve() {
int n; cin>>n;
long long x,sum=0;//注意数据范围
for(int i=0;i<n;i++) cin>>x,sum+=x;
cout<<abs(sum)<<'\n';
}
int main() {
int t=1; cin>>t;
while(t--) solve();
return 0;
}
题意
题解
代码
#include
using namespace std;
void solve() {
int n; cin>>n;
cout<<(n+1)/2<<'\n';
for(int i=0;i<(n/2);i++)
cout<<i*3+1<<' '<<3*n-3*i<<'\n';
if(n&1) cout<<(n+1)/2*3-2<<' '<<(n+1)/2*3<<'\n';
}
int main() {
int t=1; cin>>t;
while(t--) solve();
return 0;
}
题意
题解
代码
#include
using namespace std;
void solve() {
int n,x,mi; cin>>n;
bool f=0;
for(int i=1;i<=n;i++) {
cin>>x;
if(i==1) mi=x;
if(x<mi) f=1;
}
if(!f) cout<<"Bob"<<'\n';
else cout<<"Alice"<<'\n';
}
int main() {
int t=1; cin>>t;
while(t--) solve();
return 0;
}
题意
题解
代码
#include
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
ll n,q; cin >> n >> q;
vector<ll> a(n);
for(ll i=0;i<n;++i) cin >> a[i];
vector<ll> b(n),c(n);//前缀和,异或前缀和
for(ll i=0;i<n;++i){
b[i] = a[i];
c[i] = a[i];
if(i > 0){
b[i] ^= b[i-1];
c[i] += c[i-1];
}
}
vector<map<ll,vector<ll> > > h(2);
for(ll i=0;i<n;++i) h[i%2][b[i]].push_back(i);//预处理异或和相同且奇偶性相同的下标
while(q--){
ll l,r;
cin >> l >> r; --l; --r;//下标调整
if((b[r]^(l == 0 ? 0 : b[l-1])) != 0){//异或和不为0
cout << -1 << "\n";
continue;
}
if(c[r]-(l == 0 ? 0 : c[l-1]) == 0){//异或和为0且全是0
cout << 0 << "\n";
continue;
}
if((r-l+1)%2 == 1 || a[l] == 0 || a[r] == 0){//异或和为0,且有效区间可看成奇数长度
cout << 1 << "\n";
continue;
}
ll v = (l == 0 ? 0 : b[l-1]);
auto it = lower_bound(h[l%2][v].begin(),h[l%2][v].end(),l);
ll k = (it == h[l%2][v].end() ? n : *it);//l后面离l最近的下标
cout << (k < r ? 2 : -1) << "\n";//下标存在2次,不存在-1
}
return 0;
}