PAT | A1056 Mice and Rice

一开始没考虑一共只有一只老鼠的情况,导致测点2过不去。
AC代码:

#include 
#include 

using namespace std;

vector<int> programmer;
vector<int> seq;


int main(){
	int np,ng;
	// np : 人数
	// ng : 每组最多人数
	scanf("%d %d",&np,&ng);
	for(int i = 0;i < np;i++){
		int weight;
		scanf("%d",&weight);
		programmer.push_back(weight);
	}
	for(int i = 0;i < np;i++){
		int s;
		scanf("%d",&s);
		seq.push_back(s);
	}
	int rank[100010];
	if(seq.size() == 1){
		printf("1");
		system("pause");
		return 0;
	}
	while(seq.size() > 1){
		int groupNum;
		int lastGroup;
		if(seq.size() % ng == 0){
			groupNum = seq.size() / ng;
			lastGroup = ng;
		}
		else{
			groupNum = seq.size() / ng + 1;//一共可以分多少组
			lastGroup = seq.size() % ng;
		}
		for(int i = 0;i < groupNum;i++){//每一组[i * ng ... (i + 1) * ng - 1]
			int maxWeight = programmer[seq[i * ng]],maxNum = seq[i * ng];
			int j;
			for(j = i * ng;j <= (i + 1) * ng - 1 && j <= seq.size() - 1;j++){
				if(programmer[seq[j]] > maxWeight){
					maxWeight = programmer[seq[j]];
					maxNum = seq[j];
				}
			}
			for(int a = i * ng;a <= (i + 1) * ng - 1 && a <= seq.size() - 1;a++){
				if(seq[a] != maxNum)
					rank[seq[a]] = groupNum + 1;
			}
			seq[i] = maxNum;
		}
		vector<int>::iterator it = seq.begin() + groupNum;
		seq.erase(it,seq.end());
		if(seq.size() == 1)
			rank[seq[0]] = 1;
	}
	for(int i = 0;i < np;i++){
		if(i != np - 1)
			printf("%d ",rank[i]);
		else
			printf("%d",rank[i]);
	}
	system("pause");
	return 0;
}

你可能感兴趣的:(PTA,c++)