這道題有點(diǎn)難讀懂,做了n久,理解錯(cuò)誤,其實(shí)他的意思是,序列按給出站的編號(hào)站好位置,開始篩選,最后按老鼠編號(hào)的順序輸出 即開始按 編號(hào)6 編號(hào)0 編號(hào)8 編號(hào)7 編號(hào)10 編號(hào)5 編號(hào)9 編號(hào)1 編號(hào)4 編號(hào)2 編號(hào)3站好 篩選完成 按 編號(hào)0 編號(hào)1 編號(hào)2 …編號(hào)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;//代表序號(hào) int n;//代表位置號(hào) node() { rank = 0; } bool Operator<(const node that)const { //排序,保持原來站位的基礎(chǔ)上,把有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)//進(jìn)行篩選,循環(huán) { 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;}新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注