是根据弄夫大佬的bilibili课程分类专题,练习的。
1028 List Sorting 25
L i s t S o r t i n g List Sorting ListSorting
error code:20
偷懒使用string读入,则当col==3时, string 是字典排序表,对于成绩60和9, s t r ( 60 ) < s t r ( 9 ) str(60)<str(9) str(60)<str(9)
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main(){
int n,c;cin>>n>>c;
vector<vector<string>>stu(n,vector<string>(3));
for(int i=0;i<n;i++) cin>>stu[i][0]>>stu[i][1]>>stu[i][2];
if(c==1){
sort(stu.begin(),stu.end(),[&](const vector<string>&a,const vector<string>& b ){
return a[0]<b[0];
});
}
else if(c==2){
sort(stu.begin(),stu.end(),[&](const vector<string>&a,const vector<string>& b ){
return a[1]==b[1]? a[0]<b[0]: a[1]<b[1];
});
}
else{//error
sort(stu.begin(),stu.end(),[&](const vector<string>&a,const vector<string>& b ){
return a[2]==b[2]? a[0]<b[0]: a[2]<b[2];
});
}
cout<<stu[0][0]<<" "<<stu[0][1]<<" "<<stu[0][2];
for(int i=1;i<stu.size();i++) cout<<endl<<stu[i][0]<<" "<<stu[i][1]<<" "<<stu[i][2];
return 0;
}
code:
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
struct stu{
string id,name;
int gr;
};
int main(){
int n,c;cin>>n>>c;
vector<stu>st(n);
for(int i=0;i<n;i++) cin>>st[i].id>>st[i].name>>st[i].gr;
if(c==1){
sort(st.begin(),st.end(),[&](const stu&a,const stu& b ){
return a.id<b.id;
});
}
else if(c==2){
sort(st.begin(),st.end(),[&](const stu&a,const stu& b ){
return a.name==b.name? a.id<b.id: a.name<b.name;
});
}
else{
sort(st.begin(),st.end(),[&](const stu&a,const stu& b ){
return a.gr==b.gr? a.id<b.id: a.gr<b.gr;
});
}
for(int i=0;i<st.size();i++) cout<<st[i].id<<" "<<st[i].name<<" "<<st[i].gr<<endl;
return 0;
}
1025 PAT Ranking
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long LL;
const int N=3e5+10;
struct stu{
string id;int loc,gr;
};
stu st[N];
int main(){
int n,k,cnt=0;
cin>>n;
for(int i=1;i<=n;i++){
cin>>k;
for(int j=0;j<k;j++){
cin>>st[cnt].id>>st[cnt].gr;
st[cnt].loc=i;
cnt++;
}
}
sort(st,st+cnt,[&](const stu&a,const stu& b){
return a.gr==b.gr? a.id<b.id: a.gr>b.gr;
});
cout<<cnt;
vector<int> lrk(n+1,0),lgr(n+1,-1),ct(n+1,1);
int pregr=0,prerank=0;
for(int i=0;i<cnt;i++){
int loc=st[i].loc,gr=st[i].gr;
if(gr!=pregr) prerank=i+1,pregr=gr;
if(gr!=lgr[loc]) lgr[loc]=gr,lrk[loc]+=ct[loc],ct[loc]=1;
else ct[loc]++;
cout<<endl<<st[i].id<<" "<<prerank<<" "<<loc<<" "<<lrk[loc];
}
return 0;
}
1029 Median
#include<iostream>
#include<algorithm>
using namespace std;
const int N=2e5+10;
int w1[N],w2[N];
int main(){
int n1,n2;
cin>>n1;
for(int i=0;i<n1;i++) scanf("%d",&w1[i]);
cin>>n2;
for(int i=0;i<n2;i++) scanf("%d",&w2[i]);
int p1=0,p2=0,p=0,tar=(n1+n2-1)>>1;
while(p1<n1&&p2<n2){
if(w1[p1]<w2[p2]){
if(p==tar){
cout<<w1[p1];return 0;
}
p1++;p++;
}
else{
if(p==tar){
cout<<w2[p2];return 0;
}
p2++;p++;
}
}
if(p1<n1) cout<<w1[tar-p+p1];
else cout<<w2[tar-p+p2];
return 0;
}
1047 Student List for Course
!!!\ 可能有重名学生:用multiset
#include<map>
#include<set>
#include<vector>
#include<iostream>
using namespace std;
int main(){
int n,k,c,cr;//人数,总课数,单人课数,课
cin>>n>>k;
multiset<int>cor[2510];//(可能有重名学生:用multiset)
char id[4];
for(int i=0;i<n;i++){
scanf("%s",id);
int key=id[3]-'0'+10*(id[2]-'A')+260*(id[1]-'A')+6760*(id[0]-'A');
cin>>c;
for(int j=0;j<c;j++){
cin>>cr;cor[cr].insert(key);
}
}
for(int cr=1;cr<=k;cr++){
cout<<cr<<" "<<cor[cr].size()<<endl;
for(int id:cor[cr]){
printf("%c%c%c%d\n",id/6760+'A',id%6760/260+'A',id%260/10+'A',id%10);
}
}
return 0;
}
1052 Linked List Sorting
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
const int N=1e5+10;
int adr2key[N],adr2nex[N],key2adr[N*2];
// unorder_map<int,int>ma;
int main(){
int n,root;
cin>>n>>root;
int adr,key,nex;
for(int i=0;i<n;i++){
cin>>adr>>key>>nex;key+=N;
adr2key[adr]=key;
adr2nex[adr]=nex;
key2adr[key]=adr;
}
if(root==-1){
cout<<0<<" "<<-1;
return 0;
}
adr=root;
vector<int>keys;
while(adr!=-1){
keys.push_back(adr2key[adr]);
adr=adr2nex[adr];
}
sort(keys.begin(),keys.end());
root=key2adr[keys[0]];
printf("%d %05d\n",keys.size(),root);
for(int i=0;i<keys.size()-1;i++){
key=keys[i];adr=key2adr[key];
printf("%05d %d %05d\n",adr,key-N,key2adr[keys[i+1]]);
}
printf("%05d %d %d\n",key2adr[keys.back()],keys.back()-N,-1);
return 0;
}
code by 弄夫
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
const int N=1e5+10;
int adr2key[N],adr2nex[N];
int main(){
int n,root;
cin>>n>>root;
int adr,key,nex;
for(int i=0;i<n;i++){
cin>>adr>>key>>nex;
adr2key[adr]=key;
adr2nex[adr]=nex;
}
adr=root;
vector<int>adrs;
while(adr!=-1){
adrs.push_back(adr);
adr=adr2nex[adr];
}
sort(adrs.begin(),adrs.end(),[&](const int& a,const int& b){
return adr2key[a]<adr2key[b];
});
printf("%d",adrs.size());
for(int ar:adrs) printf(" %05d\n%05d %d",ar,ar,adr2key[ar]);
printf(" %d",-1);
return 0;
}
1055 The World’s Richest
code 优先队列== L e e t C o d e LeetCode LeetCode:合并k个有序链表:
#include<vector>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
#include<string.h>
using namespace std;
const int N=1e5+10,AG=201;
struct peo{
string name;
int ag,mon;
};
vector<peo> ages[AG];
int keep[AG];
bool cmp(const peo& a,const peo& b){
if(a.mon!=b.mon) return a.mon>b.mon;
else return a.ag==b.ag? a.name<b.name: a.ag<b.ag;
}
struct qu_cmp{
bool operator ()(const int& a,const int& b){
peo& x=ages[a][keep[a]],&y =ages[b][keep[b]];
return !cmp(x,y);
}
};
void que(int X,int m,int l,int r){
printf("Case #%d:\n",X);
memset(keep,0,sizeof keep);
priority_queue<int,vector<int>,qu_cmp>qu;
for(int i=l;i<=r;i++) if(ages[i].size()) qu.emplace(i);
if(qu.empty()){
printf("None\n");return;
}
while(m&&qu.size()){
int age=qu.top();qu.pop();
peo& p=ages[age][keep[age]++];m--;
if(ages[age].size()>keep[age]) qu.emplace(age);
printf("%s %d %d\n",p.name.c_str(),p.ag,p.mon);
}
}
int main(){
int n,k;
cin>>n>>k;
string name;int age,money;
for(int i=0;i<n;i++){
cin>>name>>age>>money;
ages[age].push_back(peo{name,age,money});
}
for(int i=1;i<AG;i++)
if(ages[i].size()) sort(ages[i].begin(),ages[i].end(),cmp);
int mrank,rage,lage;
for(int i=1;i<=k;i++){
cin>>mrank>>lage>>rage;
que(i,mrank,lage,rage);
}
return 0;
}
1062 Talent and Virtue
#include<vector>
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
struct peo{
string id;int virtue,total;
};
bool cmp(const peo& a,const peo& b){
if(a.total!=b.total) return a.total>b.total;
else return a.virtue==b.virtue? a.id<b.id: a.virtue>b.virtue;
}
int main(){
int n,l,h;
cin>>n>>l>>h;
vector<peo>sages,nobleman,fool,small;
string id;int virtue,talent;
for(int i=0;i<n;i++){
cin>>id>>virtue>>talent;
if(virtue<l||talent<l) continue;
if(virtue>=h&&talent>=h) sages.push_back({id,virtue,talent+virtue});
else if(virtue>=h) nobleman.push_back({id,virtue,talent+virtue});
else if(virtue>=talent) fool.push_back({id,virtue,talent+virtue});
else small.push_back({id,virtue,talent+virtue});
}
sort(sages.begin(),sages.end(),cmp);
sort(nobleman.begin(),nobleman.end(),cmp);
sort(fool.begin(),fool.end(),cmp);
sort(small.begin(),small.end(),cmp);
cout<<sages.size()+nobleman.size()+fool.size()+small.size()<<endl;
for(peo& p:sages) cout<<p.id<<" "<<p.virtue<<" "<<p.total-p.virtue<<endl;
for(peo& p:nobleman) cout<<p.id<<" "<<p.virtue<<" "<<p.total-p.virtue<<endl;
for(peo& p:fool) cout<<p.id<<" "<<p.virtue<<" "<<p.total-p.virtue<<endl;
for(peo& p:small) cout<<p.id<<" "<<p.virtue<<" "<<p.total-p.virtue<<endl;
return 0;
}