- 先从卡号提取信息,再根据相应信息筛选。
- 尽量用
scanf
和printf
进行输入输出。
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<unordered_map>
using namespace std;
struct node {
string card;
string site;
string date;
int score;
};
vector<node> testees;
unordered_map<string, int> testeeCount; //site-->testeeCount
bool cmp1(node &a, node &b) {
if (a.score != b.score)
return a.score > b.score;
else return a.card < b.card;
}
bool cmp2(string &a, string &b) {
if (testeeCount[a] != testeeCount[b]) {
return testeeCount[a] > testeeCount[b];
}
else return a < b;
}
int main() {
int cardNum, queryNum;
scanf("%d %d\n", &cardNum, &queryNum);
testees.resize(cardNum);
for (int i = 0;i < cardNum;i++) {
cin >> testees[i].card >> testees[i].score;
cin.get();
testees[i].site = testees[i].card.substr(1, 3);
testees[i].date = testees[i].card.substr(4, 6);
}
for (int i = 0;i < queryNum;i++) {
int type;
cin >> type;
if (type == 1) {
char level;
vector<node> res;
cin >> level;
for (int j = 0;j < cardNum;j++) {
if (testees[j].card[0] == level) {
res.push_back(testees[j]);
}
}
printf("Case %d: %d %c\n", i + 1, type, level);
if (!res.empty()) {
sort(res.begin(), res.end(), cmp1);
for (int i = 0;i < res.size();i++) {
printf("%s %d\n", res[i].card.c_str(), res[i].score);
}
}
else printf("NA\n");
}
else if (type == 2) {
string site;
cin >> site;
int totalTestee = 0, totalScore = 0;
for (int j = 0;j < cardNum;j++) {
if (testees[j].site == site) {
totalScore += testees[j].score;
totalTestee++;
}
}
printf("Case %d: %d %s\n", i + 1, type, site.c_str());
if (totalTestee != 0) {
printf("%d %d\n", totalTestee, totalScore);
}
else printf("NA\n");
}
else {
string date;
cin >> date;
testeeCount.clear();
for (int j = 0;j < cardNum;j++) {
if (testees[j].date == date) {
testeeCount[testees[j].site]++;
}
}
vector<string> sites;
for (auto it = testeeCount.begin(); it != testeeCount.end(); it++) {
sites.push_back(it->first);
}
sort(sites.begin(), sites.end(), cmp2);
printf("Case %d: %d %s\n", i + 1, type, date.c_str());
if (!sites.empty()) {
for (int j = 0;j < sites.size();j++) {
printf("%s %d\n", sites[j].c_str(), testeeCount[sites[j]]);
}
}
else printf("NA\n");
}
}
}
二刷:
用各种表处理好数据,在查询时可以很快得到结果。注意三种查询都要判空。
#include<iostream>
#include<string>
#include<cmath>
#include<unordered_map>
#include<algorithm>
#include<utility>
#include<vector>
using namespace std;
struct node {
char level;
string id, site, date;
int score;
};
vector<node> card;
unordered_map<char, vector<node> > level2Card;
unordered_map<string, int> site2TotalScore, site2Count;
bool ScoreCmp(node &a, node &b) {
if (a.score != b.score) {
return a.score > b.score;
}
else return a.id < b.id;
}
bool dateCmp(node &a, node &b) {
if (a.date != b.date) {
return a.date < b.date;
}
else return a.id < b.id;
}
bool pairCmp(pair<string, int> &a, pair<string, int> &b) {
if (a.second != b.second) {
return a.second > b.second;
}
else return a.first < b.first;
}
int main() {
int numCard, numQuery;
cin >> numCard >> numQuery;
card.resize(numCard);
for (int i = 0; i < numCard; i++) {
node temp;
cin >> temp.id >> temp.score;
temp.date = temp.id.substr(4, 6);
temp.site = temp.id.substr(1, 3);
temp.level = temp.id[0];
card[i] = temp;
level2Card[temp.level].push_back(temp);
site2TotalScore[temp.site] += temp.score;
site2Count[temp.site]++;
}
sort(card.begin(), card.end(), dateCmp);
for (auto &it : level2Card) {
vector<node> &temp = it.second;
sort(temp.begin(), temp.end(), ScoreCmp);
}
for (int i = 0; i < numQuery; i++) {
int type;
cin >> type;
if (type == 1) {
char level;
cin >> level;
printf("Case %d: %d %c\n", i + 1, type, level);
if (level2Card[level].empty()) {
printf("NA\n");
}
else {
for (node e : level2Card[level]) {
printf("%s %d\n", e.id.c_str(), e.score);
}
}
}
else if (type == 2) {
string site;
cin >> site;
printf("Case %d: %d %s\n", i + 1, type, site.c_str());
if (site2TotalScore.count(site) == 0) {
printf("NA\n");
}
else {
printf("%d %d\n", site2Count[site], site2TotalScore[site]);
}
}
else {
unordered_map<string, int> site2Count;
string date;
cin >> date;
printf("Case %d: %d %s\n", i + 1, type, date.c_str());
int index = 0;
while (index < numCard && card[index].date != date) {
index++;
}
while (index < numCard && date == card[index].date) {
site2Count[card[index].site]++;
index++;
}
vector<pair<string, int> > res;
for (auto e : site2Count) {
res.push_back(e);
}
sort(res.begin(), res.end(), pairCmp);
if (res.empty()) {
printf("NA\n");
}
else {
for (auto e : res) {
printf("%s %d\n", e.first.c_str(), e.second);
}
}
}
}
}