T1
本题非常简单,无任何算法,只需要写很长的代码即可。
#include<bits/stdc++.h>
#define int long long
using namespace std;
//const int N=;
int n=3,ans1,ans2;
char a[5][5];
bool o[35],t[35][35];
void check(char x,char y,char z)
{
if(x==y && y==z)
{
o[x-'A']=1;
return;
}
if(x==y)
{
t[x-'A'][z-'A']=true;
t[z-'A'][x-'A']=true;
}
if(y==z)
{
t[y-'A'][x-'A']=true;
t[x-'A'][y-'A']=true;
}
if(x==z)
{
t[x-'A'][y-'A']=true;
t[y-'A'][x-'A']=true;
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>a[i][j];
}
}
check(a[1][1],a[1][2],a[1][3]);
check(a[2][1],a[2][2],a[2][3]);
check(a[3][1],a[3][2],a[3][3]);
check(a[1][1],a[2][1],a[3][1]);
check(a[1][2],a[2][2],a[3][2]);
check(a[1][3],a[2][3],a[3][3]);
check(a[1][1],a[2][2],a[3][3]);
check(a[1][3],a[2][2],a[3][1]);
for(int i=0;i<=25;i++)
{
if(o[i])
{
ans1++;
}
}
for(int i=0;i<=25;i++)
{
for(int j=0;j<=25;j++)
{
if (t[i][j])
{
ans2++;
}
}
}
cout<<ans1<<'\n'<<ans2/2;
return 0;
}T2
本题需要一个优化:我们可以发现,确定一个矩形只需要四个坐标,两个坐标,两个坐标,也就是只需要枚举对角线上的点即可
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+10;
struct node {
int x, y;
}a[N];
bool cmp(node a, node b)
{
if(a.x != b.x) return a.x< b.x;
return a.y< b.y;
}
int n,ans;
map<pair<int, int>,bool>mp;
signed main()
{
cin>>n;
for (int i=1;i<=n;i++)
{
cin>>a[i].x>>a[i].y;
mp[{a[i].x,a[i].y}]=1;
}
sort(a+1,a+1+n,cmp);
for (int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++)
{
int x1=a[i].x,y1=a[i].y,x2=a[j].x,y2=a[j].y;
int x3=x2,y3=y1,x4=x1,y4=y2;
if (x1<x2 && y1<y2)
{
if(mp.count({x3,y3}) && mp.count({x4,y4}))
{
ans++;
}
}
}
}
cout<<ans;
return 0;
}T3
我们可以枚举,再枚举以为中心的区间(奇数长度或偶数长度),然后根据每次变化造成的影响统计结果。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=7505;
int n,a[N],b[N],ans[N],cnt,l,r;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=n;i++)
{
cin>>b[i];
}
for(int i=1;i<=n;i++)
{
if(a[i]==b[i])
{
cnt++;
}
}
for(int i=1;i<=n;i++)
{
int tmp=cnt;
l=i,r=i;
while(l>=1 && r<=n)
{
if(a[l]==b[l] && a[l]!=b[r])
{
tmp--;
}
if(a[r]==b[r] && a[r]!=b[l])
{
tmp--;
}
if(a[l]!=b[l] && a[l]==b[r])
{
tmp++;
}
if(a[r]!=b[r] && a[r]==b[l])
{
tmp++;
}
l--,r++;
ans[tmp]++;
}
l=i,r=i+1;
tmp=cnt;
while(l>=1 && r<=n)
{
if(a[l]==b[l] && a[l]!=b[r])
{
tmp--;
}
if(a[r]==b[r] && a[r]!=b[l])
{
tmp--;
}
if(a[l]!=b[l] && a[l]==b[r])
{
tmp++;
}
if(a[r]!=b[r] && a[r]==b[l])
{
tmp++;
}
l--,r++;
ans[tmp]++;
}
}
for(int i=0;i<=n;i++)
{
cout<<ans[i]<<'\n';
}
return 0;
}T4
本题同上题枚举以为中心的区间,枚举区间,用求出区间长度取最小值得到答案。注意,因为表示长度为时的答案,可以发现,对于任意的,
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e3+5;
int n,a[N],t[N];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
memset(t,0x3f,sizeof(t));
t[1]=0;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=2;i<n;i++)
{
int l=i,r=i,sum=0;
while(l>=1 && r<=n)
{
sum+=abs(a[l]-a[r]);
t[r-l+1]=min(t[r-l+1],sum);
l--,r++;
}
}
for(int i=1;i<n;i++)
{
int l=i,r=i+1,sum=0;
while(l>=1 && r<=n)
{
sum+=abs(a[l]-a[r]);
t[r-l+1]=min(t[r-l+1],sum);
l--,r++;
}
}
t[1]=0;
for(int i=1;i<=n;i++)
{
cout<<t[i]<<' ';
}
return 0;
}T5
本题按每一秒模拟次,可以得到很简单的思路: 枚举每一秒 让居民走一步 判断重叠并标记 输出 注意,重叠只需要判断相邻的是否重叠 本解法可得分。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=100005;
int n,q,t,x,a[N],r[N],pos[N];
bool stop[N];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n>>t>>q;
for(int i=1;i<=n;i++)
{
cin>>a[i]>>r[i];
pos[i]=a[i];
stop[i]=false;
}
for(int i=1;i<=t;i++)
{
for(int j=1;j<=n;j++)
{
if(!stop[j])
{
if(r[j]==1)
{
pos[j]++;
}
else
{
pos[j]--;
}
}
}
for(int j=1;j<n;j++)
{
if(pos[j]==pos[j+1])
{
stop[j]=true;
stop[j+1]=true;
}
}
}
while(q--)
{
cin>>x;
cout<<pos[x]<<'\n';
}
return 0;
}