
代码好像有点问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#include<vector> #include<algorithm> #include<math.h> #include<string.h> #include<map> #include<set> #include<iostream> #include<string> #include<stack> #include<queue> using namespace std;
struct { int num; int weight; int ran; }mice[1010];
queue<mouse> q;
int getGroup(int np,int ng) { int group; int t=np%ng; if(t==0) { group=np/ng; } else group=np/ng+1; return group; } int main() { int np,ng; scanf("%d%d",&np,&ng); for(int i=0;i<np;i++) { scanf("%d",&mice[i].weight); mice[i].num=i; } int temp; for(int i=0;i<np;i++) { scanf("%d",&temp); q.push(mice[temp]); } mouse tempMouse; while(q.size()!=1) { queue<mouse> tempQueue; int group=getGroup(q.size(),ng); for(int i=0;i<group;i++) { int maxWeight=-1; int maxNum=-1; for(int j=0;j<ng;j++) { tempMouse=q.front(); q.pop(); if(tempMouse.weight>maxWeight) { maxWeight=tempMouse.weight; maxNum=tempMouse.num; } mice[tempMouse.num].ran=group+1; } tempQueue.push(mice[maxNum]); } q=tempQueue; } tempMouse=q.front(); mice[tempMouse.num].ran=1; for(int i=0;i<np-1;i++) { printf("%d ",mice[i].ran); } printf("%dn",mice[np-1].ran); return 0; }
|
Sharing
注意!scanf使用%c格式时是可以读入空格的,因此在输入地址、数据及后继结点地址时,格式不能写成%d%c%d,必须在中间加空格。
近期评论