PAT A1056 Mice and Rice
最开始没想到用队列,数组循环计数把自己绕死了,后来看了书上 这里注意几点:1.push最大值是在循环结束之后;2.只有if(i * K + j == tmp) break;用到了i * K + j(实际上还不如直接用cnt ++),其他都是用队头出来的idx;3.max要给初值,或者说每次循环用过之后要复位成最小值,不然后来者可能盖不过 有一个点挂了,先睡了 经分析发现当只有一个数的时候不会进入循环,无法给rank赋值,那个测试点也就是这么回事,所以需要把ranky[order.front()] = 1;扔到循环外面即可
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <stack>
using namespace std;
vector< int > weight, ranky;
queue< int > order;
#define DEBUG
int main ( ) {
#ifdef DEBUG
freopen ( "1.txt" , "r" , stdin ) ;
#endif
int num, K;
cin >> num >> K;
for ( int i = 0 ; i < num; i ++ ) {
int w;
cin >> w;
weight. push_back ( w) ;
}
for ( int i = 0 ; i < num; i ++ ) {
int od;
cin >> od;
order. push ( od) ;
}
ranky. resize ( num) ;
int nowNG, tmp = num;
while ( order. size ( ) > 1 ) {
if ( tmp % K == 0 ) nowNG = tmp / K;
else nowNG = tmp / K + 1 ;
for ( int i = 0 ; i < nowNG; i ++ ) {
int max = - 1 , maxi = - 1 ;
for ( int j = 0 ; j < K; j ++ ) {
if ( i * K + j == tmp) break ;
int idx = order. front ( ) ;
order. pop ( ) ;
if ( weight[ idx] > max) {
max = weight[ idx] ;
maxi = idx;
}
ranky[ idx] = nowNG + 1 ;
}
order. push ( maxi) ;
}
tmp = nowNG;
}
ranky[ order. front ( ) ] = 1 ;
for ( int i = 0 ; i < num; i ++ ) {
cout << ranky[ i] ;
if ( i < num - 1 ) cout << ' ' ;
}
return 0 ;
}