总结
习题课(兴趣小组)
Not Adjacent Matrix
题意
将一个的格子分别填上的数,使相邻的两个格子中的数不相邻。如果可以做到,输出矩阵,否则输出
知识点
构造
正解
设行为,列为。现将的格子依次填 上数字,剩余的接着填。
注意
是特例,无法做到,输出
错因
我好像没错
心得
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
int a[105][105];
int Q , n;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
memset (a , 0 , sizeof (a));
cin >> n;
if (n==2)
{
cout << -1 << "\n";
continue;
}
int now=0;
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
if ((i+j)%2==0)
{
now++;
a[i][j]=now;
}
}
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
if (a[i][j]==0)
{
now++;
a[i][j]=now;
}
cout << a[i][j] << " ";
}
cout << "\n";
}
}
return 0;
}Same Differences
题意
求有几对满足
知识点
方程,
正解
化简:,统计每个只出现多少次,再每个的数量选出两个的方案数的总和
错因
我好像没错
心得
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
map <int , int> a;
int Q , n , v;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
a.clear ( );
cin >> n;
for (int i=1;i<=n;i++)
{
cin >> v;
a[v-i]++;
}
long long cnt=0;
for (auto i:a)
{
cnt+=(long long) i.second*(i.second-1)/2;
}
cout << cnt << "\n";
}
return 0;
}Arranging The Sheep
题意
每个*可以往左边或者右边的.挪一个。求至少要挪几下才能使所有的*连在一起
知识点
贪心
正解
有个*那么所有的*全部往第点靠拢,再计算即可
错因
我好像没错
心得
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
string s;
int cnt[1000005];
int Q , n;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
cin >> n >> s;
s=" "+s;
int tot=0;
for (int i=1;i<=n;i++)
{
if (s[i]=='*')
{
cnt[++tot]=i;
}
}
long long ans=0 , S=(tot+1)/2;
int res=cnt[S];
for (int i=1;i<=S-1;i++)
{
ans+=(res-(S-i))-cnt[i];
}
for (int i=S+1;i<=tot;i++)
{
ans+=cnt[i]-(res+(i-S));
}
cout << ans << "\n";
}
return 0;
}Corrupted Array
题意
先有一个数组,有个数下标分别为,=,为随机抽取。将打乱得到数组。已知数组,求任意一个可能的数组
知识点
思想,
正解
只可能为个数中,所以,最后判断减去两倍后是否有剩下的那个数
错因
我好像没错
心得
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[200005];
map <int , int> mp1 , mp2;
int Q , n;
signed main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
mp1.clear ( );
mp2.clear ( );
cin >> n;
n+=2;
long long sum=0;
for (int i=1;i<=n;i++)
{
cin >> a[i];
sum+=a[i];
}
sort (a+1 , a+n+1);
for (int i=1;i<=n-1;i++)
{
mp1[a[i]]++;
}
for (int i=1;i<=n-2;i++)
{
mp2[a[i]]++;
}
mp2[a[n]]++;
if (mp1.count (sum-a[n]*2))
{
mp1[sum-a[n]*2]--;
for (auto i:mp1)
{
for (int j=1;j<=i.second;j++)
{
cout << i.first << " ";
}
}
}
else if (mp2.count (sum-a[n-1]*2))
{
mp2[sum-a[n-1]*2]--;
for (auto i:mp2)
{
for (int j=1;j<=i.second;j++)
{
cout << i.first << " ";
}
}
}
else
{
cout << -1 << " ";
}
cout << "\n";
}
return 0;
}Permutation by Sum
题意
构造一个的排列使,无法构造输出
知识点
思想
正解
毕竟,,完全可以用的方法。如果填最小的还是多了或者填最大的还是小了,输出,其他情况都能满足。先将中的每一个数分别,次操作,每次操作最后一个满足条件的数,就,最后将没出现的数分配到两边即可
错因
两边都要来一次,我的数没增加。发了,这时:比赛已经结束了
心得
应该要写出来的
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
bool vis[505];
int b[505];
int Q , n , l , r , s , tot;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
cin >> n >> l >> r >> s;
long long sum=0;
for (int i=l;i<=r;i++)
{
b[i]=i-l+1;
sum+=b[i];
}
if (sum>s)
{
cout << -1 << "\n";
}
else
{
int now=r;
int len=r-l+1;
while (sum<s)
{
if (b[now]==n-(r-now)) now--;
if (now<l) break;
b[now]++;
sum++;
}
if (sum==s)
{
memset (vis , 0 , sizeof (vis));
for (int i=l;i<=r;i++)
{
vis[b[i]]=1;
}
int tot=1;
for (int i=1;i<=l-1;i++)
{
while (vis[tot]==1)
{
tot++;
}
cout << tot << " ";
tot++;
}
for (int i=l;i<=r;i++)
{
cout << b[i] << " ";
}
for (int i=r+1;i<=n;i++)
{
while (vis[tot]==1)
{
tot++;
}
cout << tot << " ";
tot++;
}
cout << "\n";
}
else
{
cout << -1 << "\n";
}
}
}
return 0;
}Education
题意
等级为是一天赚元,每天可以扣升一级,求最少要几天能让手中的钱
知识点
贪心
正解
记下至少要多少天才能升到级和剩余的钱数,再递推取
错因
没有做
心得
每道题都要做到
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
int a[200005] , b[200005];
int Q , n , c;
int CNT (int x , int y)
{
return (x+y-1)/y;
}
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
cin >> n >> c;
long long minn=1e18 , now=0 , sum=0;
for (int i=1;i<=n;i++)
{
cin >> a[i];
}
for (int i=1;i<=n-1;i++)
{
cin >> b[i];
}
for (int i=1;i<=n-1;i++)
{
minn=min (minn , now+CNT (c-sum , a[i]));
int cnt=CNT (b[i]-sum , a[i]);
now+=cnt+1;
sum=sum+cnt*a[i]-b[i];
// cout << now << " " << cnt << " " << sum << "\n";
}
minn=min (minn , now+CNT (c-sum , a[n]));
cout << minn << "\n";
}
return 0;
}Short Task
题意
设为所有因数和,求最小的使得
正解
筛法
正解
用筛法求出每个数的因数,再用数组记录即可
错因
我好像没错
心得
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
const int M=1e7;
int sum[M+5];
int minn[M+5];
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
memset (minn , 0x3f , sizeof (minn));
for (int i=1;i<=M;i++)
{
for (int j=i;j<=M;j+=i)
{
sum[j]+=i;
}
}
for (int i=1;i<=10000000;i++)
{
if (sum[i]<=M&&minn[sum[i]]>i)
{
minn[sum[i]]=i;
}
}
int Q , n;
cin >> Q;
while (Q--)
{
cin >> n;
if (minn[n]<=1e9) cout << minn[n] << "\n";
else cout << "-1\n";
}
return 0;
}
///新课
P3366 【模板】最小生成树
或板子题
代码
#include <bits/stdc++.h>
using namespace std;
struct node
{
int x , y , z;
};
node v[200005];
int fa[5005];
int n , m , x , y , z;
int sum=0 , cnt;
int find (int x)
{
if (fa[x]==x) return x;
return fa[x]=find (fa[x]);
}
bool cmp (node x , node y)
{
return x.z<y.z;
}
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
cnt=n;
for (int i=1;i<=n;i++)
{
fa[i]=i;
}
for (int i=1;i<=m;i++)
{
cin >> v[i].x >> v[i].y >> v[i].z;
}
sort (v+1 , v+m+1 , cmp);
for (int i=1;i<=m;i++)
{
int nx=find (v[i].x) , ny=find (v[i].y);
if (nx==ny) continue;
fa[ny]=nx;
cnt--;
sum+=v[i].z;
if (cnt==1)
{
cout << sum;
return 0;
}
}
cout << "orz";
return 0;
}P2330 [SCOI2005] 繁忙的都市
板子题,再加上记录最大值即可
代码
#include <bits/stdc++.h>
using namespace std;
struct node
{
int x , y , z;
};
node v[8008];
int fa[305];
int n , m , x , y , z;
int sum=0 , cnt , p=0;
int find (int x)
{
if (fa[x]==x) return x;
return fa[x]=find (fa[x]);
}
bool cmp (node x , node y)
{
return x.z<y.z;
}
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
cnt=n;
for (int i=1;i<=n;i++)
{
fa[i]=i;
}
for (int i=1;i<=m;i++)
{
cin >> v[i].x >> v[i].y >> v[i].z;
p+=v[i].z;
}
sort (v+1 , v+m+1 , cmp);
for (int i=1;i<=m;i++)
{
int nx=find (v[i].x) , ny=find (v[i].y);
if (nx==ny) continue;
fa[ny]=nx;
cnt--;
sum++;
if (cnt==1)
{
cout << sum << " " << v[i].z;
return 0;
}
}
return 0;
}
/**
* @runId: 67228
* @language: C++
* @author: 伍位泽
* @submitTime: 2026-05-31 14:43:19
*/P4047 [JSOI2010] 部落划分
因为题目要让剩下的联通块距离尽量大,用跑一边,再找两个有边且不在同一联通快的点,输出最小值
代码
#include <bits/stdc++.h>
using namespace std;
struct node
{
int x , y , z;
};
node v[1000005];
int fa[1005] , x[1005] , y[1005];
int n , k , tot;
int find (int x)
{
if (fa[x]==x) return x;
return fa[x]=find (fa[x]);
}
bool cmp (node x , node y)
{
return x.z<y.z;
}
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i=1;i<=n;i++)
{
cin >> x[i] >> y[i];
fa[i]=i;
for (int j=1;j<=i-1;j++)
{
int dx=abs (x[i]-x[j]) , dy=abs(y[i]-y[j]);
v[++tot]={i , j , dx*dx+dy*dy};
}
}
sort (v+1 , v+tot+1 , cmp);
int maxn=0 , cnt=n;
for (int i=1;i<=tot;i++)
{
int nx=find (v[i].x) , ny=find (v[i].y);
if (nx==ny) continue;
fa[ny]=nx;
cnt--;
if (cnt==k) break;
}
for (int i=1;i<=tot;i++)
{
int dx=find (v[i].x) , dy=find (v[i].y);
if (dx==dy) continue;
maxn=v[i].z;
break;
}
printf ("%.2lf" , sqrt (maxn*1.0));
return 0;
}P1546 [USACO3.1] 最短网络 Agri-Net
还是一样的,只不过每两个点都连边了。套或板子就行
代码
#include <bits/stdc++.h>
using namespace std;
struct node
{
int x , y , z;
};
node v[10005];
int fa[2005];
int n , m , x , y , z , tot;
int find (int x)
{
if (fa[x]==x) return x;
return fa[x]=find (fa[x]);
}
bool cmp (node x , node y)
{
return x.z<y.z;
}
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i=1;i<=n;i++) fa[i]=i;
while (m--)
{
cin >> x >> y >> z;
v[++tot]={x , y , z};
}
sort (v+1 , v+tot+1 , cmp);
long long maxn=0;
for (int i=1;i<=tot;i++)
{
int dx=find (v[i].x) , dy=find (v[i].y);
if (dx==dy) continue;
maxn=v[i].z;
fa[dy]=dx;
}
cout << maxn;
return 0;
}习题课
Make it Increasing
题意
开始,每次可以,求最少要多少次才能使
知识点
枚举
正解
枚举最后哪个点还是,再求最少次数取
错因
我好像没错
心得
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[5005];
int n , ans=LONG_LONG_MAX;
int CNT (int x , int y)
{
return x/y+1;
}
int cishu (int now)
{
int cnt=0 , lst;
lst=0;
for (int i=now+1;i<=n;i++)
{
int res=CNT (lst , a[i]);
cnt+=res;
lst=res*a[i];
}
lst=0;
for (int i=now-1;i>=1;i--)
{
int res=CNT (lst , a[i]);
cnt+=res;
lst=res*a[i];
}
return cnt;
}
signed main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i=1;i<=n;i++)
{
cin >> a[i];
}
for (int i=1;i<=n;i++)
{
ans=min (ans , cishu (i));
}
cout << ans << "\n";
return 0;
}
//The Enchanted Forest
题意
每次格子上都会,求最多能拿到几个
知识点
分类讨论
正解
简单分析即可
错因
我好像没错
心得
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int s[200005];
int T , n , k , v , ans=0;
signed main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> T;
while (T--)
{
cin >> n >> k;
for (int i=1;i<=n;i++)
{
cin >> v;
s[i]=s[i-1]+v;
}
if (k>n)
{
cout << ((k-1+k-n)*n/2)+s[n] << '\n';
}
else
{
ans=0;
for (int i=k;i<=n;i++)
{
ans=max (ans , (s[i]-s[i-k])+((k-1)*k/2));
}
cout << ans << "\n";
}
}
return 0;
}
//Make Them Equal
题意
,每次可以将其中一个,每当一个,在操作次的情况下,求最大价值
知识点
预处理,背包
正解
先处理出至少要几次才能到,再套用背包模版
注意
有可能较大,那么就可以直接输出价值总和,不用跑,否则会严重超时
错因
看错题了,以为是输入的一个变量
心得
不是特别难
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
int f[100005];
int dp[1000005];
int b[1005] , c[1005];
int Q , n , k;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
memset (f , 0x3f , sizeof (f));
f[1]=0;
for (int i=1;i<=1000;i++)
{
for (int j=1;j<=i;j++)
{
int pos=i+i/j;
f[pos]=min (f[pos] , f[i]+1);
}
}
cin >> Q;
while (Q--)
{
memset (dp , 0 , sizeof (dp));
cin >> n >> k;
int cnt=0 , sum=0;
for (int i=1;i<=n;i++)
{
cin >> b[i];
cnt+=f[b[i]];
}
for (int i=1;i<=n;i++)
{
cin >> c[i];
sum+=c[i];
}
if (cnt<=k)
{
cout << sum << "\n";
}
else
{
for (int i=1;i<=n;i++)
{
for (int j=k;j>=f[b[i]];j--)
{
dp[j]=max (dp[j] , dp[j-f[b[i]]]+c[i]);
}
}
cout << dp[k] << "\n";
}
}
return 0;
}Berpizza
题意
中输入:
1.,新来一个人,数值为 2.给未买单且最早来的顾客买单 3.给未买单且数值最大顾客买单
依次输出买单的人的编号(第几个来的)
知识点
,模拟
正解
用堆和标记数组模拟过程即可
错因
我好像没错
心得
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
struct node
{
int id , w;
bool operator < (const node &a) const
{
if (w!=a.w) return w<a.w;
return id>a.id;
}
};
priority_queue <node> q;
bool vis[500005];
int Q , op , m , tot , lst;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
for (int i=1;i<=Q;i++)
{
cin >> op;
if (op==1)
{
cin >> m;
tot++;
q.push ({tot , m});
}
else if (op==2)
{
for (int j=lst+1;j<=tot;j++)
{
if (vis[j]==0)
{
cout << j << " ";
vis[j]=1;
lst=j;
break;
}
}
}
else
{
while (!(q.empty ( )))
{
node p=q.top ( );
if (vis[p.id]==0)
{
cout << p.id << " ";
vis[p.id]=1;
break;
}
q.pop ( );
}
}
}
return 0;
}
//习题课(兴趣小组)
Another Problem About Dividing Numbers
题意
求是否能经过次相同
知识点
质因数分解
正解
求出最多最少要几次即可
错因
我好像没错
心得
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
int Q , a , b , k , x , y;
int minn (int a , int b)
{
int c=__gcd (a , b) , res=0;
if (a>c) res++;
if (b>c) res++;
return res;
}
int zys (int s)
{
int cnt=0;
for (int i=2;i*i<=s;i++)
{
while (s%i==0)
{
cnt++;
s/=i;
}
}
if (s>1) cnt++;
return cnt;
}
int maxn (int a , int b)
{
return zys (a)+zys (b);
}
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
cin >> a >> b >> k;
x=minn (a , b);
y=maxn (a , b);
if (a==b&&k==1)
{
cout << "No\n";
}
else if (k>=x&&k<=y)
{
cout << "Yes\n";
}
else
{
cout << "No\n";
}
}
return 0;
}
//