题解:P15172 [SWERC 2021] Organizing SWERC
P15172
本蒟蒻第一篇题解OvO
题意
题目传送门
给定一个正整数
给一个长度为
对于第
- 将数对数列中筛选出次项为
i 的所有元素 - 如果筛出的元素数量为
0 就输出"MOREPROBLEMS" 并结束所有操作,否则在筛出来的元素中找到首项最大的元素并将累加变量sum 加上那个元素的首项的值(若有相同值任取其一,不影响结果) - 输出
sum 解题思路
我们采用
pair<int,int>a[MAX\_N] 或结构体数组用于存储每道题的信息
其次定义累加变量sum 来表示最大美观度之和
最后遍历数组并取max AC代码pair
#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代码结构体
#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代码双数组
#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
#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代码结构体
#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代码双数组
#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; }谢观看
留个赞再走叭!