1. 程式人生 > 其它 >The 15th Zhejiang Provincial Collegiate Programming Contest

The 15th Zhejiang Provincial Collegiate Programming Contest

比賽連結:(重現)

https://vjudge.net/contest/487174

A - Peak

題目大意:

判斷序列是不是先嚴格單調遞增,後嚴格單調遞減。

思路:

直接模擬判斷就好了。

程式碼:

#include <bits/stdc++.h>
using namespace std;
#define LL long long
LL T, n;
void solve(){
	cin >> n;
	vector <LL> a(n + 1);
	for (int i = 1; i <= n; ++ i)
		cin >> a[i];
	LL p = 1, q = n;
	while ( p < n && a[p] < a[p + 1] ) p++;
	while ( q > 1 && a[q - 1] > a[q] ) q--;
	if (p == 1 || q == n) cout << "No\n";
	else if (p == q) cout << "Yes\n";
	else cout << "No\n";
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	cin >> T;
	while (T--)
		solve();
	return 0;
}

B - King of Karaoke

題目大意:

給定兩個序列 \(a\)\(b\),可以讓序列 \(a\) 的每一個元素加上 \(k\),判斷最多能讓多少 \(a_i == b_i\),輸出數量。

思路:

因為所有元素加上一個值後,不會改變一個序列內所有元素間的差值,所以直接讓兩個序列做差,判斷哪個數數量最多就行。

程式碼:

#include <bits/stdc++.h>
using namespace std;
#define LL long long
LL T, n, x;
void solve(){
	cin >> n;
	LL ans = 0;
	map <LL, LL> cnt; 
	vector <LL> a(n + 1), b(n + 1);
	for (int i = 1; i <= n; ++ i)
		cin >> a[i];
	for (int i = 1; i <= n; ++ i)
		cin >> b[i];
	for (int i = 1; i <= n; ++ i){
		x = a[i] - b[i];
		cnt[x]++;
	}
	for (auto i : cnt)
		ans = max(ans, i.second);
	cout << ans << "\n";
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	cin >> T;
	while (T--)
		solve();
	return 0;
}

J - CONTINUE...?

題目大意:

給一個長為 \(n\) 的 01 字串,第 \(i\) 位的同學有 \(i\) 個寶石,0 表示這個人是女生,1 表示他是男生,現在將女生分成兩組 \(G1\)\(G2\),男生分成 \(G3\)\(G4\),找到一種方案,讓 \(G1 + G3\) 的寶石和 \(G2 + G4\) 的寶石數量相同,找不到輸出 -1.

思路:

由兩組的寶石數量相同可以得到,寶石的總數量要為偶數,即 \(( (n + 1) * n / 2 ) % 2 == 0\)
接下來只需要讓找到方案等於總數的一半就行,直接遍歷去找就好了,將所有人分成兩組,每組再判斷是不是男女,輸出對應的組就行了。

程式碼:

#include <bits/stdc++.h>
using namespace std;
#define LL long long
LL T, n;
string s;
void solve(){
	cin >> n >> s;
	LL k = n * (n + 1) / 2;
	vector <LL> v(n + 1);
	if (k % 2 == 0){
		k /= 2;
		for (LL i = n; i; -- i){
			if ( k >= i ){
				k -= i;
				v[i] = 1;
			}
		}
		for (int i = 1; i <= n; ++ i){
			if (v[i]){
				if (s[i - 1] == '1') cout << 3;
				else cout << 1;
			}
			else {
				if (s[i - 1] == '1') cout << 4;
				else cout << 2;
			}
		}
		cout << "\n";
	}
	else cout << "-1\n";
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	cin >> T;
	while (T--)
		solve();
	return 0;
}

L - Doki Doki Literature Club

題目大意:

\(n\) 個單詞,每個單詞有一個喜愛值 \(f\),現在要選擇 \(m\) 個單詞寫成一首詩,詩的總快樂值為 $\sum_{i = 1}^{m} (m - i + 1) * f(i) $,找到一種單詞的排序方式,使詩的快樂值最大,若有多個方案,輸出字典序最小的那一種。

思路:

因為 \(m - i + 1\) 是減少的,所以要讓快樂值大的放在最前面,然後按照字典序升序一下就行。

程式碼:

#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 110;
LL T, n, m;
struct node{
	LL a;
	string s;
}nd[N];
void solve(){
	cin >> n >> m;
	for (int i = 1; i <= n; ++ i)
		cin >> nd[i].s >> nd[i].a;
	sort( nd + 1, nd + n + 1, [](node a, node b){
		if (a.a != b.a) return a.a > b.a;
		return a.s < b.s;
	});
	LL ans = 0;
	for (int i = 1; i <= m; ++ i)
		ans += (m - i + 1) * nd[i].a;
	cout << ans;
	for (int i = 1; i <= m; ++ i)
		cout << " " << nd[i].s;
	cout << "\n";
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	cin >> T;
	while (T--)
		solve();
	return 0;
}

M - Lucky 7

題目大意:

給一個長為 \(n\) 的序列,判斷有沒有一個元素加上 \(b\) 是 7 的倍數。

思路:

依次判斷即可。

程式碼:

#include <bits/stdc++.h>
using namespace std;
#define LL long long
LL T, n, b;
void solve(){
	cin >> n >> b;
	vector <LL> a(n + 1);
	for (int i = 1; i <= n; ++ i)
		cin >> a[i];
	for (int i = 1; i <= n; ++ i){
		if ( (a[i] + b) % 7 == 0 ){
			cout << "Yes\n";
			return;
		}
	}
	cout << "No\n";
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	cin >> T;
	while (T--)
		solve();
	return 0;
}