【题解】10-3入门组模拟

(由于没多少空闲时间了,所以只能草草贴一下代码了)

A.八百标兵奔北坡

lnk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Problem:Luogu P4771 八百标兵奔北坡

#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
int a[1010][1010],dp[1010][1010];
int n,m,q;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
memset(dp,0x3f,sizeof(dp));
cin>>n>>m>>q;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j],dp[i][j]=inf;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(a[i][j]>=a[i+1][j]&&
a[i][j]>=a[i-1][j]&&
a[i][j]>=a[i][j-1]&&
a[i][j]>=a[i][j+1])
dp[i][j]=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if(dp[i-1][j+1]!=inf)dp[i][j]=min(dp[i][j],dp[i-1][j+1]+1);
if(dp[i-1][j]!=inf)dp[i][j]=min(dp[i][j],dp[i-1][j]+1);
if(dp[i-1][j-1]!=inf)dp[i][j]=min(dp[i][j],dp[i-1][j-1]+1);
}
for(int i=1;i<=q;i++)
{
int x,y;
cin>>x>>y;
if(dp[x][y]!=inf)cout<<dp[x][y]<<'\n';
else cout<<"Pool Babingbaboom!\n";
}
}

B.[IOI 1996 / USACO3.3] 游戏 A Game

lnk

博弈论,感觉很有趣……(但是想不出来啊)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Problem:Luogu P2734 [IOI 1996 / USACO3.3] 游戏 A Game

#include<bits/stdc++.h>
using namespace std;
int n,dp[110][110],sum[110];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>dp[i][i];
sum[i]=sum[i-1]+dp[i][i];
}
for(int len=2;len<=n;len++)
for(int i=1;i<=n-len+1;i++)
{
int j=i+len-1;
int s=sum[j]-sum[i-1];
dp[i][j]=max(s-dp[i+1][j],s-dp[i][j-1]);
}
cout<<dp[1][n]<<" "<<sum[n]-dp[1][n];
return 0;
}

C.角色属性树

lnk

真的没想到这题硬膜拟能过,而且$\color{green} 绿题$

当时没做出来看到正解是模拟$\Huge \color{red} 红温$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Problem:Luogu P2441 角色属性树

#include<bits/stdc++.h>
using namespace std;
const int MAXN=200010;
int n,k,fa[MAXN],m[MAXN];
int dfs(int u,int v)
{
if(u==0)return -1;
if(__gcd(m[u],m[v])>1)return u;
return dfs(fa[u],v);
}
int main()
{
cin>>n>>k;
for(int i=1;i<=n;i++)cin>>m[i];
for(int i=1;i<n;i++)
{
int u,v;
cin>>u>>v;
fa[v]=u;
}
for(int i=1;i<=k;i++)
{
int opt,u;
cin>>opt>>u;
if(opt==1)
cout<<dfs(fa[u],u)<<'\n';
else
{
int val;
cin>>val;
m[u]=val;
}
}
}

D. [USACO17DEC] Greedy Gift Takers P

因为如果有一头牛拿不到礼物,它后面的牛也拿不到,所以这题可以二分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Problem:Luogu P4090 [USACO17DEC] Greedy Gift Takers P

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN=100010;
int a[MAXN],b[MAXN],n;
bool check(int x)
{
for(int i=1;i<x;i++)b[i]=a[i];
sort(b+1,b+x);
int tmp=n-x;
for(int i=1;i<x;i++)
{
if(b[i]>tmp)return 0;
tmp++;
}
return 1;
}
signed main()
{
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
int l=1,r=n;
while(l<=r)
{
int mid=(l+r)>>1;
if(check(mid))l=mid+1;
else r=mid-1;
}
cout<<n-r;
return 0;
}

【题解】10-3入门组模拟
http://j27egu.github.io/2025/10/04/【题解】10-3入门组模拟/
作者
j27eGU
发布于
2025年10月4日
许可协议