尚真书院暨信息技术学院2022年程序设计大赛

文章目录

    • 7-2 BWO - 2
    • 7-3 重叠字符串
    • 7-4 鹅武JUNshi冲突 - 1
    • 7-5 鹅武JUNshi冲突 - 2
    • 7-6 探基逃课
    • 7-8 小马的财富
    • 7-10 选课

7-2 BWO - 2

签到题

#include 
#define IOS std::ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef map<string, int> msi;
template<typename T> void printv(vector<T> a) {for(int i=0;i<a.size();++i) {if(i == 0) {cout << a[i]; } else {cout << " " << a[i];}}}

//const int maxn = 1e5+5;

void solve() {
	string name;
	int a,b,c;
	int x = 0,y = 0,z = 0;
	while(cin >> name >> a >> b >> c) {
		x += a;
		y += b;
		z += c ;
	}
	cout << x << " " << y << " " << z; 
}

int main() {
	IOS
	solve();
	return 0;
}



7-3 重叠字符串

字符串哈希 + 折半

#include 
#include 
#include 
#include 
#include  
#define IOS std::ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef map<string, int> msi;
template<typename T> void printv(vector<T> a) {for(int i=0;i<a.size();++i) {if(i == 0) {cout << a[i]; } else {cout << " " << a[i];}}}

const int maxn = 1005;
const int P = 131;
char str[maxn];
ull _hash[maxn];
ull p[maxn];

int len;

ull get(int a,int b) {
	return _hash[b] - _hash[a - 1] * p[b - a + 1];
}

void init() {
	p[0] = 1;
	for(int i=1;i<=len;++i) {
		p[i] = p[i - 1] * P;
		_hash[i] = _hash[i - 1] * P + str[i];
	}
}

void solve() {
	while(cin >> str + 1) {
		len = strlen(str + 1); 
		memset(p, 0, sizeof p);
		memset(_hash, 0, sizeof _hash);
		init();
		int j = len;
		int res = 0;
		while(j % 2 == 0) {
			if(get(1, j / 2) == get(j / 2 + 1, j)) {
				j /= 2;
				res ++;
			} else {
				break;
			}
		}
		for(int i=1;i<=j;++i) {
			cout << str[i];
		}
		cout << " " << res << endl; 
	}
	
}

int main() {
//	IOS
	solve();
	return 0;
}

7-4 鹅武JUNshi冲突 - 1

模拟+遍历

#include 
#include 
#include 
#include 
#define IOS std::ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef map<string, int> msi;
template<typename T> void printv(vector<T> a) {for(int i=0;i<a.size();++i) {if(i == 0) {cout << a[i]; } else {cout << " " << a[i];}}}

//const int maxn = 1e5+5;
int u[1005];
int kill[1005];

int half(int x) {
	return x / 2 + 1;
}

int bigthen0(int x) {
	return x <= 0 ? 0 : x;
}

void solve() {
	int n,L,R;
	cin >> n >> L >> R;
	for(int i=0;i<n;++i) {
		cin >> u[i];
	}
	
	int res = 1e9+5;
	int iii = 0;
	for(int i=L;i<=R;++i) {
		int lost = n * i;
		int get = 0;
//		cout << "当前伤害:" << i << endl;
		for(int j=0;j<n;++j) {
			int h = half(u[j]);
			//cout << h << endl; 
			if(i >= h) {
				//cout << "摧毁" << j << ",获取" << u[j] - h << endl; 
				get += bigthen0(u[j] - i);
			} else {
				//cout << "未摧毁" << j << ",损失" << u[j] - h << endl;
				lost += bigthen0(u[j] - i);
			}
		}
//		cout << lost - get << endl; 
		if(lost - get < res) {
			res = lost - get;
			iii = i;
		}
	}
	cout << iii << " " << res; 
}

int main() {
	IOS
	solve();
	return 0;
}



7-5 鹅武JUNshi冲突 - 2

洪泛法 BFS

#include 
#include 
#include 
#include 
using namespace std;
#define IOS std::ios::sync_with_stdio(false);
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef map<string, int> msi;
template<typename T> void printv(vector<T> a) {for(int i=0;i<a.size();++i) {if(i == 0) {cout << a[i]; } else {cout << " " << a[i];}}}

//const int maxn = 1e5+5;

int m,n;
int a[1005][1005];
bool vis[1005][1005];

int dx[4] = {0,0,-1,1};
int dy[4] = {1,-1,0,0};

int res = 0;

