PTA A_1056 Mice and Rice
传送门:https://pintia.cn/problem-sets/994805342720868352/problems/994805419468242944解:
队列
#include<cstdio>
#include<queue>
using namespace std;
struct{
int weight, rank;
}mouse;
int main(void)
{
int np, ng;
scanf("%d %d", &np, &ng);
queue<int> q;
for (int i = 0; i < np; i++)
scanf("%d", &mouse.weight);
for (int i = 0; i < np; i++)
{
int order;
scanf("%d", &order);
q.push(order);
}
int len;
while ((len = q.size()) > 1)
{
int group = (len % ng) ? (len / ng + 1) : (len / ng);
// printf("group = %d, ", group);
for (int i = 0; i < group; i++)
{
int winer = q.front();
for (int j = 0; j < ng && (i * ng + j) < len; j++)
{
mouse.rank = group + 1;
if (mouse.weight > mouse.weight)
winer = q.front();
// printf("%d ", mouse.weight);
q.pop();
}
q.push(winer);
}
// putchar('\n');
}
mouse.rank = 1;
for (int i = 0; i < np; i++)
{
printf("%d", mouse.rank);
if (i < np - 1)
putchar(' ');
}
return 0;
}
页:
[1]