您的位置:首页 > 其它

1056. Mice and Rice (25)

2016-03-07 22:25 309 查看
传送门 :https://www.patest.cn/contests/pat-a-practise/1056
英语不好,所以读起来很吃力,好久才弄明白第一行11个数是编号从0-10的重量 而不是对应下方随机编号的.
AC代码
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <stack>
#include <map>
using namespace std;
struct player{
int id,rank,weight;
};
void solution(vector<player *> ans,int NG){
if(ans.size()==1){
ans[0]->rank = 1;
return ;
}
int len = (int)ans.size();
int x= ans.size()%NG==0?len/NG:len/NG+1;
vector<player *>nextMath;
int i = 0,j;
while (i<len) {
if(i+NG<len)
j = i+NG;
else
j = len;
sort(ans.begin()+i, ans.begin()+j, [](player *a ,player *b){
return a->weight>b->weight;
});
for (int k = i+1; k<j; k++)
ans[k]->rank = x+1;
nextMath.push_back(ans[i]);
i = j;
}
solution(nextMath, NG);
}
int main()
{
int NP,NG,a;
scanf("%d %d",&NP,&NG);
vector<player *>ans;
vector<int>weight(NP);
for (int i=0; i<NP; ++i) {
player * p = new player();
ans.push_back(p);
scanf("%d",&weight[i]);
}
for (int i=0; i<NP; ++i) {
scanf("%d",&a);
ans[i]->id = a;
ans[i]->weight = weight[a];
}
solution(ans, NG);
sort(ans.begin(), ans.end(), [](player *a ,player *b){
return a->id<b->id;
});
for (int i=0; i<NP; ++i) {
printf(i==NP-1?"%d\n":"%d ",ans[i]->rank);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: