1. 程式人生 > 實用技巧 >第三屆“傳智杯”全國大學生IT技能大賽(初賽A組)題解

第三屆“傳智杯”全國大學生IT技能大賽(初賽A組)題解

留念

C - 志願者

排序。。按照題目規則說的排就可以。wa了兩發我太菜了qwq

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
inline int read() {
	char c = getchar(); int x = 0, f = 1;
	while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}
struct Node {
	int t, k, id;
	bool operator < (const Node &b) const {
		if(t * k > b.t * b.k) return 1;
		else if(t * k < b.t * b.k) return 0;
		
		if(t > b.t) return 1;
		else if(t < b.t) return 0;
		
		return id < b.id;
	}
}a[MAXN];
int main() {
	int N = read();
	for(int i = 1; i <= N; i++) {
		a[i].t = read();
		a[i].k = read();
		a[i].id = i;
	}
	sort(a + 1, a + N + 1);
	for(int i = 1; i <= N; i++)
		cout << a[i].id << ' '; 
	return 0;
}

D - 終端

模擬一下就行了吧。。

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
inline int read() {
	char c = getchar(); int x = 0, f = 1;
	while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}
map<string, int> mp;
string opt;
int main() {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	int N = read();
	for(int i = 1; i <= N; i++) {
		cin >> opt;
		if(opt == "touch") {
			string fn;
			cin >> fn;
			if(mp.find(fn) != mp.end()) continue;
			mp[fn] = i;
		} else if(opt == "rm") {
			string fn;
			cin >> fn;
			if(mp.find(fn) == mp.end()) continue;
			mp.erase(fn); 
		} else if(opt == "ls") {
			vector<pair<int, string>> tmp;
			for(auto &x: mp) {
				tmp.push_back(make_pair(x.second, x.first));
			}
			sort(tmp.begin(), tmp.end());
			for(auto x: tmp)
				cout << x.second << '\n';
		} else if(opt == "rename") {
			string s1, s2;
			cin >> s1 >> s2;
			if(mp.find(s1) == mp.end()) continue;
			int tmp = mp[s1];
			mp.erase(s1);
			mp[s2] = tmp;
		}
	} 
	return 0;
}

E - 運氣

顯然每個位置只能是1-6,因此所有狀態數是\(6^{10}\)不會很大,dfs一下

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
const int mod = 1e9 + 7;
#define LL long long 
inline int read() {
	char c = getchar(); int x = 0, f = 1;
	while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}
LL N, K, ans;
void dfs(int x, LL val) {
	if(x == N + 1) {
		ans += (val % K == 0);
		return ;
	}
	for(int i = 1; i <= 6; i++)
		dfs(x + 1, val * 10 + i);
}
int main() {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	N = read(); K = read();
	dfs(1, 0);
	cout << ans % mod;
	return 0;
}

F - 遊戲

首先題目有個bug,沒有描述序列中有相同數的情況

那就假設所有數都不相同

可以分兩種情況考慮,\(c1 < c2\)\(c1 > c2\),相等的話直接輸出\((N - 1) * c1\)就行。

這兩種是類似的,這裡只說第一種。

顯然,陣列中的每一對數都有兩種情況:1.異或之後二進位制位僅有1位為1,2.有多位唯一。

接下來我是轉化成了圖論問題去考慮,不然感覺有點複雜。

我們把所有異或之後二進位制位僅有1個1的點之間連邊。

考慮得到的這張圖的性質:

對於任意一個聯通塊,我們一定能通過使用c1代價來每次消掉一個元素,並能保證最後只剩下一個元素。

emmm,,至於為什麼,,可以從聯通塊中抽出一個樹來,顯然每次從葉子節點刪,一定滿足條件。

這樣的話,只需要dfs出所有聯通塊,並求出其大小就好。

然後加加減減把答案算出來,具體看程式碼

複雜度\(O(n^2)\)(實際上還可以優化為\(O(nlogn)\),這裡不再贅述)

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
const int mod = 1e9 + 7;
#define LL long long 
inline int read() {
	char c = getchar(); int x = 0, f = 1;
	while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}