void bfs(int x,int y) {
	//cout << x << " " << y << endl; 
	queue< pii > Q;
	Q.push({x, y});
	
	//是否为死路 
	bool die = false;
	vector< pii > jilu;
	while(Q.size()) {
		pii t = Q.front();
		Q.pop();
		jilu.push_back(t);
		
		//cout << t.first << " " << t.second << "|";
		
		// 边界 一定不是 
		if(t.first <= 1 || t.second <= 1 || t.first >= m || t.second >= n) {
			die = true;
		}
		// 如果访问过 ,不再处理 
		if(vis[t.first][t.second]) {
			continue;
		}
		//标记访问 
		vis[t.first][t.second] = true;
		
		//查看周围 
		for(int i=0;i<4;++i) {
			int ax = t.first + dx[i];
			int ay = t.second + dy[i];
			//cout << ay << ":" << ay << endl;
			//判断出界点 
			if(ax <= 0 || ay <= 0 || ax > m || ay > n) {
				continue;
			}
			// 连通块 
			if(a[ax][ay] == a[t.first][t.second] && !vis[ax][ay]) {
				Q.push({ax, ay});
				//cout << "PUSH : " << x << " " << y << endl; 
			}
			// 周围有更小的点 ,则当前为死路 
			if(a[ax][ay] < a[t.first][t.second]) {
				die = true;
			}
		}
	}
	if(die) {
		return;
	}
	res ++;
}

void solve() {
	cin >> m >> n;
	for(int i=1;i<=m;++i) {
		for(int j=1;j<=n;++j) {
			cin >> a[i][j];
		}
	}
	for(int i=1;i<=m;++i) {
		for(int j=1;j<=n;++j) {
			if(! vis[i][j]) {
				bfs(i, j);
				//cout << endl; 
			}
		}
	}
	cout << res; 
}

int main() {
	IOS
	solve();
	return 0;
}
/*
5 5
1 2 3 4 5
1 1 2 3 3
1 2 2 1 3
3 3 3 1 2
3 5 6 3 2

*/


7-6 探基逃课

签到题

#include 
#define IOS std::ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef map<string, int> msi;
template<typename T> void printv(vector<T> a) {for(int i=0;i<a.size();++i) {if(i == 0) {cout << a[i]; } else {cout << " " << a[i];}}}

//const int maxn = 1e5+5;

void solve() {
	ll a, b;
	cin >> a >> b;
	cout << a * ( b - 1);
}

int main() {
	IOS
	solve();
	return 0;
}


7-8 小马的财富

快速幂

题意有小坑

#include 
#define IOS std::ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef map<string, int> msi;
template<typename T> void printv(vector<T> a) {for(int i=0;i<a.size();++i) {if(i == 0) {cout << a[i]; } else {cout << " " << a[i];}}}

//const int maxn = 1e5+5;

ll ksm(ll a,ll b,ll p) {
	ll res = 1;
	while(b) {
		if(b&1) {
			res = (res % p) * (a % p) % p;
		}
		b >>= 1;
		a = (a % p) * (a % p) % p; 
	}
	return res;
}

void solve() {
	ll m ,k ,n;
	cin >> m >> k >> n;
	ll sum = 0;
	sum = ((m % n) * (ksm(k,11, n)) % n) % n;
	if(sum == 0) {
		sum += n;
	}
	cout << sum;
}

int main() {
	IOS
	solve();
	return 0;
}


7-10 选课

模拟

#include 
#include 
#include 
#include 
#include 
#include 
#define IOS std::ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef map<string, int> msi;
template<typename T> void printv(vector<T> a) {for(int i=0;i<a.size();++i) {if(i == 0) {cout << a[i]; } else {cout << " " << a[i];}}}

//const int maxn = 1e5+5;

set<int> stu[100005];
map<int, int> cls;
map<int, int> cls_cnt;
set<int> cls_close;

vector<pii> l;

void solve() {
	int n, low, high;
	scanf("%d%d%d",&n,&low,&high);
	int sid, cid;
	for(int i=0;i<n;++i) {
		scanf("%d%d",&sid, &cid);
		l.push_back({sid, cid});
		// 统计课程号 
		if(cls_cnt.count(cid)) {
			cls_cnt[cid] ++;
		} else {
			cls_cnt[cid] = 1;
		}
	}
	// 找出不能开课的课程 
	for(pair<int, int> kv : cls_cnt) {
		//cout << kv.first << " " <<  kv.second << endl;
		if(kv.second < low) {
			cls_close.insert(kv.first);
		}
	}
//	for(int close : cls_close) {
//		cout << close << endl; 
//	}
	for(int i=0;i<n;++i) {
		sid = l[i].first;
		cid = l[i].second;
		// 初始化这个班级选课人数 
		if(!cls.count(cid)) {
			cls[cid] = 0;
		}
		// 可以选 
		if(cls[cid] + 1 <= high && !cls_close.count(cid)) {
			cls[cid] ++; 
			stu[sid].insert(cid);
		}
	}
	int id;
	scanf("%d",&id);
	if(stu[id].size() <= 0) {
		printf("sorry\n");
	}
	vi res;
	for(int x : stu[id]) {
		res.push_back(x);
	}
	sort(res.begin(), res.end());
	for(int i=0;i<res.size(); ++i) {
		if(i==0) {
			printf("%03d", res[i]);
		} else {
			printf(" %03d", res[i]);
		}
	}
}

int main() {
	solve();
	return 0;
}


你可能感兴趣的:(PTA,哈希算法,c++,散列表,acm竞赛)