跳到正文

题解

题解:P15172 [SWERC 2021] Organizing SWERC

yxy2约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年2月4日
收录
2026年2月4日
更新
2026年11月15日
篇幅
约 9 分钟
题目
P15172

版本

  1. v2当前
  2. v1

P15172

本蒟蒻第一篇题解OvO

题意

题目传送门

给定一个正整数tt并对下列操作执行tt次

给一个长度为nn的数对数列(b1,d1),(b2,d2),⋅⋅⋅⋅⋅⋅,(bn,dn)(b_1,d_1),(b_2,d_2),······,(b_n,d_n)

对于第i(1≤i≤10)i(1≤i≤10)次操作::

  1. 将数对数列中筛选出次项为ii的所有元素
  2. 如果筛出的元素数量为00就输出"MOREPROBLEMS""MOREPROBLEMS" 并结束所有操作,否则在筛出来的元素中找到首项最大的元素并将累加变量sumsum加上那个元素的首项的值(若有相同值任取其一,不影响结果)
  3. 输出sumsum

解题思路

我们采用pair<int,int>a[MAX_N]pair<int,int>a[MAX\_N]或结构体数组用于存储每道题的信息 其次定义累加变量sumsum来表示最大美观度之和 最后遍历数组并取maxmax

AC代码pair

C++
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 105;
int t, n, sum;
bool bl; //MOREPROBLEMS标记
pair<int, int>a[MAX_N];
int main() {
	cin >> t;
	for (int i = 1; i <= t; i++) {
		sum = 0;
		bl = 1;
		cin >> n;
		for (int j = 1; j <= n; j++) {
			cin >> a[j].first;
			cin >> a[j].second;
		}
		for (int j = 1; j <= 10; j++) { //枚举所有难度
			int maxx = 0;
			for (int k = 1; k <= n; k++) { //遍历a数组,查询最大值
				if (a[k].second == j) {
					maxx = max(maxx, a[k].first);
				}
			}
			if (maxx == 0) {
				bl = 0;
			}
			sum += maxx;
		}
		if (bl)cout << sum << endl;
		else cout << "MOREPROBLEMS\n";
	}
	return 0;
}

因为这是一道入门题,所以肯定有一些人没学过pair,所以我提供了以下代码

AC代码结构体

C++
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 105;
int t, n, sum;
bool bl; //MOREPROBLEMS标记
struct pairr{
	int b,d;
}a[MAX_N];
int main() {
	cin >> t;
	for (int i = 1; i <= t; i++) {
		sum = 0;
		bl = 1;
		cin >> n;
		for (int j = 1; j <= n; j++) {
			cin >> a[j].b;
			cin >> a[j].d;
		}
		for (int j = 1; j <= 10; j++) { //枚举所有难度
			int maxx = 0;
			for (int k = 1; k <= n; k++) { //遍历a数组,查询最大值
				if (a[k].d == j) {
					maxx = max(maxx, a[k].b);
				}
			}
			if (maxx == 0) {
				bl = 0;
			}
			sum += maxx;
		}
		if (bl)cout << sum << endl;
		else cout << "MOREPROBLEMS\n";
	}
	return 0;
}

再考虑到有些人连结构体都没学,我又双叒叕提供了以下代码

AC代码双数组

C++
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 105;
int t, n, sum;
bool bl; //MOREPROBLEMS标记
int b[MAX_N];
int d[MAX_N];
int main() {
	cin >> t;
	for (int i = 1; i <= t; i++) {
		sum = 0;
		bl = 1;
		cin >> n;
		for (int j = 1; j <= n; j++) {
			cin >> b[j];
			cin >> d[j];
		}
		for (int j = 1; j <= 10; j++) { //枚举所有难度
			int maxx = 0;
			for (int k = 1; k <= n; k++) { //遍历a数组,查询最大值
				if (d[k] == j) {
					maxx = max(maxx, b[k]);
				}
			}
			if (maxx == 0) {
				bl = 0;
			}
			sum += maxx;
		}
		if (bl)cout << sum << endl;
		else cout << "MOREPROBLEMS\n";
	}
	return 0;
}

然后打完了上面的代码我们发现太复杂了,还要每个难度都枚举一遍,能不能简化呢?我们秉这精益求精的原则,打出了下面的三个代码

简化AC代码pair

C++
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 105;
int t, n, sum;
bool bl; //MOREPROBLEMS标记
pair<int, int>a[MAX_N];
int main() {
	cin >> t;
	for (int i = 1; i <= t; i++) {
		sum = 0;
		bl = 1;
		cin >> n;
		for (int j = 1; j <= n; j++) {
			cin >> a[j].first;
			cin >> a[j].second;
		}
		int maxx[11]; //每种难度的美观度最大值
		for (int j = 1; j <= 10; j++) {
			maxx[j] = 0;
		}//或使用memset (maxx, 0, sizeof(maxx));
		for (int j = 1; j <= n; j++) {
			maxx[a[j].second] = max(maxx[a[j].second], a[j].first);
		}
		for (int j = 1; j <= 10; j++) {
			if (maxx[j] == 0) {
				bl = 0;
			}
			sum += maxx[j];
		}
		if (bl)cout << sum << endl;
		else cout << "MOREPROBLEMS\n";
	}
	return 0;
}

简化AC代码结构体

C++
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 105;
int t, n, sum;
bool bl; //MOREPROBLEMS标记
struct pairr{
	int b, d;
} a[MAX_N];
int main() {
	cin >> t;
	for (int i = 1; i <= t; i++) {
		sum = 0;
		bl = 1;
		cin >> n;
		for (int j = 1; j <= n; j++) {
			cin >> a[j].b;
			cin >> a[j].d;
		}
		int maxx[11]; //每种难度的美观度最大值
		for (int j = 1; j <= 10; j++) {
			maxx[j] = 0;
		}//或使用memset (maxx, 0, sizeof(maxx));
		for (int j = 1; j <= n; j++) {
			maxx[a[j].d] = max(maxx[a[j].d], a[j].b);
		}
		for (int j = 1; j <= 10; j++) {
			if (maxx[j] == 0) {
				bl = 0;
			}
			sum += maxx[j];
		}
		if (bl)cout << sum << endl;
		else cout << "MOREPROBLEMS\n";
	}
	return 0;
}

简化AC代码双数组

C++
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 105;
int t, n, sum;
bool bl; //MOREPROBLEMS标记
int b[MAX_N];
int d[MAX_N];
int main() {
	cin >> t;
	for (int i = 1; i <= t; i++) {
		sum = 0;
		bl = 1;
		cin >> n;
		for (int j = 1; j <= n; j++) {
			cin >> b[j];
			cin >> d[j];
		}
		int maxx[11]; //每种难度的美观度最大值
		for (int j = 1; j <= 10; j++) {
			maxx[j] = 0;
		}//或使用memset (maxx, 0, sizeof(maxx));
		for (int j = 1; j <= n; j++) {
			maxx[d[j]] = max(maxx[d[j]], b[j]);
		}
		for (int j = 1; j <= 10; j++) {
			if (maxx[j] == 0) {
				bl = 0;
			}
			sum += maxx[j];
		}
		if (bl)cout << sum << endl;
		else cout << "MOREPROBLEMS\n";
	}
	return 0;
}

谢观看

留个赞再走叭!

原文