A. 四种情况 不改 改倒数第一个 改倒数第二个 后面两个全改 (自己的代码难懂 就贴个大佬的)
void solve()
{
cin >> n;
for(int i = 1; i <= n ; i ++ ) cin >> a[i];
int sum = a[1];
for(int i = 2; i <= n ; i ++ )
sum = __gcd(sum, a[i]);
if(sum == 1) {
cout << 0 << endl;
return ;
}
if(__gcd(n, sum) == 1) cout << 1 << endl;
else if(__gcd(n - 1, sum) == 1) cout << 2 << endl;
else cout << 3 << endl;
}
B. 只要是不同就要改一次 所以统计多少不同即可
#include <bits/stdc++.h>
using namespace std;
void solve(){
int n;cin>>n;int cnt=0;
string s;cin>>s;
for(int i=0;i<n-1;i++){
if(s[i]!=s[i+1])cnt++;
}
if(s[0]=='0')cnt--;
if(cnt>0)cout<<cnt<<'\n';
else cout<<0<<'\n';
}
int main(){
int t;cin>>t;
while(t--){
solve();
}
}
C1. 最大值一定是 所有的sum-所有的XOR 二分长度即可
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=5e5;
ll n,q;
ll a[N];
void solve()
{
cin>>n>>q;
ll ans=0,x=0;
for (ll i=1;i<=n;i++)
cin>>a[i],ans+=a[i],x^=a[i];
while (q--)
{
ll x,y;cin>>x>>y;
}
ll ma=ans-x;
ans=x=0;
ll l=1,r=0;
ll pl=1,pr=n;
while (l<=n)
{
if (r<l)
r++,ans+=a[r],x^=a[r];
while (r<n&&ans-x!=ma)
r++,ans+=a[r],x^=a[r];
if (ans-x!=ma)
break;
if (r-l+1<pr-pl+1)
pl=l,pr=r;
ans-=a[l],x^=a[l],l++;
}
cout<<pl<<" "<<pr<<'\n';
return;
}
int main()
{
ll t;cin>>t;
while (t--)
solve();
}
D1. 直接模拟即可
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 10;
int n;
set<ll> s;
map<ll, ll> mp;
int main()
{
scanf("%d", &n);
char op;
ll x;
for(int i = 1; i <= n; i ++) {
scanf(" %c%lld", &op, &x);
if(op == '+') {
s.insert(x);
}else{
if(!mp[x]) mp[x] = x;
ll y = mp[x];
while(s.count(y)) y += x;
mp[x] = y;
printf("%lld\n", y);
}
}
}
D2. 就是多个减法 我们只需要再开一个map 去存减法 模拟即可
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll, set<ll>> del, path;
set<ll> s;
void solve(){
char o;
ll x;
cin >> o >> x;
if(o == '+') {
s.insert(x);
}
else if(o == '-') {
s.erase(x);
for(auto i : path[x]) {
del[i].insert(x);
}
path[x].clear();
}
else {
if(del[x].empty()) {
del[x].insert(x);
}
while(1) {
ll t = *del[x].begin();
path[t].insert(x);
if(!s.count(t)) {
break;
}
del[x].erase(del[x].begin());
if(del[x].empty()) {
del[x].insert(t + x);
}
}
cout << *del[x].begin() << '\n';
}
}
int main(){
int t;cin>>t;
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
//int t=1;
while(t--){
solve();
}
}