2021年PAT冬季甲级题解

前言:
2021/12/19:PAT甲级冬季考试
前前后后准备一个月叭
前三题比较简单,一个小时左右拿下了
最后一题读题不太懂,还是英语不太行吧,然后反复读也找不出坑点,最后92低分飘过。
感慨:哎没能拿到徽章,满分还是有难度的。感觉自己准备还是不够充分,考前一两个星期都没怎么刷题了——因为部分课程结课得实现小游戏、app inventor实现的飞机大战,感觉还是麻烦点的(花了两天学了点皮毛做了俩游戏,就开摆了。)还有框架代码需要搞,准备英语四级(都江堰说明年我还得来,阅读这次写的慢也错很多,反正寄了问题很多)。
然后,马上要考试了,准备好好复习辣(自己啥都不会哎,不能再摆烂了我。

在这里插入图片描述

贴上考试写的代码:

T1

哈希表

#include<bits/stdc++.h>

using namespace std;

const int N = 1e5+10;

/*

*/

int n,m;
int a[N],b[N],c[N];
int cnt[N];

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> m;
	for(int i = 0; i < m; i ++ ){
		map<int,int> hs;
		for(int j = 1; j <= n; j ++ ){
			cin>>a[j];
			hs[a[j]] ++;
		}
		int mx = -1;
		for(int j = 1; j <= n; j ++ ){
			b[j] = n - hs[a[j]];
			c[j] = n - hs[a[j]];
			mx = max(b[j],mx);
		} 
		for(int j = 1; j <= n; j ++ ){
			if(mx == c[j]) cnt[j]++;
		}
	}
	int mx = -1e9,idx = -1;
	for(int i = 1; i <= n; i ++ ){
		//cout<<cnt[i]<<" ";
		if(mx < cnt[i]) mx = cnt[i],idx = i;
	}
	//puts("");
	cout<<idx;
	return 0;
}

T2

模拟链表

#include<bits/stdc++.h>

using namespace std;

const int N = 1e5+10;

/*
3 -1 4 1 0
0 1  2 3 4

2-4-0-3-1-NULL
  1 2 3 4 5

*/
int n;
int h[N],rk[N];
bool vis[N];

int main(){
	cin >> n;
	for(int i = 0; i < n; i ++ ){
		int x;cin>>x;
		h[i] = x;
		if(x != -1) vis[x] = true;
	}	
	int idx = 0;
	while(vis[idx]) idx++;
	int cnt = 0;
	while(idx != -1) rk[idx] = cnt++,idx = h[idx];
	rk[idx] = cnt;
	for(int i = 0; i < n; i ++ ){
		if(i) cout<<" ";
		cout<<rk[h[i]];
	}	 
	return 0;
}

T3

深搜

#include<bits/stdc++.h>

using namespace std;

const int N = 1e5+10;

/*

*/

int n,m;
vector<int> g[N];
bool vis[N];
int cnt[N];

int dfs(int u){
	int sum = 1;
	for(int j : g[u]){
		if(vis[j]) continue;
		vis[j] = true;
		sum += dfs(j);
		vis[j] = false;
	}
	cnt[u] = sum;
	return sum;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for(int i = 2; i <= n; i ++ ){
		int x;cin>>x;
		g[x].push_back(i);
	}	
	dfs(1);
	cin>>m;
	for(int i = 0; i < m; i ++){
		int idx;cin>>idx;
		cout<<cnt[idx]<<endl;
	}
	return 0;
}

T4

floyd+模拟

找到坑点了,初始化payment为-inf 时间为inf
可能会跨天,时间要对60*24进行取余

#include<bits/stdc++.h>

using namespace std;

const int N = 1e3+10;
const int inf = 0x3f3f3f3f;
typedef pair<int,int> pii;
/*
5 11 08:00
09:00 10 2
08:30 50 10
13:00 5 1
08:35 20 3
08:30 200 80
1 0 5
0 2 30
3 0 20
0 4 40
4 5 5
1 4 21
1 3 60
1 2 30
2 3 10
3 4 2
2 4 60
5
1 4 1 3 2
3 4 1 2 1
3 4 1 1 2
5 1 2 3 1
5 4 1 1 2

*/

int n,m,st;
int g[N][N];
pair<int,pii> p[N];
int a[N];

int convert(string tim){
	int hh = stoi(tim.substr(0,2))*60;
	int tt = stoi(tim.substr(3,2));
	return hh+tt;
}

void floyd(){
	for(int k = 0; k <= n; k ++ ){
		for(int i = 0; i <= n; i ++ ){
			for(int j = 0; j <= n; j ++ ){
				if(g[i][k]!=inf&&g[k][j]!=inf)
				g[i][j] = min(g[i][k]+g[k][j],g[i][j]);
			}
		}
	}
}

int main(){
	string t;
	cin>>n>>m>>t;
	st = convert(t);
	for(int i = 1; i <= n; i ++ ){
		string t;
		int ya,yb;
		cin>>t>>ya>>yb;
		int tim = convert(t);
		p[i] = {tim,{ya,yb}};
	}
	for(int i = 0; i <= n; i ++ ){
		for(int j = 0; j <= n; j ++ ){
			if(i == j) continue;
			g[i][j] = inf;
		}
	}
	for(int i = 1; i <= m; i ++){
		int a,b,c;cin>>a>>b>>c;
		g[a][b] = g[b][a] = min(g[a][b],c);
	}
	floyd();
//	for(int i = 0; i <= n; i ++ ){
//		for(int j = 0; j <= n; j ++ ){
//			cout<<g[i][j]<<" ";
//		}
//		puts("");
//	}
//	return 0;
	int k;cin>>k;
	int mx_a = -1e9,mx_b = 1e9;
	//修改为-inf,inf
	while(k --){
		map<int,int> hs;
		bool flag = false;
		for(int i = 1; i <= n; i ++){
			cin >> a[i];
			hs[a[i]]+=1;
			if(hs[a[i]] > 1 || a[i] < 1 || a[i] > n) flag = true;
		}
		if(flag) continue;
		int tim = st;
		int idx = 0;
		int sa = 0;
		for(int i = 1; i <= n+1; i ++){
			if(g[idx][a[i]] == inf) {
				flag = true;break;
			}
			tim += g[idx][a[i]];
			idx = a[i];
			int a1 = p[a[i]].second.first;
			int b1 = p[a[i]].second.second;
			int c = p[a[i]].first;
			sa+=a1;
			if(tim > c) sa -= b1;
			//cout<<tim/60<<" "<<tim%60<<endl;
		}
		if(flag) continue;
		//return 0;
		if(mx_a < sa){
			mx_a = sa;
			mx_b = tim;
		}else if(mx_a == sa){
			if(mx_b > tim) mx_b = tim;
		}
//		printf("--------\n");
//		cout<<tim/60<<" "<<tim%60<<endl;
//		cout<<sa<<endl;
//		printf("--------\n");
	}
	//分钟对24*60进行取余运算
	//mx_b%=24*60;
	printf("%d %02d:%02d",mx_a,mx_b/60,mx_b%60);
	return 0;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值