作业-2.24-小六集训队-407
wangsy2014 · · 个人记录
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
本题按每一秒模拟