Skip to content

Commit

Permalink
⚡ perf: improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchuo committed Jan 8, 2019
1 parent 7343489 commit ba26b2b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 55 deletions.
81 changes: 26 additions & 55 deletions AdvancedLevel_C++/1153. Decode Registration Card of PAT (25).cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,46 @@
#include <unordered_map>
#include <algorithm>
using namespace std;
struct stu {
string id;
int sco;
struct node {
string t;
int value;
};
struct site {
string siteId;
int cnt;
};
bool cmp1(const stu &a, const stu &b) {
return a.sco == b.sco ? a.id < b.id : a.sco > b.sco;
}
bool cmp2(const site &a, const site &b) {
return a.cnt == b.cnt ? a.siteId < b.siteId : a.cnt > b.cnt;
bool cmp(const node &a, const node &b) {
return a.value != b.value ? a.value > b.value : a.t < b.t;
}
int main() {
int n, k;
int n, k, num;
string s;
cin >> n >> k;
vector<stu> v(n);
vector<node> v(n);
for (int i = 0; i < n; i++)
cin >> v[i].id >> v[i].sco;
cin >> v[i].t >> v[i].value;
for (int i = 1; i <= k; i++) {
int num;
scanf("%d", &num);
cin >> num >> s;
printf("Case %d: %d %s\n", i, num, s.c_str());
vector<node> ans;
int cnt = 0, sum = 0;
if (num == 1) {
string level;
cin >> level;
printf("Case %d: %d %s\n", i, num, level.c_str());
vector<stu> ans;
for (int i = 0; i < n; i++) {
if (v[i].id[0] == level[0])
ans.push_back(v[i]);
}
sort(ans.begin(), ans.end(),cmp1);
for (int i = 0; i < ans.size(); i++)
printf("%s %d\n", ans[i].id.c_str(), ans[i].sco);
if (ans.size() == 0) printf("NA\n");
for (int j = 0; j < n; j++)
if (v[j].t[0] == s[0]) ans.push_back(v[j]);
} else if (num == 2) {
int cnt = 0, sum = 0;
int siteId;
cin >> siteId;
printf("Case %d: %d %d\n", i, num, siteId);
vector<stu> ans;
for (int i = 0; i < n; i++) {
if (v[i].id.substr(1, 3) == to_string(siteId)) {
for (int j = 0; j < n; j++) {
if (v[j].t.substr(1, 3) == s) {
cnt++;
sum += v[i].sco;
sum += v[j].value;
}
}
if (cnt != 0) printf("%d %d\n", cnt, sum);
else printf("NA\n");
} else if (num == 3) {
string date;
cin >> date;
printf("Case %d: %d %s\n", i, num, date.c_str());
vector<site> ans;
unordered_map<string, int> m;
for (int i = 0; i < n; i++) {
if (v[i].id.substr(4, 6) == date) {
string tt = v[i].id.substr(1, 3);
m[tt]++;
}
}
for (auto it : m)
ans.push_back({it.first, it.second});
sort(ans.begin(), ans.end(),cmp2);
for (int i = 0; i < ans.size(); i++)
printf("%s %d\n", ans[i].siteId.c_str(), ans[i].cnt);
if (ans.size() == 0) printf("NA\n");
for (int j = 0; j < n; j++)
if (v[j].t.substr(4, 6) == s) m[v[j].t.substr(1, 3)]++;
for (auto it : m) ans.push_back({it.first, it.second});
}
sort(ans.begin(), ans.end(),cmp);
for (int j = 0; j < ans.size(); j++)
printf("%s %d\n", ans[j].t.c_str(), ans[j].value);
if (((num == 1 || num == 3) && ans.size() == 0) || (num == 2 && cnt == 0)) printf("NA\n");
}
return 0;
}
}
48 changes: 48 additions & 0 deletions BasicLevel_C++/1095. 解码PAT准考证 (25).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
struct node {
string t;
int value;
};
bool cmp(const node &a, const node &b) {
return a.value != b.value ? a.value > b.value : a.t < b.t;
}
int main() {
int n, k, num;
string s;
cin >> n >> k;
vector<node> v(n);
for (int i = 0; i < n; i++)
cin >> v[i].t >> v[i].value;
for (int i = 1; i <= k; i++) {
cin >> num >> s;
printf("Case %d: %d %s\n", i, num, s.c_str());
vector<node> ans;
int cnt = 0, sum = 0;
if (num == 1) {
for (int j = 0; j < n; j++)
if (v[j].t[0] == s[0]) ans.push_back(v[j]);
} else if (num == 2) {
for (int j = 0; j < n; j++) {
if (v[j].t.substr(1, 3) == s) {
cnt++;
sum += v[j].value;
}
}
if (cnt != 0) printf("%d %d\n", cnt, sum);
} else if (num == 3) {
unordered_map<string, int> m;
for (int j = 0; j < n; j++)
if (v[j].t.substr(4, 6) == s) m[v[j].t.substr(1, 3)]++;
for (auto it : m) ans.push_back({it.first, it.second});
}
sort(ans.begin(), ans.end(),cmp);
for (int j = 0; j < ans.size(); j++)
printf("%s %d\n", ans[j].t.c_str(), ans[j].value);
if (((num == 1 || num == 3) && ans.size() == 0) || (num == 2 && cnt == 0)) printf("NA\n");
}
return 0;
}

1 comment on commit ba26b2b

@zaiyangliu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good solution!

Please sign in to comment.