每一輪快排,超時:
# include
# include
# include
# include
using
namespace std;
class
athlete
// athlete
(int _id,
int _competence,
int _score):id
(_id)
,competence
(_competence)
,score
(_score)};
bool
comp
(const athlete& _a,
const athlete& _b)
intmain()
sort
(athlete_vec.
begin()
,athlete_vec.
end(
),comp)
;for
(int round=
0;round++round)
sort
(athlete_vec.
begin()
,athlete_vec.
end(
),comp);}
cout<.id+
1
init_score_arr;
delete
competence_arr;
return0;
}
每次比賽之後,分數按照降序排列,每兩人中的贏的人都+1分,贏的n個人仍為降序,輸的n個人也仍為降序,所以將贏的序列與輸的序列歸併即可。每輪o(n)複雜度。而快排每輪o(nlogn)。
用vector push_back出現超時,用new分配空間accepeted,應該是push_back太慢。
# include
# include
# include
# include
using
namespace std;
class
athlete
// athlete
(int _id,
int _competence,
int _score):id
(_id)
,competence
(_competence)
,score
(_score)};
bool
comp
(const athlete& _a,
const athlete& _b)
intmain()
sort
(athlete_vec.
begin()
,athlete_vec.
end(
),comp)
;for
(int i=
0;i<
2*n;
++i)
int* winner_id_arr=
newint
[n];
int* loser_id_arr=
newint
[n];
for(
int round=
0;round++round)
else
winner_id_arr[i]
=winner_id;
loser_id_arr[i]
=loser_id;
}// printf("winner_id_vec: ");
// for(int i=0;i// printf("%d, ",winner_id_vec[i]);
// cout//merge sort
int winner_i=0;
int loser_i=0;
int i=0;
while
(winner_ielse
}while
(winner_iwhile
(loser_i// printf("rank: ");
// for(int i=0;i<2*n;++i)
// printf("%d, ",rank_id_arr[i]);
// cout<}
cout<+1
winner_id_arr;
delete
loser_id_arr;
delete
score_arr;
delete
competence_arr;
delete
rank_id_arr;
return0;
}
洛谷 P1309 瑞士輪(歸併)
題意 一共2n個人 有乙個初始分數 進行k輪比賽,每輪比賽按當前得分排出名次,然後比賽按照第1名和第2名,第3和第4 第2n 1和2n進行 實力各不相同且實力大的總能獲勝 獲勝方加一分,失敗方分數不變。思路 一開始想在每輪比賽前用sort排序,算了下複雜度大概o n n logn n k 顯然不行。...
歸併 STL 洛谷 P1309 瑞士輪
樸素演算法每輪都sort會tle 所以要優化 用歸併 難點在於想明白為什麼是歸併 比賽過程中 輸贏持續有序 這是歸併的前提條件 所以才可以歸併 include include using namespace std const int maxn 2e5 10 struct team arr maxn...
洛谷P1309 瑞士輪
本題同樣是noip普及組第三題。因為太久沒有做過題目了,先從普及組開始練習吧。題目內容 很顯然想到的思路就是模擬,如下 include include include include using namespace std struct player bool cmp player a,player...