Decode Registration Card of PAT

PAT-1153

  • 这里需要注意题目的规模,并不需要一开始就存储好所有的满足题意的信息
  • 这里必须使用unordered_map否则会超时
  • vector的使用需要注意,只有一开始赋予了容量才能读取。
  • 不需要使用set也可以
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<sstream>
#include<set>
#include<map>
#include<cmath>
#include<vector>
using namespace std;
const int maxn=10004;
int n,m;
struct Node{
string code;
char type;
int site;
string date;
int number;
int score;
Node(){
}
Node(string co,char ty,int si,string da,int nu,int sc):
code(co),type(ty),site(si),date(da),number(nu),score(sc){
}
bool operator<(const Node& node)const{
if(score==node.score){
return code<node.code;
}else return score>node.score;
}
int times;
};
struct NType3{
int site,times;
NType3(){
}
NType3(int a,int b):site(a),times(b){
}
bool operator<(const NType3& node2)const{
if(times==node2.times){
return site<node2.site;
}else return times>node2.times;
}
};
set<Node>type1[26];
map<int,int>sit;
map<int,int>totscore;
map<int,int>dat[1000006];
set<NType3>type3[1000006]; int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
string code;int score;
cin>>code>>score;
char level=code[0];
int site=stoi(code.substr(1,3));
string date=code.substr(4,6);
int number=stoi(code.substr(10));
// cout<<level<<" "<<site<<" "<<date<<" "<<number<<endl;
Node node=Node(code,level,site,date,number,score);
sit[site]++;
totscore[site]+=score;
type1[level-'A'].insert(node);
if(dat[stoi(date)][site]==0){
set<NType3>te;
te.insert(NType3(site,0));
type3[stoi(date)].insert(NType3(site,0));
}
dat[stoi(date)][site]++;
}
for(int i=0;i<m;i++){
int type;string s;
cin>>type>>s;
if(type==1){//level
set<Node> now=type1[s[0]-'A'];
set<Node>::iterator it;
int num=0;
for(it=now.begin();it!=now.end();it++){
Node no=*it;
cout<<no.code<<" "<<no.score<<endl;
num++;
}
if(num==0)
cout<<"NA"<<endl;
}else if(type==2){//site
int numsit=sit[stoi(s)];
if(numsit==0){
cout<<"NA"<<endl;
}else cout<<sit[stoi(s)]<<" "<<totscore[stoi(s)]<<endl;
}else{//date
set<NType3> now=type3[stoi(s)];
set<NType3> temp;
for(set<NType3>::iterator it=now.begin();it!=now.end();it++){
NType3 no=*it;
int site=no.site;
int times=dat[stoi(s)][site];
no.times=times;
temp.insert(no);
}
set<NType3>::iterator it;
int num=0;
for(it=temp.begin();it!=temp.end();it++){
NType3 no=*it;
cout<<no.site<<" "<<no.times<<endl;
num++;
}
if(num==0)
cout<<"NA"<<endl;
}
}
return 0;
}
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<sstream>
#include<set>
#include<map>
#include<cmath>
#include<vector>
#include<unordered_map>
using namespace std;
const int maxn=10004;
int n,m;
struct Node{
string code;
int value;
}; bool cmp(Node& node1,Node& node2){
if(node1.value==node2.value){
return node1.code<node2.code;
}else return node1.value>node2.value;
}
int main(){
cin>>n>>m;
vector<Node>v(n);//只有指定了容器大小才能使用下面输入语句
for(int i=0;i<n;i++){
string code;int score;
// cin>>code>>score;
// Node node;
// node.code=code;
// node.value=score;
// v.push_back(node);
cin>>v[i].code>>v[i].value;
}
for(int i=1;i<=m;i++){
int type;string s;
cin>>type>>s; printf("Case %d: %d %s\n",i,type,s.c_str());
// cout<<"Case "<<i<<": "<<type<<" "<<s<<endl;
if(type==1){
vector<Node>ve;
for(Node item:v){
if(item.code.substr(0,1)==s){
ve.push_back(item);
}
}
if(ve.size()==0)
printf("NA\n");
else{
sort(ve.begin(),ve.end(),cmp);
for(Node item:ve){
printf("%s %d\n",item.code.c_str(),item.value);
// cout<<item.code<<" "<<item.value<<endl;
}
}
}else if(type==2){
int num=0,tot=0;
for(Node item:v){
if(s==item.code.substr(1,3)){
num++;
tot+=item.value;
}
}
if(num==0)
printf("NA\n");
else {
printf("%d %d\n",num,tot);
// cout<<num<<" "<<tot<<endl;
}
}else if(type==3){
unordered_map<string,int>ma;
for(Node item:v){
if(s==item.code.substr(4,6)){
string site=item.code.substr(1,3);
ma[site]++;
}
}
if(ma.size()==0)
printf("NA\n");
else{
vector<Node>now;
for(auto item:ma){
now.push_back({item.first,item.second});
}
sort(now.begin(),now.end(),cmp);
for(Node item:now){
printf("%s %d\n",item.code.c_str(),item.value);
}
}
}
}
// cout<<"jhjj"<<endl;
return 0;
}

最新文章

  1. javascript设计模式:策略模式
  2. makefile之变量赋值
  3. liunx 系统 git clone ssh代码时需要sshkey
  4. Lucene 4.X 倒排索引原理与实现: (3) Term Dictionary和Index文件 (FST详细解析)
  5. Java NIO入门(二):缓冲区内部细节
  6. maven中解决javax.servlet.jsp.PageContext cannot be resolved to a type
  7. (转载)ubuntu创建、删除文件及文件夹,强制清空回收站方法
  8. thinkphp中的_get,_post,_request
  9. BZOJ 2096([Poi2010]Pilots-单调队列-差值)
  10. 安卓工程中定义的app_name等报错解决办法 工程上有叹号
  11. ubuntu配置LAMP
  12. Laravel框架开发规范-修订版
  13. 第二次项目冲刺(Beta阶段)--第七天
  14. Idea中右边的maven projects窗口找不到了如何调出来
  15. Sniffer初识
  16. sass重构响应式unofficial‘s博客轻松适应移动端
  17. LinkedList与ArrayList的区别
  18. JavaScript中HTML DOM focus()与onblur() setSelectionRange()用法
  19. Lua入门教程
  20. Codeforces729D(SummerTrainingDay01-F)

热门文章

  1. 【poj 1988】Cube Stacking(图论--带权并查集)
  2. 【noi 2.6_1759】LIS 最长上升子序列(DP,3种解法)
  3. 【noi 2.6_8786】方格取数(DP)
  4. 2015ACM/ICPC亚洲区沈阳站-重现赛 M - Meeting (特殊建边,最短路)
  5. .net面试--值类型和引用类型
  6. 金牛来到,福气来到——TcaplusDB新年放送
  7. Java中new一个对象是一个怎样的过程?JVM中发生了什么?
  8. nodejs非安装版配置(windows)
  9. C++中函数的形式参数引用
  10. filter_var 函数()绕过执行命令