4.15总结
GeorgeDeng · · 个人记录
4.15 考试总结。
A:B3865 [GESP202309 二级] 小杨的 X 字矩阵
这是一道大水题,考场上 A 了,循环输出即可。估计老师是用来防爆 0 的
code:
#include <iostream>
using namespace std;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n;
cin>>n;
for(int i = 1;i<=n;i++){
for(int j = 1;j<=n;j++){
if(i==j) cout<<"+";
else if(i==n-j+1){
cout<<"+";
}else cout<<"-";
}
cout<<endl;
}
return 0;
}
B:B2160 病人排队
这个赛场上也 A 了。就是一道结构体排序问题。cmp 函数按照题目模拟,没看题导致卡了好久。
code:
#include <iostream>
#include <algorithm>
using namespace std;
struct node{
string s;
int age;
int id;
};
node kkk[100005];
int n;
bool cmp(node a,node b){
if(a.age<60&&b.age<60){
return a.id<b.id;
}
if(a.age!=b.age) return a.age>b.age;
return a.id<b.id;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n;
for(int i = 1;i<=n;i++){
cin>>kkk[i].s>>kkk[i].age;
kkk[i].id = i;
}
sort(kkk+1,kkk+1+n,cmp);
for(int i = 1;i<=n;i++){
cout<<kkk[i].s<<endl;
}
return 0;
}
C:P2126 Mzc家中的男家丁
这道题是最小生成树板子题。使用 kruskal 算法即可。
这道题赛场上没 A,因为我的并查集的 find 函数写错了,导致
code:
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 2305,M = 4e5+5;
int fa[N];
struct node{
int s,e,w;
}edge[M];
bool cmp(node a,node b){
return a.w<b.w;
}
int find(int x){
if(fa[x]==x) return x;
return fa[x] = find(fa[x]);
}
void unionn(int x,int y){
int tx = find(x),ty = find(y);
if(tx!=ty) fa[ty] = tx;//错在这里了,正确代码是祖宗连祖宗,可我是y连x。
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n,m;
cin>>n>>m;
for(int i = 1;i<=m;i++){
cin>>edge[i].s>>edge[i].e>>edge[i].w;
}
for(int i = 0;i<=n;i++){
fa[i] = i;
}
sort(edge+1,edge+1+m,cmp);
int ans = 0;
int cnt = 0;
for(int i = 1;i<=m;i++){
int tx = edge[i].s,ty = edge[i].e;
if(find(tx)==find(ty)) continue;
ans+=edge[i].w;
cnt++;
unionn(tx,ty);
if(cnt==n) break;
}
cout<<ans;
return 0;
}
D:P1806 跑步
这道题其实没有什么技术含量。
首先,我们先把暴力搜索写出来,发现样例都会 TLE。然后发现有重复状态出现,于是用 vis 数组标记当前状态下的答案。搜索完成之后注意答案要
这道题 A 了。
#include <iostream>
#define int long long
using namespace std;
int n;
int dp[505][505];
int dfs(int now,int prev){
if(dp[now][prev]) return dp[now][prev];
if(now==n){
dp[now][prev] = 1;
return dp[now][prev];
}
for(int i = prev+1;i+now<=n;i++){
dp[now][prev]+=dfs(now+i,i);
}
return dp[now][prev];
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n;
cout<<dfs(0,0)-1;
return 0;
}
E:P1702 突击考试
这道题可以说是优雅的暴力(可是不是莫队)。
这个其实非常好算。因为
我的错因是考场只想到写暴力了,所以没 A。
code:
#include <iostream>
using namespace std;
int th[100005][2];
int n;
int a[100005];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n;
for(int i = 1;i<=n;i++){
cin>>th[i][0]>>th[i][1];
}
for(int k = 1;k<=5;k++){
int ans = 0;
int cnt = 0;
for(int i = 1;i<=n;i++){
if(th[i][0]==k||th[i][1]==k) cnt++;
else{
ans = max(ans,cnt);
cnt = 0;
}
}
ans = max(ans,cnt);
a[k] = ans;
}
int maxn = 0;
int finald = 0;
for(int i = 1;i<=5;i++){
if(maxn<a[i]){
maxn = a[i];
finald = i;
}
}
cout<<maxn<<' '<<finald;
return 0;
}
F:P11005 [蓝桥杯 2024 省 Python B] 缴纳过路费
这道题赛场上没有人 A。
这道题就是最小生成树修改一点地方。把边从小到大排序,然后遍历。如果符合要求,就计算答案并连边,否则直接连边。
这道题考场上会都不会好吧。
code:
#include <iostream>
#include <algorithm>
#define itn int
using namespace std;
int n,m,l,r;
int fa[100005];
int siz[100005];
struct node{
int u,v,w;
}e[200005];
bool cmp(node a,node b){
return a.w<b.w;
}
int find(int x){
if(fa[x]==x) return x;
return fa[x] = find(fa[x]);
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>m>>l>>r;
for(int i = 1;i<=n;i++) fa[i] = i;
for(int i = 1;i<=n;i++) siz[i] = 1;
for(itn i = 1;i<=m;i++){
cin>>e[i].u>>e[i].v>>e[i].w;
}
sort(e+1,e+1+m,cmp);
int ans = 0;
for(int i = 1;i<=m;i++){
int tx = find(e[i].u),ty = find(e[i].v);
if(tx!=ty){
if(e[i].w>=l&&e[i].w<=r){
ans+=siz[tx]*siz[ty];
}
fa[tx] = ty;
siz[ty]+=siz[tx];
}
}
cout<<ans;
return 0;
}