这道题有点难读懂,做了n久,理解错误,其实他的意思是,序列按给出站的编号站好位置,开始筛选,最后按老鼠编号的顺序输出 即开始按 编号6 编号0 编号8 编号7 编号10 编号5 编号9 编号1 编号4 编号2 编号3站好 筛选完成 按 编号0 编号1 编号2 …编号10输出
#include<iostream>#include<vector>#include<algorithm>#PRagma warning(disable:4996)using namespace std;int P, G;struct node { int data; int rank; int index;//代表序号 int n;//代表位置号 node() { rank = 0; } bool Operator<(const node that)const { //排序,保持原来站位的基础上,把有rank的放后面去 if (this->rank < that.rank || (this->rank == that.rank && this->n < that.n)) return true; return false; }};vector<node> all;int main(){ cin >> P >> G; all.resize(P); for (int t = 0;t < P;t++) { scanf("%d", &all[t].data); all[t].index = t; } for (int t = 0;t < P;t++) { int temp; scanf("%d", &temp); all[temp].n = t; } sort(all.begin(), all.end(), [](node a, node b) {return a.n < b.n;});//按给出的序列位置站好 int num = P; while (num != 1)//进行筛选,循环 { int temp = num%G != 0 ?num / G+2 : num / G+1; for (int i = 0;i < num;i+= G) { int max=all[i].data, v=i; all[i].rank = temp; for (int j = i+1;j < i + G;j++) { if ( j >= P||all[j].rank != 0) break; if (max < all[j].data) { max = all[j].data;v = j; } all[j].rank = temp; } all[v].rank = 0; } num = temp-1; sort(all.begin(),all.end()); } all[0].rank = 1; sort(all.begin(), all.end(), [](node a, node b) {return a.index < b.index;});//按位置排序 int flag = 0; for(auto x:all)//输出 if (flag == 0) { flag = 1;printf("%d", x.rank); } else printf(" %d", x.rank); cout << endl;}新闻热点
疑难解答