int N, c1, c2, a[MAXN], vis[MAXN];
LL cnt = 0, ans;
vector<int> v[MAXN];
void dfs(int x) {
	cnt++;
	vis[x] = 1; 
	for(auto &to: v[x]) {
		if(!vis[to])
			dfs(to);
	}
}
void dfs2(int i) {
	cnt++;
	vis[i] = 1;
	for(int j = 1; j <= N; j++) {
		if(__builtin_popcount(a[i] ^ a[j]) != 1 && i != j && vis[j] == 0) {
			dfs2(j);
		}
	}
}
int main() {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	N = read();
	c1 = read(); c2 = read();
	for(int i = 1; i <= N; i++) a[i] = read();
	if(c1 == c2) cout << 1ll * (N - 1) * c1;
	else if(c1 < c2) {
		for(int i = 1; i <= N; i++)
			for(int j = 1; j <= N; j++)
				if(__builtin_popcount(a[i] ^ a[j]) == 1 && i != j)
					v[i].push_back(j);
		LL sum = N;
		for(int i = 1; i <= N; i++) {
			if(!vis[i]) {
				dfs(i);
				ans += 1ll * (cnt - 1) * c1;
				sum -= (cnt - 1);
				cnt = 0;
			}
		}
		ans += 1ll * (sum - 1) * c2;
		cout << ans << '\n';
	} else {	
		LL sum = N;
		for(int i = 1; i <= N; i++) {
			if(!vis[i]) {
				dfs2(i);
				ans += 1ll * (cnt - 1) * c2;
				sum -= (cnt - 1);
				cnt = 0;
			}
		}
		ans += 1ll * (sum - 1) * c1;
		cout << ans << '\n';		
	}
	return 0;
}
/*

*/

G - 森林

(好久沒打比賽,開場還以為是個LCT,後來又想操作子樹的好像是ETT,不過還好及時終止了自己的危險想法)

感覺這題思維上比上一題簡單不少,

對於第一個刪邊比較難操作

可以倒序考慮轉化成加邊。

然後並查集維護連通性就ok了

具體看程式碼

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
const int mod = 1e9 + 7;
#define LL long long 
inline int read() {
	char c = getchar(); int x = 0, f = 1;
	while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}
int N, M;
int fa[MAXN], sum[MAXN];
struct Edge {
	int u, v;
}E[MAXN];
struct Opt {
	int opt, a, b;
}op[MAXN];
int val[MAXN], flag[MAXN];
int find(int x) {
	return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void unionn(int x, int y) {
	int fx = find(x), fy = find(y);
	sum[fy] += sum[fx];
	sum[fx] = 0;
	fa[fx] = fy;
}
int query(int x) {
	return sum[find(x)];
}
vector<int> ans;
int main() {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	N = read(); M = read();
	for(int i = 1; i <= N; i++) val[i] = read(), fa[i] = i;
	for(int i = 1; i < N; i++) {
		E[i].u = read(); 
		E[i].v = read();
	}
	for(int i = 1; i <= M; i++) {
		op[i].opt = read();
		if(op[i].opt == 1) {
			op[i].a = read();//add E[a]
			flag[op[i].a] = 1; 
		} else if(op[i].opt == 2) {
			op[i].a = read(), op[i].b = read();
			int tmp = val[op[i].a];//֮ǰµÄval 
			val[op[i].a] = op[i].b;
			op[i].b = tmp;
		} else {
			op[i].a = read();
		}
	}
	for(int i = 1; i <= N; i++) sum[i] = val[i];
	for(int i = 1; i < N; i++) {
		if(!flag[i]) {//not delet 
			unionn(E[i].u, E[i].v);	
		}
	
	}
	for(int i = M; i >= 1; i--) {
		if(op[i].opt == 1) {
			int id = op[i].a;
			unionn(E[id].u, E[id].v);
		} else if(op[i].opt == 2) {
			int id = op[i].a, pre = val[id];
			int fx = find(id);
			sum[fx] -= pre;
			sum[fx] += op[i].b;
			val[id] = op[i].b;
		} else {
			ans.push_back(query(op[i].a));
		}
	}
	
	reverse(ans.begin(), ans.end());
	for(auto &x: ans) cout << x << '\n';
	return 0;
}
/*

*/