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;
}
#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 (Advanced) 1056. Mice and Rice (25)
- 1056. Mice and Rice (25)
- PAT 1056. Mice and Rice (25)
- PAT (Advanced Level) Practise 1056 Mice and Rice (25)
- PAT 甲级 1056. Mice and Rice (25)
- 1056. Mice and Rice (25)
- 1056.Mice and Rice (25)...to be continued...
- PAT (Advanced Level) 1056. Mice and Rice (25) 模拟比赛,用queue辅助
- 1056. Mice and Rice (25)
- PAT甲级1056. Mice and Rice (25)
- 1056. Mice and Rice (25)
- PAT甲题题解-1056. Mice and Rice (25)-模拟题
- PAT 1056. Mice and Rice (25)(依照一定顺序给数列排序并输出)
- 1056. Mice and Rice (25)
- PAT 1056. Mice and Rice (25)
- PAT A 1056. Mice and Rice (25)
- 1056. Mice and Rice (25)
- PAT (Advanced Level) 1056. Mice and Rice (25)
- 1056. Mice and Rice (25)
- 1056. Mice and Rice (25)