A
模拟即可
# include <bits/stdc++.h>
using namespace std;
# define int long long
signed main()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
if(s=="101")
{
cout<<"NO"<<endl;
continue;
}
if(s.size()<=2)
{
cout<<"NO"<<endl;
continue;
}
bool st=0;
int n=s.size();
string s1=s.substr(0,2);
if(s1=="10")
st=1;
string s2=s.substr(2,s.size());
if(s2[0]!='0'&&st)
cout<<"YES";
else cout<<"NO";
cout<<endl;
}
}
B
# include <bits/stdc++.h>
using namespace std;
# define int long long
const int N=2e5+10;
signed main()
{
int t;
cin>>t;
while(t--)
{
int n;
int a[N]={0},b[N]={0};
bool st1=0;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
b[a[1]]=1;
for(int i=2;i<=n;i++)
{
bool st=0;
if(a[i]-1>=1)
{
if(b[a[i]-1]==1)
st=1,b[a[i]]=1;
}
if(a[i]+1<=n&&!st)
{
if(b[a[i]+1]==1)
st=1,b[a[i]]=1;
}
if(!st)
{
st1=1;
break;
}
}
if(st1)
cout<<"NO";
else cout<<"YES";
cout<<endl;
}
}
C 用结构体存值和编号然后排序遍历一遍寻找相同的记录下来编号
# include <bits/stdc++.h>
using namespace std;
# define int long long
#define x first
#define y second
const int N=2e5+10;
int n,m;
struct node
{
int w,id;
}a[N];
struct node1
{
int id;
char s;
}b[N];
bool cmp(node a,node b)
{
return a.w<b.w;
}
bool cmp1(node1 a,node1 b)
{
return a.s<b.s;
}
signed main()
{
int T;
cin>>T;
while(T--)
{
int c[N]={0};
vector<pair<int,int>>v;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i].w,a[i].id=i,c[i]=a[i].w;
sort(a+1,a+n+1,cmp);
for(int i=1;i<n;i++)
{
if(a[i].w==a[i+1].w)
v.push_back({a[i].id,a[i+1].id});
}
cin>>m;
while(m--)
{
vector<pair<int,int>>v1;
bool st1=0,st2=0;
string s;
cin>>s;
if(s.size()!=n)
{
cout<<"NO"<<endl;
continue;
}
for(auto t:v)
{
//cout<<t.x<<" "<<t.y<<endl;
if(s[t.x-1]!=s[t.y-1])
st1=1;
}
for(int i=0;i<s.size();i++)
{
b[i+1].s=s[i];
b[i+1].id=i+1;
}
sort(b+1,b+1+s.size(),cmp1);
for(int i=1;i<s.size();i++)
{
if(b[i].s==b[i+1].s)
v1.push_back({b[i].id,b[i+1].id});
}
for(auto t:v1)
{
//cout<<t.x<<" "<<t.y<<endl;
if(c[t.x]!=c[t.y])
st2=1;
}
//cout<<st1<<st2<<endl;
if(!st1&&!st2)
cout<<"YES";
else cout<<"NO";
cout<<endl;
}
}
}
D
考虑贪心区间覆盖的越大值越大,所以利用两个指针扫描一遍即可
# include <bits/stdc++.h>
using namespace std;
# define int long long
#define x first
#define y second
const int N=2e5+10;
int n;
struct node
{
int w;
char s;
}a[N];
signed main()
{
int T;
cin>>T;
while(T--)
{
int sum=0;
cin>>n;
int b[N]={0};
for(int i=1;i<=n;i++)
cin>>a[i].w,b[i]+=b[i-1]+a[i].w;
string s1;
cin>>s1;
for(int i=0;i<n;i++)
a[i+1].s=s1[i];
int l=1,r=n;
while(l<r)
{
if(a[l].s=='L'&&a[r].s=='R')
{
sum+=b[r]-b[l-1],l++,r--;
continue;
}
if(a[l].s=='R')l++;
else if(a[r].s=='L')r--;
}
cout<<sum<<endl;
}
}
E
把相对较高的猩猩放到被计算次数较多也就是中间的格子,具体计算多少次也要算出来,所以直接差分处理一下
# include <bits/stdc++.h>
using namespace std;
# define int long long
#define x first
#define y second
signed main()
{
int n,m,k;
cin>>n>>m>>k;
int w;
cin>>w;
vector<int> a(w+1);
for(int i=1;i<=w;i++)
{
cin>>a[i];
}
sort(a.begin()+1,a.end());
int d[n+k+1][m+k+1];
memset(d,0,sizeof(d));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
int x2=i+k-1,y2=j+k-1;
if(x2>n or y2>m)
continue;
d[i][j]++;
d[i][y2+1]--;
d[x2+1][j]--;
d[x2+1][y2+1]++;
}
}
priority_queue<int> q;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
d[i][j]+=d[i-1][j]+d[i][j-1]-d[i-1][j-1];
q.push(d[i][j]);
}
}
int ans=0;
for(int i=w;i>=1 and !q.empty();i--)
{
ans+=q.top()*a[i];
q.pop();
}
printf("%lld\n",ans);
}