https://pintia.cn/problem-sets/994805342720868352/problems/994805393241260032
就是排序,注意一些細節即可。
#include<bits/stdc++.h>
using namespace std
;
struct node
{string name
;int s
[10],cnt
;int sum
=0;
}temp
;
unordered_map
<string
,node
>mp
;
unordered_map
<string
,int>hush
;
vector
<node
>ve
;
int a
[10],n
,k
,m
;
bool cmp(node a
,node b
)
{if(a
.sum
==b
.sum
){if(a
.cnt
==b
.cnt
)return a
.name
<b
.name
;return a
.cnt
>b
.cnt
;}return a
.sum
>b
.sum
;
}
int main(void)
{scanf("%d%d%d",&n
,&k
,&m
);for(int i
=0;i
<k
;i
++) scanf("%d",&a
[i
]);for(int i
=0;i
<m
;i
++){string name
;int id
,score
;cin
>>name
;scanf("%d%d",&id
,&score
);if(!hush
[name
]){for(int j
=0;j
<k
;j
++) mp
[name
].s
[j
]=-2;mp
[name
].s
[id
-1]=score
;hush
[name
]=1;}else mp
[name
].s
[id
-1]=max({score
,mp
[name
].s
[id
-1]});}for(auto i
=mp
.begin();i
!=mp
.end();i
++){temp
.name
=i
->first
;int cnt
=0;temp
.sum
=0;bool flag
=0;for(int i
=0;i
<k
;i
++){temp
.s
[i
]=mp
[temp
.name
].s
[i
];if(temp
.s
[i
]==a
[i
]) cnt
++;if(temp
.s
[i
]>=0) temp
.sum
+=temp
.s
[i
],flag
=1;}temp
.cnt
=cnt
;if(flag
) ve
.push_back(temp
);}sort(ve
.begin(),ve
.end(),cmp
);int index
=1;for(int i
=0;i
<ve
.size();i
++) {printf("%d",index
);printf(" %s %d",ve
[i
].name
.c_str(),ve
[i
].sum
);for(int j
=0;j
<k
;j
++) {if(ve
[i
].s
[j
]==-2) printf(" -");else if(ve
[i
].s
[j
]==-1) printf(" 0");else printf(" %d",ve
[i
].s
[j
]);}puts("");if(i
+1!=ve
.size()&&ve
[i
].sum
!=ve
[i
+1].sum
) index
=i
+2;}return 0;
}
總結
以上是生活随笔為你收集整理的1075 PAT Judge (25 分)【难度: 一般 / 知识点: 多关键字排序】的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。