高一下三调2
T1攻击装置
题目
脑瘫了,考场上连边时 \((i-1)*n+j\) 写成 \(i*n+j\),连边直接错位
点击查看代码
#include<bits/stdc++.h>
const int maxn=4e4+10;
using namespace std;
int n,head[maxn<<3],nxt[maxn<<4],to[maxn<<4],vis[maxn],match[maxn],tot;
int a[500][500],sum,ans;
string s[300];
void add(int x,int y)
{
to[++tot]=y;
nxt[tot]=head[x];
head[x]=tot;
}
bool dfs(int u)
{
for(int i=head[u];i;i=nxt[i])
{
int y=to[i];
if(vis[y])continue;
vis[y]=1;
if(!match[y]||dfs(match[y]))
{
match[y]=u;
return 1;
}
}
return 0;
}
int main()
{
freopen("attack.in","r",stdin);
freopen("attack.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
cin>>s[i];
for(int j=0;j<n;j++)
{
if(s[i][j]=='0')a[i][j+1]=1,sum++;
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][j]==1)
{
if(a[i-2][j-1]==1)add((i-1)*n+j,(i-3)*n+j-1);
if(a[i-1][j-2]==1)add((i-1)*n+j,(i-2)*n+j-2);
if(a[i-2][j+1]==1)add((i-1)*n+j,(i-3)*n+j+1);
if(a[i-1][j+2]==1)add((i-1)*n+j,(i-2)*n+j+2);
if(a[i+2][j-1]==1)add((i-1)*n+j,(i+1)*n+j-1);
if(a[i+1][j-2]==1)add((i-1)*n+j,(i)*n+j-2);
if(a[i+2][j+1]==1)add((i-1)*n+j,(i+1)*n+j+1);
if(a[i+1][j+2]==1)add((i-1)*n+j,(i)*n+j+2);
}
}
}
for(int i=1;i<=n*n;i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i))ans++;
}
printf("%d\n",sum-ans/2);
return 0;
}
/*
3
010
000
100
*/
T2循环
题目
水题
点击查看代码
#include<bits/stdc++.h>
const int maxn=1e3+10;
const int mod=100;
int x,vis[maxn];
int main()
{
freopen("number.in","r",stdin);
freopen("number.out","w",stdout);
scanf("%d",&x);
x%=100;
int flag=1,ans=1;
while(flag)
{
ans=ans*x%100;
printf("%d ",ans);
if(vis[ans])flag=0;
vis[ans]=1;
}
return 0;
}
T3漫步
题目
也是水题,但我想复杂了,打了个贪心的暴力,T俩点
贪心——80pts
#include<bits/stdc++.h>
const int maxn=1e5+10;
using namespace std;
int n,st[maxn],ans,rs,minn=1e10;
struct node{int x,v;}m[maxn];
bool cmp(node a,node b)
{
return a.x<b.x;
}
int main()
{
freopen("jog.in","r",stdin);
freopen("jog.out","w",stdout);
scanf("%d",&n);
for(register int i=1;i<=n;i++)
{
scanf("%lld%lld",&m[i].x,&m[i].v);
minn=min(minn,m[i].v);
}
sort(m+1,m+n+1,cmp);
for(register int i=1;i<=n;i++)
{
if(m[i].v==minn)st[++rs]=i;
}
ans+=rs;
int end=st[rs];
while(minn!=m[n].v&&minn!=1e10)
{
minn=1e10;
memset(st,0,sizeof st);
rs=0;
for(register int i=end+1;i<=n;i++)
{
minn=min(minn,m[i].v);
}
for(register int i=end+1;i<=n;i++)
{
if(m[i].v==minn)st[++rs]=i;
}
sort(st+1,st+1+rs);
end=st[rs];
ans+=rs;
if(end==n)break;
}
printf("%d\n",ans);
return 0;
}
/*
5
0 1
1 2
2 3
3 2
6 1
*/
正解
#include<bits/stdc++.h>
#define int long long
const int maxn=1e5+10;
using namespace std;
int n,st[maxn],ans,rs,minn=1e10;
struct node{int x,v;}m[maxn];
bool cmp(node a,node b)
{
return a.x<b.x;
}
signed main()
{
freopen("jog.in","r",stdin);
freopen("jog.out","w",stdout);
scanf("%lld",&n);
ans=n;
for(int i=1;i<=n;i++)
{
scanf("%lld%lld",&m[i].x,&m[i].v);
}
sort(m+1,m+n+1,cmp);
for(int i=n-1;i>=1;i--)
{
if(m[i].v>m[i+1].v)ans--,m[i].v=m[i+1].v;
}
printf("%d\n",ans);
return 0;
}
/*
5
0 1
1 2
2 3
3 2
6 1
*/
T4穿越
题目
当时以为是个搜索,也确实是个搜索,但没打出来,骗了40pts走了
点击查看代码
#include<bits/stdc++.h>
#define fi first
#define se second
const int maxn=1e5+10;
using namespace std;
int n,m,a[400][400],aa,yu[400][400],p,x,y,b,z,h,lsx;
typedef pair<int,int> pii;
vector<pii>q[400][400],ch;
queue<pair<pii,int> >qq;
bool vis[400][400];
void bfs(int x,int y,int t)
{
// cout<<"!"<<endl;
if(x<=0||x>n||y<=0||y>m||vis[x][y]==1||a[x][y]==1||t>=yu[x][y])return;
if(a[x][y]!=2)
{
qq.push({{x,y},t});
vis[x][y]=1;
if(x==n&&y==m)
{
printf("%d\n",t);
exit(0);
}
}
else
{
for(auto i:q[x][y])
{
if(i.fi<=t&&i.se>=t)return;
}
qq.push({{x,y},t});
vis[x][y]=1;
return;
}
}
int main()
{
freopen("cross.in","r",stdin);
freopen("cross.out","w",stdout);
scanf("%d%d",&n,&m);
memset(yu,0x7f7f7f,sizeof yu);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==3)ch.push_back({i,j});
}
scanf("%d",&aa);
for(int i=1;i<=aa;i++)
{
scanf("%d",&lsx);
scanf("%d",&p);
for(int j=1;j<=p;j++)
scanf("%d%d",&x,&y),yu[x][y]=min(lsx,yu[x][y]);
}
scanf("%d",&b);
for(int i=1;i<=b;i++)scanf("%d%d%d%d",&x,&y,&z,&h),q[z][h].push_back({x,y});
if(a==0&&b==0)printf("%d",n+m-2),exit(0);
qq.push({{1,1},0});
vis[1][1]=1;
while(1)
{
pii k=qq.front().fi;
int x=k.fi,y=k.se,t=qq.front().se;
qq.pop();
vis[x][y]=0;
bfs(x+1,y,t+1);bfs(x-1,y,t+1);bfs(x,y+1,t+1);bfs(x,y-1,t+1);bfs(x,y,t+1);
if(a[x][y]==3)
{
for(auto i:ch)bfs(i.fi,i.se,t+2);
}
}
return 0;
}
/*
3 3
0 1 0
2 0 1
3 2 0
2
2 1 3 1
1 1 1 3
1
2 4 2 1
*/
T5结队
题目
早上吃饭时还在说会不会考并查集,说中了,但当时却没想到。。。
点击查看代码
#include<bits/stdc++.h>
const int maxn=1e5+10;
using namespace std;
int A,B,P,zs[maxn],cnt,ans,v[maxn],fa[maxn];
bool vis[maxn];
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
void pre()
{
for(int i=2;i<=sqrt(B);i++)
{
if(!vis[i])
{
for(int j=2;j*i<=B;j++)
vis[j*i]=1;
}
}
}
int main()
{
freopen("merge.in","r",stdin);
freopen("merge.out","w",stdout);
scanf("%d%d%d",&A,&B,&P);
pre();
for(int i=P;i<=B;i++)
if(!vis[i])zs[++cnt]=i,fa[i]=i;
for(int i=1;i<=cnt;i++)
{
for(int j=1;j*zs[i]<=B;j++)
{
int x=j*zs[i];
if(x<A)continue;
if(fa[x]&&find(x)!=x)fa[find(x)]=zs[i];
if(fa[x]==0)fa[x]=zs[i];
}
}
for(int i=A;i<=B;i++)
{
int x=find(i);
if(!v[x]&&x)ans++,v[x]=1;
if(x==0)ans++;
}
printf("%d\n",ans);
return 0;
}