A. Serval and String Theory
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--)
{
int n,k;
cin>>n>>k;
string s;
cin>>s;
string t=s;
reverse(t.begin(),t.end());
if(k==0)
{
s<t? cout<<"YES\n":cout<<"NO\n";
}
else
{
bool f=true;
for(int i=1;i<n;i++)
{
if(s[i]!=s[i-1])
{
f=false;
break;
}
}
if(f==true)
{
cout<<"NO\n";
}
else
{
cout<<"YES\n";
}
}
}
return 0;
}
B. Serval and Final MEX
- 从一个极端走到了另一个极端…看见这么松的限制就觉得题目肯定又在骗我…不曾想这次出题人是真的想降低题目难度…然后分类讨论把自己绕晕了…
- 另外一个有趣的方法是,把 n>4 的情况通过任意一次操作化归到 n=4 的情况,然后就可以随便做了
#include <bits/stdc++.h>
using namespace std;
int a[5005];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--)
{
int n,cnt=0,p=-1;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(a[i]==0)
{
cnt++;
if(i!=1&&i!=n)
{
p=i;
}
}
}
if(cnt==0)
{
cout<<1<<endl;
cout<<1<<" "<<n<<endl;
}
else if(p!=-1)
{
if(cnt>1)
{
if(p==2)
{
cout<<1+(a[1]==0||a[2]==0)+(a[n]==0||a[n-1]==0)<<endl;
if(a[n]==0||a[n-1]==0)
{
cout<<n-1<<" "<<n<<endl;
}
if(a[1]==0||a[2]==0)
{
cout<<1<<" "<<2<<endl;
}
cout<<1<<" "<<n-(a[1]==0||a[2]==0)-(a[n]==0||a[n-1]==0)<<endl;
}
else
{
if(cnt==2&&a[n]==0)
{
cout<<2<<endl;
cout<<p<<" "<<n<<endl;
cout<<1<<" "<<p<<endl;
}
else
{
cout<<3<<endl;
cout<<p<<" "<<n<<endl;
cout<<1<<" "<<p-1<<endl;
cout<<1<<" "<<2<<endl;
}
}
}
else
{
cout<<2<<endl;
cout<<p<<" "<<n<<endl;
cout<<1<<" "<<p<<endl;
}
}
else
{
cout<<1+(a[1]==0||a[2]==0)+(a[n]==0||a[n-1]==0)<<endl;
if(a[n]==0||a[n-1]==0)
{
cout<<n-1<<" "<<n<<endl;
}
if(a[1]==0||a[2]==0)
{
cout<<1<<" "<<2<<endl;
}
cout<<1<<" "<<n-(a[1]==0||a[2]==0)-(a[n]==0||a[n-1]==0)<<endl;
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--)
{
int x,y;
cin>>x>>y;
if(x>y)
{
swap(x,y);
}
if(x==y)
{
cout<<-1<<endl;
continue;
}
if((x&y)==0)
{
cout<<0<<endl;
continue;
}
for(int i=0;i<60;i++)
{
if((1ll<<i)>y)
{
int u=(1ll<<i),v=x+u-y;
if((u&v)==0)
{
cout<<u-y<<endl;
break;
}
}
}
}
return 0;
}
D. Serval and Kaitenzushi Buffet
#include <bits/stdc++.h>
using namespace std;
#define int long long
int d[200005],h[200005];
bool cmp(int a,int b)
{
return d[a]>d[b];
}
struct t1
{
int l,r;
long long v,bj;
#define len(x) t[x].r-t[x].l+1
}t[4000005];
void build(int p,int l,int r)
{
t[p].l=l;
t[p].r=r;
t[p].bj=0;
if(l==r)
{
t[p].v=1;
return;
}
int mid=(l+r)>>1;
build(p*2,l,mid);
build(p*2+1,mid+1,r);
t[p].v=t[p*2].v+t[p*2+1].v;
}
void spread(int p)
{
if(t[p].bj)
{
t[p*2].bj=1;
t[p*2+1].bj=1;
t[p*2].v=0;
t[p*2+1].v=0;
t[p].bj=0;
}
}
void change(int p,int l,int r)
{
if(l<=t[p].l&&r>=t[p].r)
{
t[p].v=0;
t[p].bj=1;
return;
}
spread(p);
int mid=(t[p].l+t[p].r)>>1;
if(l<=mid)
{
change(p*2,l,r);
}
if(r>mid)
{
change(p*2+1,l,r);
}
t[p].v=t[p*2].v+t[p*2+1].v;
}
long long ask(int p,int l,int r)
{
if(l>r)
{
return 0;
}
if(l<=t[p].l&&r>=t[p].r)
{
return t[p].v;
}
spread(p);
int mid=(t[p].l+t[p].r)>>1;
long long va=0;
if(l<=mid)
{
va+=ask(p*2,l,r);
}
if(r>mid)
{
va+=ask(p*2+1,l,r);
}
return va;
}
int get(int p,int k)
{
if(t[p].l==t[p].r)
{
return t[p].l;
}
spread(p);
if(t[p*2].v>=k)
{
return get(p*2,k);
}
else
{
return get(p*2+1,k-t[p*2].v);
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--)
{
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>d[i];
h[i]=i;
}
sort(h+1,h+n+1,cmp);
build(1,1,n);
int ans=0;
for(int i=1;i<=n;i++)
{
if(ask(1,h[i],n)>=k+1)
{
int R=get(1,k+1+ask(1,1,h[i]-1));
change(1,h[i],R);
ans+=d[h[i]];
}
}
cout<<ans<<endl;
}
return 0;
}