您的位置:首页 > 其它

普及练习场 排序Ex 瑞士轮

2017-12-14 21:41 211 查看
题目链接

题意理解

这题我用Java写报RE,我当时不能理解,下载了数据一看,结果是对的啊,于是猜测是不是TLE了。。。然后就拿cpp重写了一下。果然,不仅测试用例多过掉了一个,之前报RE的也都变成TLE了。。。当然之前都是用的最简单粗暴的方式写的,但是这题数据量稍微有点大,不能简单粗暴,为此,需要针对这一题的内容,进行针对性优化排序算法。最后,我选择了用cpp写这题,但是为了补一下Java的知识,我决定还是去查一下吧。。。我想想要不要用Java重写一下。后来我查了一下,发现Java的sort就是merge sort,只能说明Java真的太慢了而不是我代码写的不好。 我写cpp真的不是因为我懒!!! 等我有时间还是重新写一个Java版本的merge吧。

比赛前分数是有序的,在比完之后,胜者组应该还是有序的,败者组应该还是有序的,因此用merge。

代码

#include <cstring>
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>

using namespace std;

void read(int &x) {
x = 0;
int f = 1;
char ch = getchar();
while(ch > '9'||ch < '0') {
if(ch == '-') {
f = -1;
}
ch = getchar();
}
while(ch >= '0' && ch <= '9') {
x = x * 10 + (int)(ch - 48);
ch = getchar();
}
x = x * f;
}
const int maxn = 200050;
typedef struct {
int score;
int strength;
int index;
} Player;

Player players[maxn];
Player a1[maxn];
Player a2[maxn];

int cmp(Player p1, Player p2) {
if(p1.score == p2.score) {
return p1.index < p2.index;
} else {
return p1.score > p2.score;
}
}

int main() {
int N, R, Q;
read(N);
read(R);
read(Q);
for(int i = 0; i < 2 * N; i++) {
read(players[i].score);
players[i].index = i + 1;
}
for(int i = 0; i < 2 * N; i++) {
read(players[i].strength);
}

sort(players, players + 2 * N, cmp);
for(int i = 0; i < R; i++) {
for(int j = 0; j < 2 * N; j += 2) {
if(players[j].strength > players[j+1].strength) {
players[j].score++;
a1[j / 2] = players[j];
a2[j / 2] = players[j + 1];
} else {
players[j+1].score++;
a1[j / 2] = players[j + 1];
a2[j / 2] = players[j];
}
}
merge(a1, a1 + N, a2, a2 + N, players, cmp);
}
cout << players[Q - 1].index << endl;
return 0;
}


欢迎加入“不会算法一群菜鸟”,群号是⑥⑥①⑨②2025,这是我设置的一道很低的门槛用来阻止广告的。入群的验证暗号是:我爱编译原理
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: