A
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n, m;
cin >> n >> m;
vector<string> s(n);
for(int i = 0; i < n; i++) cin >> s[i];
for(int i = 0, j = 0; i < n; i++)
{
j += s[i].size();
if(j > m)
{
cout << i << "\n";
return;
}
}
cout << n << "\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while(t--) solve();
return 0;
}
B
我们需要观察到一次操作只能改变奇偶性相隔的数 且需要观察奇偶平均数是否相等
# include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+10;
int a[N],n;
signed main()
{
int T;
cin>>T;
while(T--)
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
int sum1=0,sum2=0,cnt1=0,cnt2=0;
for(int i=1;i<=n;i++)
{
if(i&1)
sum1+=a[i],cnt1++;
else sum2+=a[i],cnt2++;
}
if(sum1%cnt1||sum2%cnt2||sum1*cnt2!=sum2*cnt1)
{
cout<<"NO"<<endl;
continue;
}
cout<<"YES"<<endl;
}
}
C
可以被9整除的数各个数位上数和也为9的倍数 分类讨论即可
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
string s;
cin>>s;
int cnt2=0;
int cnt3=0;
int sum=0;
for (int i=0;i<s.size();i++)
{
if (s[i]=='2')
cnt2++;
else if (s[i]=='3')
cnt3++;
sum+=(s[i]-'0');
}
int r=(9-sum%9);
if (r==9)
{
cout<<"YES"<<"\n";
return;
}
if (r&1)
r+=9;
r-=min(cnt3,(r/6))*6;
int n=r/2;
if (cnt2>=n)
cout<<"YES"<<"\n";
else
cout<<"NO"<<"\n";
}
signed main()
{
int T=1;
cin>>T;
while(T--)
{
solve();
}
return 0;
}
D
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
string s;
cin>>s;
for (int i = 0; i < (int)s.size(); i ++)
{
int res = s[i], pos = i;
for (int j = i + 1; j < min((int)s.size(), i + 11); j ++)
if ((int)s[j] - (j - i) > res) res = (int)s[j] - (j - i), pos = j;
for (int j = pos; j > i; j --) swap(s[j], s[j - 1]), s[j - 1]--;
}
cout<<s<<endl;
}
signed main()
{
int T=1;
cin>>T;
while(T--)
{
solve();
}
return 0;
}
F
根据同余可得a[i]-a[i+1]=(k[i+1]-k[i])*m,a[i]=k[i]m+b,k与m有关 即求数组差之间最大公约数 用线段树维护即可
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N =2e5+10;
int n, m;
LL w[N];
struct Node
{
int l, r;
LL sum, d,ma;
}tr[N * 4];
LL gcd(LL a, LL b)
{
return b ? gcd(b, a % b) : a;
}
void pushup(Node &u, Node &l, Node &r)
{
u.sum = l.sum + r.sum;
u.ma=max(l.ma,r.ma);
u.d = gcd(l.d, r.d);
}
void pushup(int u)
{
pushup(tr[u], tr[u << 1], tr[u << 1 | 1]);
}
void build(int u, int l, int r)
{
if (l == r)
{
LL b = w[r] - w[r - 1];
tr[u] = {l, r, b, b,w[l]};
}
else
{
tr[u].l = l, tr[u].r = r;
int mid = l + r >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
pushup(u);
}
}
void modify(int u, int x, LL v)
{
if (tr[u].l == x && tr[u].r == x)
{
LL b = tr[u].sum + v;
tr[u] = {x, x, b, b};
}
else
{
int mid = tr[u].l + tr[u].r >> 1;
if (x <= mid) modify(u << 1, x, v);
else modify(u << 1 | 1, x, v);
pushup(u);
}
}
Node query(int u, int l, int r)
{
if (tr[u].l >= l && tr[u].r <= r) return tr[u];
else
{
int mid = tr[u].l + tr[u].r >> 1;
if (r <= mid) return query(u << 1, l, r);
else if (l > mid) return query(u << 1 | 1, l, r);
else
{
auto left = query(u << 1, l, r);
auto right = query(u << 1 | 1, l, r);
Node res;
pushup(res, left, right);
return res;
}
}
}
int query5(int u, int l, int r)
{
if (tr[u].l >= l && tr[u].r <= r) return tr[u].ma;
int mid = tr[u].l + tr[u].r >> 1,maa=0;
if (l<= mid) maa=query5(u << 1, l, r);
if(r>mid)maa=max(maa,query5(u<<1+1,l,r));
return maa;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n,q;
cin>>n>>q;
for (int i = 1; i <= n; i ++ )cin>>w[i];
for(int i=1;i<=n;i++)w[i]=w[i+1]-w[i];
build(1, 1, n);
int l, r;
LL d;
while (q-- )
{
cin>>l>>r;
r--;
if(r<l)cout<<0<<" ";
else
{
auto left = query(1, 1, l);
Node right({0, 0, 0, 0});
if (l + 1 <= r) right = query(1, l + 1, r);
int x=abs(gcd(left.sum, right.d));
//if(x==query5(1,l,r))x=0;
cout<<x<<" ";
}
}
cout<<'\n';
}
return 0;
}