您的位置:首页 > 其它

1056. Mice and Rice (25)

2017-02-13 00:13 239 查看
感觉写的好繁琐。。。

#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<string>
#include<set>
#include<stack>
#include<queue>
using namespace std;
struct node{
int data;
int w;
int lev;
int r;
};
vector<node> num;
queue<int> q, p;
bool comp(node a, node b){
if(a.lev > b.lev) return true;
else return false;
}
bool comp1(node a, node b){
if(a.data < b.data) return true;
else return false;
}
int main(){
int n, k;
cin>>n>>k;
// int temp[n + 1];
// int w[n + 1];
// int lev[n + 1];
for(int i = 0; i < n; i++){
node a;
scanf("%d",&a.w);
a.data = i;
a.lev = 1;
num.push_back(a);
}
for(int i = 0; i < n; i++){
int ans;
scanf("%d",&ans);
q.push(ans);
}
while(q.size() != 1){
while(!q.empty()){
int count = 0;
int max = -1;
int maxi = 0;
while(count < k && !q.empty()){
int temp = q.front();
q.pop();
if(num[temp].w > max){
max = num[temp].w;
maxi = temp;
}
count++;
}
num[maxi].lev++;
p.push(maxi);
}
q = p;
while(!p.empty()){
p.pop();
}
}
sort(num.begin(),num.end(),comp);
int level = num[0].lev;
int j = 1;
int ran = 1;
for(int i = 0; i < n; i++){
if(num[i].lev == level){
num[i].r = ran;
j++;
}
else{
level = num[i].lev;
ran = j++;
num[i].r = ran;
}
}
sort(num.begin(),num.end(),comp1);
for(int i = 0; i < n; i++){
if(i != 0) printf(" ");
printf("%d",num[i].r);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PAT