PAT Advanced 1153 Decode Registration Card of PAT (25 )
题目描述
Input Specification:
Output Specification:
Sample Input:
Sample Output:
解题思路
输入数据N(<10^4),查询数据M(<100),我还在想在线处理打表。。当然在最后一个点超时了。 看了柳神题解发现炒鸡妙,离线处理好,开始日常膜柳神。照着柳神思路写发现最后两个点超时。。最后发现cout
输给printf
是因为endl
,改成'\n'
就过了。。
Code
- AC代码
#include<bits/stdc++.h>
using namespace std;
struct Node {
string id;
int score;
};
bool cmp(const Node &a, const Node &b) {
return a.score != b.score ? a.score > b.score : a.id < b.id;
}
int main() {
//freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
int N, M, type;
cin >> N >> M;
vector<Node> students(N);
for(int i = 0; i<N; i++) {
cin >> students[i].id >> students[i].score;
}
string term;
for(int i = 1; i<=M; i++) {
cin >> type >> term;
cout << "Case " << i << ": " << type << " " << term << '\n';
vector<Node> res;
int cnt = 0, totalScore = 0;
if(type == 1) {
for(int j = 0; j<N; j++) {
if(students[j].id[0] == term[0]) {
res.push_back(students[j]);
}
}
} else if(type == 2) {
for(int j = 0; j<N; j++) {
if(students[j].id.substr(1, 3) == term) {
cnt++, totalScore += students[j].score;
}
}
if(cnt) {
cout << cnt << " " << totalScore << '\n';
}
} else if(type == 3) {
unordered_map<string, int> m;
for(int j = 0; j<N; j++) {
if(students[j].id.substr(4, 6) == term) {
m[students[j].id.substr(1, 3)]++;
}
}
for(auto it : m) {
res.push_back({it.first, it.second});
}
} else {
cout << "NA\n";
}
sort(res.begin(), res.end(), cmp);
for(int j = 0; j<res.size(); j++) {
cout << res[j].id << " " << res[j].score << '\n';
}
if(((type == 1 || type == 3) && !res.size()) || (type == 2 && cnt == 0)) {
cout << "NA\n";
}
}
return 0;
}
- 原始超时代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4+10;
struct Node {
int score;
string id;
Node(int _score, string _id):score(_score), id(_id){
}
};
vector<Node> pat[3];
struct Site{
int countPeople=0, totalScore = 0;
}site[1000];
struct Date1 {
int cnt, site;
}date1[maxn][1000];
unordered_map<string, int> str2date1;
unordered_map<string, int> site2int;
bool cmp(const Node &a, const Node &b) {
if(a.score != b.score) {
return a.score > b.score;
}
return a.id < b.id;
}
bool cmp2(const Date1 &a, const Date1 &b) {
if(a.cnt != b.cnt) {
return a.cnt > b.cnt;
}
return a.site < b.site;
}
int str2int(string &str) {
int ans = 0;
for(int i = 0; i<str.length(); i++) {
ans = ans * 10 + str[i] - '0';
}
return ans;
}
void printAll(const vector<Node> &v){
if(!v.size()) {
cout << "NA\n";
return ;
}
for(int i = 0; i<v.size(); i++) {
cout << v[i].id << " " << v[i].score << endl;
}
}
int main() {
//freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
int N, M, score, totalA = 0, totalT = 0, totalB = 0, cnt = 0, flag = 1;
string id;
cin >> N >> M;
for(int i = 1; i<=N; i++) {
cin >> id >> score;
string sitestr = id.substr(1, 3);
int siteNumber = site2int[sitestr];
if(!siteNumber) {
site2int[sitestr] = siteNumber = str2int(sitestr);
}
site[siteNumber].countPeople++;
site[siteNumber].totalScore += score;
string str = id.substr(4, 6);
if(!str2date1[str]) {
str2date1[str] = i;
}
date1[str2date1[str]][siteNumber].site = siteNumber;
date1[str2date1[str]][siteNumber].cnt ++;
int level = 0;
if(id[0] == 'A') level = 1;
else if(id[0] == 'T') level = 2;
Node temNode(score, id);
pat[level].push_back(temNode);
}
string type, term;
for(int i = 1; i<=M; i++) {
cin >> type >> term;
cout << "Case " << i << ": " << type << " " << term << '\n';
if(type == "1") {
if(flag) {
for(int i = 0; i<3; i++) {
sort(pat[i].begin(), pat[i].end(), cmp);
}
flag = 0;
}
if(term == "A") {
printAll(pat[1]);
} else if(term == "B") {
printAll(pat[0]);
} else if(term == "T") {
printAll(pat[2]);
}
} else if(type == "2") {
int num = site2int[term];
if(num && site[num].countPeople > 0) {
cout << site[num].countPeople << " " << site[num].totalScore << '\n';
} else {
cout << "NA\n";
}
} else if(type == "3") {
int dateId = str2date1[term];
if(!dateId) {
cout << "NA\n";
} else {
sort(date1[dateId], date1[dateId]+1000, cmp2);
if(!date1[dateId][0].cnt) {
cout << "NA";
} else {
int j = 0;
while(date1[dateId][j].cnt && j < 1000) {
cout << date1[dateId][j].site << " " << date1[dateId][j].cnt << '\n';
j++;
}
}
}
} else {
cout << "NA\n";
}
}
return 0;
}
总结
res.push_back({it.first, it.second});
妙